DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.64k stars 3.2k forks source link

Why need extra 5 bytes for buf? #255

Open ruozhiertong opened 6 years ago

ruozhiertong commented 6 years ago

in test.c line 56:/ the extra 5 bytes are because of inaccuracies when reserving memory / why the the extra 5 bytes ? It is like a magic number. what reason cause inaccuracies when reserving memory?

FSMaxB commented 6 years ago

Good question, I should have documented this better back when I did it. The commit that changed this comment was made in pull request #146, before that it was more than that, because back then it wasn't possible to know beforehand, how much space was needed for numbers. Now that numbers are first parsed into a buffer and then copied over, this problem doesn't exist anymore, therefore the change to 5.

But I have to investigate further, where the 5 comes from, I think it had something to do with UTF-16 \uXXXX notation or surrogate pairs.

FSMaxB commented 6 years ago

I currently can't find any reason why. Needs some more investigation, maybe I can drop these + 5, I think they could have been just "to be safe".

FSMaxB commented 6 years ago

It definitely was "just to be safe". I haven't had the time yet to review all the codepaths thoroughly enough to ensure that the required memory isn't still overestimated (by a smaller margin) somewhere.

huang4115 commented 6 years ago

I seem to have found the reason for the inaccurate memory estimate. Please take a look at #283