libjson: fix missing buffer free, slight cleanup

This commit is contained in:
BurnZeZ 2013-10-28 03:17:53 -04:00
parent a8e8b650f3
commit 96c1e08f48

View file

@ -203,13 +203,9 @@ jsonobj(Lex *l)
JSONEl **ln; JSONEl **ln;
int obj; int obj;
l->buf = mallocz(l->slen, 1); if((j = mallocz(sizeof(*j), 1)) == nil)
if(l->buf == nil)
return nil; return nil;
j = mallocz(sizeof(*j), 1);
if(j == nil)
return nil;
if(lex(l) < 0){ if(lex(l) < 0){
error: error:
free(j); free(j);
@ -232,8 +228,7 @@ error:
break; break;
case TSTRING: case TSTRING:
j->t = JSONString; j->t = JSONString;
j->s = strdup(l->buf); if((j->s = strdup(l->buf)) == nil)
if(j->s == nil)
goto error; goto error;
break; break;
case TNUM: case TNUM:
@ -269,8 +264,7 @@ error:
werrstr("json: syntax error, not string"); werrstr("json: syntax error, not string");
goto abort; goto abort;
} }
e = mallocz(sizeof(*e), 1); if((e = mallocz(sizeof(*e), 1)) == nil)
if(e == nil)
goto abort; goto abort;
e->name = strdup(l->buf); e->name = strdup(l->buf);
if(e->name == nil || lex(l) < 0){ if(e->name == nil || lex(l) < 0){
@ -283,8 +277,7 @@ error:
goto abort; goto abort;
} }
}else{ }else{
e = mallocz(sizeof(*e), 1); if((e = mallocz(sizeof(*e), 1)) == nil)
if(e == nil)
goto abort; goto abort;
} }
e->val = jsonobj(l); e->val = jsonobj(l);
@ -320,12 +313,18 @@ error:
JSON* JSON*
jsonparse(char *s) jsonparse(char *s)
{ {
JSON *j;
Lex l; Lex l;
memset(&l, 0, sizeof(l)); memset(&l, 0, sizeof(l));
l.s = s; l.s = s;
l.slen = strlen(s)+1; l.slen = strlen(s)+1;
return jsonobj(&l); if((l.buf = mallocz(l.slen, 1)) == nil)
return nil;
j = jsonobj(&l);
free(l.buf);
return j;
} }
void void