libjson: dynamically allocate buffer

This commit is contained in:
BurnZeZ 2013-10-28 23:21:07 -04:00
parent 82f4c1c0b4
commit a8e8b650f3

View file

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