Skip to content

Commit 304af46

Browse files
committed
Bugfix: certain tokens mistaken for keywords
Ex: Formerly "taaaaaaaaanull" parsed as JSTR_NULL. Generalizing, any sequence matching [tfn]a{N}(true|false|null) was misinterpreted (N depends on the integer width).
1 parent a0a8dc5 commit 304af46

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

jstr-test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ int main() {
314314
test("fruit", JSTR_INVAL);
315315
test("nul", JSTR_INVAL);
316316
test("nullnullnull", JSTR_INVAL);
317+
test("taaaaaaaaaaaaaaaaaaaaaaaaaaaaatrue", JSTR_INVAL); // bug fixed
318+
test("taaaaaaaaaaaaaaaaaaaaaaaaaaaaanull", JSTR_INVAL); // bug fixed
317319

318320
return err ? EXIT_FAILURE : EXIT_SUCCESS;
319321
}

jstr.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,18 @@ parse_escape: {
166166
}
167167

168168
parse_true_false_or_null: {
169-
unsigned v = 0;
169+
#define _(a,b) ((a)<<5|((b)-'a'))
170+
static const unsigned kw[] = {
171+
[JSTR_TRUE>>5] = _(_(_(_(31,'t'),'r'),'u'),'e'),
172+
[JSTR_FALSE>>5] = _(_(_(_(_(31,'f'),'a'),'l'),'s'),'e'),
173+
[JSTR_NULL>>5] = _(_(_(_(31,'n'),'u'),'l'),'l')
174+
};
175+
unsigned v = 31;
170176
do {
171-
v = (v<<5)|(*p-'a');
177+
v = _(v, *p);
172178
} while (((unsigned)*++p-'a')<='z'-'a');
173-
if (v==640644 || v==5254724 || v==446827) goto commit_token;
179+
if (v==kw[token_cur[-1].type__>>5]) goto commit_token;
180+
#undef _
174181
return JSTR_INVAL;
175182
}
176183

0 commit comments

Comments
 (0)