Skip to content

Commit

Permalink
don't trip over unescaped UTF-8 keys
Browse files Browse the repository at this point in the history
Support UTF-8 in the spirit of jsmn being lean and mean. Don't do any validation
or decoding, just make sure unescaped UTF-8 keys are supported.
  • Loading branch information
timkuijsten committed Nov 4, 2022
1 parent 25647e6 commit e9553a0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions jsmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
const size_t num_tokens) {
jsmntok_t *token;
int start;
unsigned char b;

start = parser->pos;

for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
switch (js[parser->pos]) {
b = js[parser->pos];
switch (b) {
#ifndef JSMN_STRICT
/* In strict mode primitive must be followed by "," or "}" or "]" */
case ':':
Expand All @@ -158,10 +160,20 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
/* to quiet a warning from gcc*/
break;
}
if (js[parser->pos] < 32 || js[parser->pos] >= 127) {

if (b < 32) {
parser->pos = start;
return JSMN_ERROR_INVAL;
}

if (b >= 127 &&
(b & (0xc0 | 0x20)) != 0xc0 &&
(b & (0xe0 | 0x10)) != 0xe0 &&
(b & (0xf0 | 0x08)) != 0xf0 &&
(b & (0x80 | 0x40)) != 0x80) {
parser->pos = start;
return JSMN_ERROR_INVAL;
}
}
#ifdef JSMN_STRICT
/* In strict mode primitive must be followed by a comma/object/array */
Expand Down

0 comments on commit e9553a0

Please sign in to comment.