jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
5.11k stars 1.84k forks source link

error messages still not fixed #821

Open seisowl opened 4 years ago

seisowl commented 4 years ago

I was excited to see the commit 012269756149ae99745b6dafefd415843d7420bb:

Improve error messages on operator[] or as<> (#656)

However when I was using today's HEAD (ce056acab7eb6a1af1872917771bad7a682e3d68), a simple calling node["non_existing_key"].as() simply core dumped without more hints. I hope it can be fixed and added to the Tests.

jbeder commented 4 years ago

Do you have example code, expected output, and actual output?

On Thu, Feb 13, 2020 at 10:01 PM seisowl notifications@github.com wrote:

I was excited to see the commit 0122697 https://github.com/jbeder/yaml-cpp/commit/012269756149ae99745b6dafefd415843d7420bb :

Improve error messages on operator[] or as<> (#656 https://github.com/jbeder/yaml-cpp/pull/656)

However when I was using today's HEAD (ce056ac https://github.com/jbeder/yaml-cpp/commit/ce056acab7eb6a1af1872917771bad7a682e3d68 ), a simple calling node["non_existing_key"].as() simply core dumped without more hints. I hope it can be fixed and added to the Tests.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jbeder/yaml-cpp/issues/821?email_source=notifications&email_token=AAICUBTR7GIAZJWVUUK6VETRCYJRNA5CNFSM4KVAEJM2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4INOSTJQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAICUBV5HWTFHCMPGMDFF53RCYJRNANCNFSM4KVAEJMQ .

seisowl commented 4 years ago
#include <iostream>
#include <yaml-cpp/yaml.h>
using YAML::Node;

int main(int argc, char** argv) {
  Node yml;

  yml["a"] = 1;

  std::cout << "a:" << yml["a"].as<int>() << std::endl;

  float c = yml["b"]["c"].as<float>(); // move this up or down
  float b = yml["b"].as<float>();       // move this up or down

  std::cout << "b:" << b << " b.c:" << c << std::endl;
  return 0;
}

Actual output:

a:1 terminate called after throwing an instance of 'YAML::TypedBadConversion' what(): bad conversion Abort

Expected (quote from the git log):

For example:

a:
  x: 1
  y: 2

node["a"]["z"].as<int>()

will say that the key "z" was invalid.
vmatare commented 4 years ago

The expected output makes no sense to me. Why would it say anything about an a/x and a/y node? What does it have to do with the code above? @seisowl if you want help then don't turn your issues into riddles. Am I supposed to guess what you mean?

teyrana commented 4 years ago

I'm seeing this same behavior, as well.

Demonstrated "Bug" Behavior:

The line:

  float b = yml["b"].as<float>();       // move this up or down

from @seisowl 's example above is demonstrating: "what happens if my code tries to access a non-existent field".

The answer, as run, is that it segfaults. @jbeder Is intended behavior?

Resolution / Workaround

    if(yml["b"]){

does behave as the tutorial implies, however. And this is, a function workaround. I've this is what I'm doing in my code.

Ideas for fixes:

  1. Leave as is, and update docs
  2. operator[] should throws exception instead of segfault'ing
  3. add function for get-or-default:
    T  Node::get( string key, T default_value);
vmatare commented 4 years ago
  float b = yml["b"].as<float>();       // move this up or down

The answer, as run, is that it segfaults.

Wait now. @seisowl says that it throws an exception. If it segfaults for you that is definitely a bug. If it throws an exception that's obviously the intended behaviour. What else should it do?

teyrana commented 4 years ago

okay, after taking a closer look, it appears this is a separate issue.