google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.28k stars 3.25k forks source link

c++ generated code should use 'bool' instead of 'uint8_t' for bool fields #4078

Closed haiiro closed 7 years ago

haiiro commented 7 years ago

If I have the table

table Foo {
  boolean_value : bool;
}

Then I get C++ generated code including:

struct FooBuilder {
  [...]
  add_boolean_value(uint8_t boolean_value) { dbb_.AddElement<uint8_t> (###, boolean_value, 0); }
};

This allows for very easy mistakes like:

int x = 0xFFFFFFFF; // packed bits of information
#define BIT_FIELD 0x02000000 // some high-order bit that we want to check

FlatBufferBuilder fbb;
FooBuilder foo_builder {fbb};
foo_builder.add_boolean_value(x & BIT_FIELD); // Whoops, stores '0'

This would not be an issue if add_boolean_value took an argument of type bool, as the compiler would then cast any non-zero integer value to true.

ghost commented 7 years ago

This was already fixed a while ago. Which version are you using?

haiiro commented 7 years ago

Ah, whoops. Apparently 1.1.0. Sorry, thought my project had the latest version. Confirmed working with 1.4.0. Thanks, and sorry for the bother!