From 506a608f59dfe16a5e568293583720ab5af3e261 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 1 May 2024 20:33:59 +1000 Subject: [PATCH] fix underflow issue --- DESCRIPTION | 2 +- NEWS.md | 5 +++++ src/R-yyjson-parse.c | 8 +++++--- .../testthat/test-bug44-infinite-loop-on-bad-string.R | 11 +++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-bug44-infinite-loop-on-bad-string.R diff --git a/DESCRIPTION b/DESCRIPTION index 6cffb6d..e48fbab 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: yyjsonr Type: Package Title: Fast 'JSON', 'NDJSON' and 'GeoJSON' Parser and Generator -Version: 0.1.20.9000 +Version: 0.1.20.9001 Authors@R: c( person("Mike", "Cheng", role = c("aut", "cre", 'cph'), email = "mikefc@coolbutuseless.com"), diff --git a/NEWS.md b/NEWS.md index c4a9f91..ce109d0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,9 @@ + +# yyjsonr 0.1.20.9001 2024-05-01 + +* Fix underflow issue when trying to verbosely report an error at position 0 + # yyjsonr 0.1.20.9000 2024-04-12 * Get size of gzipped file in an endian-neutral way. Issue #39 diff --git a/src/R-yyjson-parse.c b/src/R-yyjson-parse.c index e17e3e3..dda4392 100644 --- a/src/R-yyjson-parse.c +++ b/src/R-yyjson-parse.c @@ -1820,9 +1820,11 @@ void output_verbose_error(const char *str, yyjson_read_err err) { // Print a "^" to point to the error size_t pos = ERR_CONTEXT; - pos = err.pos < ERR_CONTEXT ? err.pos - 1 : ERR_CONTEXT; - for (unsigned int i = 0; i < pos; i++) { - Rprintf(" "); + if (err.pos > 0) { + pos = err.pos < ERR_CONTEXT ? err.pos - 1 : ERR_CONTEXT; + for (unsigned int i = 0; i < pos; i++) { + Rprintf(" "); + } } Rprintf("^\n"); } diff --git a/tests/testthat/test-bug44-infinite-loop-on-bad-string.R b/tests/testthat/test-bug44-infinite-loop-on-bad-string.R new file mode 100644 index 0000000..17fff79 --- /dev/null +++ b/tests/testthat/test-bug44-infinite-loop-on-bad-string.R @@ -0,0 +1,11 @@ + + +test_that("Invalid string input should cause error", { + + testthat::capture_output({ + expect_error({ + read_json_str("hello") + }) + }) + +})