summaryrefslogtreecommitdiff
path: root/ext/json/parser
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2025-02-25 17:12:26 +0100
committerHiroshi SHIBATA <[email protected]>2025-02-27 13:32:32 +0900
commit0d62037fc0626855c36359e4a8a02936b592f9d9 ()
tree1c79a61b620ea9e270d2903d4532616b1ba2d30e /ext/json/parser
parent75f07afd1897a81a18d3e40b57e8a5731d455827 (diff)
[ruby/json] Ensure parser error snippets are valid UTF-8
Fix: https://.com/ruby/json/issues/755 Error messages now include a snippet of the document that doesn't parse to help locate the issue, however the way it was done wasn't UTF-8 aware, and it could result in exception messages with truncated characters. It would be nice to go a bit farther and actually support codepoints, but it's a lot of complexity to do it in C, perhaps if we move that logic to Ruby given it's not a performance sensitive codepath. https://.com/ruby/json/commit/e144793b72
-rw-r--r--ext/json/parser/parser.c15
1 files changed, 12 insertions, 3 deletions
@@ -454,15 +454,24 @@ RBIMPL_ATTR_NORETURN()
#endif
static void raise_parse_error(const char *format, const char *start)
{
- char buffer[PARSE_ERROR_FRAGMENT_LEN + 1];
size_t len = start ? strnlen(start, PARSE_ERROR_FRAGMENT_LEN) : 0;
const char *ptr = start;
if (len == PARSE_ERROR_FRAGMENT_LEN) {
MEMCPY(buffer, start, char, PARSE_ERROR_FRAGMENT_LEN);
- buffer[PARSE_ERROR_FRAGMENT_LEN] = '\0';
- ptr = buffer;
}
rb_enc_raise(enc_utf8, rb_path2class("JSON::ParserError"), format, ptr);