libjson: fix memory leak setjmp/longjmp problem (thanks spew)

spew → I fixed the memory leak setjmp/longjmp problem with libjson
spew → http://www.spew.club/json.patch
spew → full file: http://www.spew.club/json.c
spew → going to bed, I'll annoy cinap_lenrek tomorrow to try to get this committed
This commit is contained in:
cinap_lenrek 2015-12-22 17:00:00 +01:00
parent 9b0de7f9d6
commit f6e8b115d4

View file

@ -22,8 +22,6 @@ struct Lex
double n; double n;
char *buf; char *buf;
Rune peeked; Rune peeked;
jmp_buf jmp;
int canjmp;
}; };
static Rune static Rune
@ -50,6 +48,20 @@ peekch(Lex *l)
return l->peeked; return l->peeked;
} }
static Rune
peeknonspace(Lex *l)
{
Rune r;
for(;;){
r = peekch(l);
if(r != 0x20 && r != 0x09 && r != 0x0A && r != 0x0D)
break;
getch(l);
}
return r;
}
static int static int
fixsurrogate(Rune *rp, Rune r2) fixsurrogate(Rune *rp, Rune r2)
{ {
@ -81,16 +93,8 @@ lex(Lex *l)
int i; int i;
char c; char c;
for(;;){ peeknonspace(l);
r = peekch(l);
if(r != 0x20 && r != 0x09 && r != 0x0A && r != 0x0D)
break;
getch(l);
}
r = getch(l); r = getch(l);
if(r == ']' && l->canjmp)
longjmp(l->jmp, 1);
l->canjmp = 0;
if(r == 0 || r == '{' || r == '[' || r == ']' || r == '}' || r == ':' || r == ','){ if(r == 0 || r == '{' || r == '[' || r == ']' || r == '}' || r == ':' || r == ','){
l->t = r; l->t = r;
return 0; return 0;
@ -241,7 +245,6 @@ error:
case '[': case '[':
obj = l->t == '{'; obj = l->t == '{';
ln = &j->first; ln = &j->first;
e = nil;
if(obj){ if(obj){
j->t = JSONObject; j->t = JSONObject;
if(lex(l) < 0) if(lex(l) < 0)
@ -251,9 +254,8 @@ error:
goto firstobj; goto firstobj;
}else{ }else{
j->t = JSONArray; j->t = JSONArray;
l->canjmp = 1; if(peeknonspace(l) == ']'){
if(setjmp(l->jmp) > 0){ getch(l);
free(e);
return j; return j;
} }
} }