Skip to content
Myr

implode: Better invalid input validation and handling

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Error on non-number and nan codepoint, would asserd before Replace negative codepoint and surrogate range with unicode replacement character, would assert before Fixes #1160

authored by Mattias Wadman · committed by Nico Williams · parent a29ac81 · Browse files · 5 files+36 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+36 added−3 removed

  • tests:+11 added−0 removed
  • other:+25 added−3 removed

5 files+36 added−3 removed

modified:src/builtin.c+22 added-1 removed
1199 unmodified lines
1200static jv f_string_implode(jq_state *jq, jv a) {1201 if (jv_get_kind(a) != JV_KIND_ARRAY) {1202 return ret_error(a, jv_string("implode input must be an array"));1203 }1204 return jv_string_implode(a);12041205 int len = jv_array_length(jv_copy(a));1206 jv s = jv_string_empty(len);12071208 for (int i = 0; i < len; i++) {1209 jv n = jv_array_get(jv_copy(a), i);1210 if (jv_get_kind(n) != JV_KIND_NUMBER || jvp_number_is_nan(n)) {1211 jv_free(a);1212 jv_free(s);1213 return type_error(n, "can't be imploded, unicode codepoint needs to be numeric");1214 }12151216 int nv = jv_number_value(n);1217 jv_free(n);1218 // outside codepoint range or in utf16 surrogate pair range1219 if (nv < 0 || nv > 0x10FFFF || (nv >= 0xD800 && nv <= 0xDFFF))1220 nv = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER1221 s = jv_string_append_codepoint(s, nv);1222 }12231224 jv_free(a);1225 return s;1226}12271228static jv f_setpath(jq_state *jq, jv a, jv b, jv c) { return jv_setpath(a, b, c); }1229extern jv _jq_path_append(jq_state *, jv, jv, jv);
671 unmodified lines
modified:src/jv_type_private.h-1 removed
1#ifndef JV_TYPE_PRIVATE2#define JV_TYPE_PRIVATE34int jvp_number_cmp(jv, jv);5int jvp_number_is_nan(jv);56#endif //JV_TYPE_PRIVATE
modified:src/jv.c+2 added-1 removed
1366 unmodified lines
1367 jv n = jv_array_get(jv_copy(j), i);1368 assert(JVP_HAS_KIND(n, JV_KIND_NUMBER));1369 int nv = jv_number_value(n);1370 jv_free(n);1371 if (nv > 0x10FFFF)1371 // outside codepoint range or in utf16 surrogate pair range1372 if (nv < 0 || nv > 0x10FFFF || (nv >= 0xD800 && nv <= 0xDFFF))1373 nv = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER1374 s = jv_string_append_codepoint(s, nv);1375 }1376
622 unmodified lines
modified:src/jv.h+1 added
61 unmodified lines
62jv jv_number(double);63jv jv_number_with_literal(const char*);64double jv_number_value(jv);65int jv_is_integer(jv);66int jvp_number_is_nan(jv);6768int jv_number_has_literal(jv n);69const char* jv_number_get_literal(jv);70
209 unmodified lines
modified:tests/jq.test+11 added
1912 unmodified lines
1913any(keys[]|tostring?;true)1914{"a":"1","b":"2","c":"3"}1915true191619171918# explode/implode1919# test replacement character (65533) for outside codepoint range and 0xd800 (55296) - 0xdfff (57343) utf16 surrogate pair range1920# 1.1 and 1.9 to test round down of non-ints1921implode|explode1922[-1,0,1,2,3,1114111,1114112,55295,55296,57343,57344,1.1,1.9]1923[65533,0,1,2,3,1114111,65533,55295,65533,65533,57344,1,1]19241925map(try implode catch .)1926[123,["a"],[nan]]1927["implode input must be an array","string (\"a\") can't be imploded, unicode codepoint needs to be numeric","number (null) can't be imploded, unicode codepoint needs to be numeric"]