Skip to content

Commit

Permalink
Fix issues accessing object/array fields when the field itself has a …
Browse files Browse the repository at this point in the history
…null character in it (fix #2176)
  • Loading branch information
gfwilliams committed Mar 31, 2022
1 parent c8b00c6 commit 3cc43cd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -47,6 +47,7 @@
Storage: Now put all .js files into internal storage (if 2 storage areas). Fix corruption issue when reading using file with explicit drive letter
Bangle.js: default 'locale' now supports second 'dp' argument for decimal places in distance/speed/temp
Graphics: Restrict setClipRect coordinates (Except on ST7789_8BIT (Bangle.js 1) where we need it for notifications. This stops potential out of bounds writes is setClipRect is wrong.
Fix issues accessing object/array fields when the field itself has a null character in it (fix #2176)

2v12 : nRF52840: Flow control XOFF is now sent at only 3/8th full - delays in BLE mean we can sometimes fill our 1k input buffer otherwise
__FILE__ is now set correctly for apps (fixes 2v11 regression)
Expand Down
3 changes: 1 addition & 2 deletions src/jslex.c
Expand Up @@ -932,8 +932,7 @@ JsVar *jslGetTokenValueAsVar() {
return jsvNewFromString(jslReservedWordAsString(lex->tk));
} else {
assert(lex->tokenl < JSLEX_MAX_TOKEN_LENGTH);
lex->token[lex->tokenl] = 0; // add final null
return jsvNewFromString(lex->token);
return jsvNewStringOfLength(lex->tokenl, lex->token);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/jsvar.c
Expand Up @@ -1210,18 +1210,21 @@ bool jsvIsBasicVarEqual(JsVar *a, JsVar *b) {
}
}
} else if (jsvIsString(a) && jsvIsString(b)) {
// OPT: could we do a fast check here with data?
JsvStringIterator ita, itb;
jsvStringIteratorNew(&ita, a, 0);
jsvStringIteratorNew(&itb, b, 0);
while (true) {
char a = jsvStringIteratorGetCharAndNext(&ita);
char b = jsvStringIteratorGetCharAndNext(&itb);
int a = jsvStringIteratorGetCharOrMinusOne(&ita);
jsvStringIteratorNext(&ita);
int b = jsvStringIteratorGetCharOrMinusOne(&itb);
jsvStringIteratorNext(&itb);
if (a != b) {
jsvStringIteratorFree(&ita);
jsvStringIteratorFree(&itb);
return false;
}
if (!a) { // equal, but end of string
if (a < 0) { // equal, but end of string
jsvStringIteratorFree(&ita);
jsvStringIteratorFree(&itb);
return true;
Expand Down
8 changes: 8 additions & 0 deletions tests/test_object_zero_in_keys.js
@@ -0,0 +1,8 @@
// https://github.com/espruino/Espruino/issues/2176

a = { "" : {}, " \x00hello" : "world" }
a["\x00abc"] = "foo"
a[" \x00abc"] = "bar"

json = JSON.stringify(a)
result = json == "{\"\":{},\" \\u0000hello\":\"world\",\"\\u0000abc\":\"foo\",\" \\u0000abc\":\"bar\"}";

0 comments on commit 3cc43cd

Please sign in to comment.