diff options
author | Joey Adams | 2011-03-28 05:28:36 +0000 |
---|---|---|
committer | Joey Adams | 2011-03-28 14:51:34 +0000 |
commit | 4a6312ceb561fba8902a28b29764a70276d1555c (patch) | |
tree | f58d9b31645619dca9266dd458deca53075d1f79 | |
parent | efe9481e0a005efae7ad54648af1327fdc09ad01 (diff) |
Added safeguards to prevent crashes if retrieved JSON is corrupted (which shouldn't happen).
-rw-r--r-- | json.c | 15 |
1 files changed, 9 insertions, 6 deletions
@@ -196,10 +196,6 @@ json_validate(const char *str, size_t length) * JSON data is condensed on input, meaning spaces around tokens are removed. * The fact that these spaces are gone is exploited in functions that * traverse and manipulate JSON. - * - * This function is intended primarily for debugging purposes, as in: - * - * Assert(json_validate_nospace(str, length)); */ bool json_validate_nospace(const char *str, size_t length) { @@ -417,7 +413,13 @@ json_escape_unicode(const char *json, size_t length, size_t *out_length) pg_wchar u; len = pg_utf_mblen((const unsigned char *) s); - Assert(s + len <= e); + if (s + len > e) + { + Assert(false); + appendStringInfoChar(&buf, *s); + s++; + continue; + } u = utf8_to_unicode((const unsigned char *) s); s += len; @@ -460,7 +462,8 @@ json_stringify(const char *json, size_t length, const char *e = json + length; StringInfoData buf; - Assert(json_validate_nospace(json, length)); + if (!json_validate_nospace(json, length)) + report_corrupt_json(); initStringInfo(&buf); s = stringify_value(&buf, s, e, space, space_length, 0); |