shithub: riscv

Download patch

ref: 96c1e08f481b67ea0e8694931cd7e46d339f5fc5
parent: a8e8b650f3643c1a9f434fb2efb4708ff53539cd
author: BurnZeZ <brz-9dev@intma.in>
date: Sun Oct 27 23:17:53 EDT 2013

libjson: fix missing buffer free, slight cleanup

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