dvidelabs / flatcc

FlatBuffers Compiler and Library in C for C
Apache License 2.0
646 stars 182 forks source link

Buffer size is huge for 3 string table #133

Closed bturner1273 closed 4 years ago

bturner1273 commented 4 years ago

I have a table with 3 strings in it that are very short:

This is my schema: image

This is my code to serialize the "APNSettings" table: image

So testing this I load the settings.apn struct with

setting_t test;
uint8_t buffer[1024];
memcpy(test.apn.apn, "apn", 3);
memcpy(test.apn.username, "username", 8);
memcpy(test.apn.password, "password", 8);
size_t size = shadow_encode_apn_settings(test, buffer);

The size that is returned is equal to 3764: this seems way too large am I right?

mikkelfj commented 4 years ago

The size does sound excessive. That is likely the input data. You copy 8 bytes to a struct I don't know the format of, and you do not make sure the string is terminated. You can use create_str_n for a purpose.

The buffer is also returned incorrectly. You either need uint8_t **buffer and assign *buffer = finalize ... and free it later with a flacc call, or or you must use another finalize call that takes a buffer as argument and the maximum capacity.

bturner1273 commented 4 years ago

Thank you for the help! I replaced the create_str calls with create_strn and it made the size reasonable. Also fixed the buffer return!

mikkelfj commented 4 years ago

you are welcome!