zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.68k stars 783 forks source link

small fix of jsondump example for cases of realloc failures #55

Closed goriy closed 9 years ago

goriy commented 9 years ago

When realloc() function fails it returns NULL pointer. But old data pointer remains valid in such a case.

It's a mistake to use old data pointer to store new pointer returned by realloc. In case of realloc failure, pointer is overwritten with NULL value, but old used memory remains unreferenced and could not be even freed anymore. Such mistakes could lead to memory leaks.

MacGritsch commented 9 years ago

if available you can use reallocf for that, if not you can create an inline function..

zserge commented 9 years ago

reallocf seems to be available on BSD systems only, I would not rely on that. But inline function wrapping realloc would be a nice improvement for this PR to avoid code duplication.

goriy commented 9 years ago

I agree that usage of non-standard reallocf function is not so good idea. It's not very likely that reallocf function is present in stdlib for most embedded platforms, for example. I've added tiny inline wrapper function.