shithub: riscv

Download patch

ref: a8e8b650f3643c1a9f434fb2efb4708ff53539cd
parent: 82f4c1c0b4160672467e5eceea4c6f7a48c9481c
author: BurnZeZ <brz-9dev@intma.in>
date: Mon Oct 28 19:21:07 EDT 2013

libjson: dynamically allocate buffer

--- a/sys/src/libjson/json.c
+++ b/sys/src/libjson/json.c
@@ -17,9 +17,10 @@
 struct Lex
 {
 	char *s;
+	ulong slen;
 	int t;
 	double n;
-	char buf[4096];
+	char *buf;
 	Rune peeked;
 	jmp_buf jmp;
 	int canjmp;
@@ -96,7 +97,7 @@
 		t = l->buf;
 		for(;;){
 			t += runetochar(t, &r);
-			if(t >= l->buf + sizeof(l->buf)){
+			if(t >= l->buf + l->slen){
 				werrstr("json: literal too long");
 				return -1;
 			}
@@ -181,7 +182,7 @@
 			}
 			r2 = 0;
 			t += runetochar(t, &r);
-			if(t >= l->buf + sizeof(l->buf)){
+			if(t >= l->buf + l->slen){
 				werrstr("json: string too long");
 				return -1;
 			}
@@ -201,7 +202,11 @@
 	JSONEl *e;
 	JSONEl **ln;
 	int obj;
-	
+
+	l->buf = mallocz(l->slen, 1);
+	if(l->buf == nil)
+		return nil;
+
 	j = mallocz(sizeof(*j), 1);
 	if(j == nil)
 		return nil;
@@ -319,6 +324,7 @@
 
 	memset(&l, 0, sizeof(l));
 	l.s = s;
+	l.slen = strlen(s)+1;
 	return jsonobj(&l);
 }
 
--