tiny realloc function wrapper which handles cases of realloc failures

Memory reallocation with old data pointer handling is done by means
of realloc_it() function.

Tiny inline function realloc_it() is a wrapper function for standart
realloc() which frees old memory pointer and prints errno to stderr
if realloc fails.
master
goriy 9 years ago
parent e709651a20
commit 2d185aa465

@ -4,6 +4,21 @@
#include <errno.h> #include <errno.h>
#include "../jsmn.h" #include "../jsmn.h"
/* Function realloc_it() is a wrapper function for standart realloc()
* with one difference - it frees old memory pointer in case of realloc
* failure. Thus, DO NOT use old data pointer in anyway after call to
* realloc_it(). If your code has some kind of fallback algorithm if
* memory can't be re-allocated - use standart realloc() instead.
*/
static inline void *realloc_it(void *ptrmem, size_t size) {
void *p = realloc(ptrmem, size);
if (!p) {
free (ptrmem);
fprintf(stderr, "realloc(): errno=%d\n", errno);
}
return p;
}
/* /*
* An example of reading JSON from stdin and printing its content to stdout. * An example of reading JSON from stdin and printing its content to stdout.
* The output looks like YAML, but I'm not sure if it's really compatible. * The output looks like YAML, but I'm not sure if it's really compatible.
@ -48,7 +63,7 @@ static int dump(const char *js, jsmntok_t *t, size_t count, int indent) {
int main() { int main() {
int r; int r;
int eof_expected = 0; int eof_expected = 0;
char *tmp, *js = NULL; char *js = NULL;
size_t jslen = 0; size_t jslen = 0;
char buf[BUFSIZ]; char buf[BUFSIZ];
@ -82,13 +97,10 @@ int main() {
} }
} }
tmp = realloc(js, jslen + r + 1); js = realloc_it(js, jslen + r + 1);
if (tmp == NULL) { if (js == NULL) {
free (js);
fprintf(stderr, "realloc(): errno=%d\n", errno);
return 3; return 3;
} }
js = tmp;
strncpy(js + jslen, buf, r); strncpy(js + jslen, buf, r);
jslen = jslen + r; jslen = jslen + r;
@ -96,16 +108,11 @@ again:
r = jsmn_parse(&p, js, jslen, tok, tokcount); r = jsmn_parse(&p, js, jslen, tok, tokcount);
if (r < 0) { if (r < 0) {
if (r == JSMN_ERROR_NOMEM) { if (r == JSMN_ERROR_NOMEM) {
jsmntok_t *tmptok;
tokcount = tokcount * 2; tokcount = tokcount * 2;
tmptok = realloc(tok, sizeof(*tok) * tokcount); tok = realloc_it(tok, sizeof(*tok) * tokcount);
if (tmptok == NULL) { if (tok == NULL) {
free (tok);
fprintf(stderr, "realloc(): errno=%d\n", errno);
return 3; return 3;
} }
tok = tmptok;
goto again; goto again;
} }
} else { } else {

Loading…
Cancel
Save