mrosenbladt / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

Accessing with key that does not exist broken at revision 730c40a3fca54f5a829d1dd20f000e284099e1d8 #286

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to execute following code:

#include <yaml-cpp/yaml.h>
#include <iostream>

void foo( const YAML::Node & n ) {
  if( n["5"] ) { std::cout << "Success!" << std::endl; }
}

int main() {
  YAML::Node n;
  n["3"] = "4";
  foo( n );
  return 0;
}

Exception "invalid node" is broken at line:

if( n["5"] ) {...}

Seems functionality was broken at revision 
730c40a3fca54f5a829d1dd20f000e284099e1d8 "Fix memory leak when accessing a 
const Node with a key that doesn't exist."

My environment:
- Linux desktop 3.13.0-46-generic #77-Ubuntu SMP Mon Mar 2 18:23:39 UTC 2015 
x86_64 x86_64 x86_64 GNU/Linux
- GCC/G++ 4.9.2

Original issue reported on code.google.com by Dmitry.S...@gmail.com on 20 Mar 2015 at 6:45

GoogleCodeExporter commented 9 years ago
Possible reason is:
const Node::operator[ key ] returns Node( ZombieNode ) when specified key does 
not exist
Node( ZombieNode ).m_isValid == false
When trying to call Node( ZombieNode )::operator bool() with m_isValid == false 
exception InvalidNode is thrown
(see Node::IsDefined() const)

If Node( ZombieNode ) is not valid node, then it's incorrect to return such 
value at const Node::operator[ key ]
If Node( ZombieNode ) is valid, then it's necessary to change the constructor 
Node( Zombie )

Original comment by Dmitry.S...@gmail.com on 20 Mar 2015 at 8:41

GoogleCodeExporter commented 9 years ago
Thanks for the report!

Original comment by jbe...@gmail.com on 20 Mar 2015 at 3:57