Skip to content

Commit e9553a0

Browse files
committed
don't trip over unescaped UTF-8 keys
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.
1 parent 25647e6 commit e9553a0

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

jsmn.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
137137
const size_t num_tokens) {
138138
jsmntok_t *token;
139139
int start;
140+
unsigned char b;
140141

141142
start = parser->pos;
142143

143144
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
144-
switch (js[parser->pos]) {
145+
b = js[parser->pos];
146+
switch (b) {
145147
#ifndef JSMN_STRICT
146148
/* In strict mode primitive must be followed by "," or "}" or "]" */
147149
case ':':
@@ -158,10 +160,20 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
158160
/* to quiet a warning from gcc*/
159161
break;
160162
}
161-
if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
163+
164+
if (b < 32) {
162165
parser->pos = start;
163166
return JSMN_ERROR_INVAL;
164167
}
168+
169+
if (b >= 127 &&
170+
(b & (0xc0 | 0x20)) != 0xc0 &&
171+
(b & (0xe0 | 0x10)) != 0xe0 &&
172+
(b & (0xf0 | 0x08)) != 0xf0 &&
173+
(b & (0x80 | 0x40)) != 0x80) {
174+
parser->pos = start;
175+
return JSMN_ERROR_INVAL;
176+
}
165177
}
166178
#ifdef JSMN_STRICT
167179
/* In strict mode primitive must be followed by a comma/object/array */

0 commit comments

Comments
 (0)