jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
5.09k stars 1.83k forks source link

Can't create a map with first key floating-point 0 #1167

Open ephere opened 1 year ago

ephere commented 1 year ago

Here's a failing test:

TEST(NodeTest, CreateMapWithFloatingPoint0Key) {
  Node node;
  node[0.0] = 1.0;
  EXPECT_TRUE(node.IsMap());
}

It doesn't work because the specialization of get_idx in node/detail/impl.h is enabled on std::is_signed condition, which returns true for floating-point types, while the Key template parameter of get_idx is expected to be integral. The result is that get_idx succeeds and the node is converted to a sequence instead of a map, with the value added at index 0.

This can be fixed by adding && std::is_integral<Key>::value to the std::enable_if<std::is_signed<Key>::value> specialization of get_idx in node/detail/impl.h. I suppose the same same has to be done for the specialization of remove_idx in the same file.

ikolev21 commented 1 year ago

Sorry, I incorrectly posted this from my company's account.

jbeder commented 1 year ago

Agreed, this is a bug, thank you! Feel free to submit a PR with that fix.

ikolev21 commented 7 months ago

Sorry, I haven't used GitHub for development yet, it will take time until I figure out how to submit PRs...