From 2ecfd1b351409c5c0932ee550745e58c2e42e708 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 5 Apr 2022 17:51:26 +0300 Subject: [PATCH] ci: fix clang sanitize errors for json_decode --- thirdparty/cJSON/cJSON.c | 5 +++++ thirdparty/cJSON/cJSON.h | 1 + vlib/v/gen/c/json.v | 40 +++++++++++++++++++++------------------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/thirdparty/cJSON/cJSON.c b/thirdparty/cJSON/cJSON.c index 60b72c018..a491ebc48 100644 --- a/thirdparty/cJSON/cJSON.c +++ b/thirdparty/cJSON/cJSON.c @@ -74,6 +74,11 @@ typedef struct { } error; static error global_error = { NULL, 0 }; +CJSON_PUBLIC(size_t) cJSON_GetErrorPos(void) +{ + return global_error.position; +} + CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) { return (const char*) (global_error.json + global_error.position); diff --git a/thirdparty/cJSON/cJSON.h b/thirdparty/cJSON/cJSON.h index 592986b86..7325abfed 100644 --- a/thirdparty/cJSON/cJSON.h +++ b/thirdparty/cJSON/cJSON.h @@ -172,6 +172,7 @@ CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const objec CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); +CJSON_PUBLIC(size_t) cJSON_GetErrorPos(void); /* Check if the item is a string and return its valuestring */ CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item); diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 0a1511f6e..7a59a74e1 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -73,27 +73,29 @@ $dec_fn_dec { if (!root) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { - char *prevline_ptr = (char*)error_ptr; + const size_t error_pos = cJSON_GetErrorPos(); int maxcontext_chars = 30; - int backlines = 1; - int backchars = maxcontext_chars-7; - while(backchars--){ - char prevc = *(prevline_ptr - 1); - if(0==prevc){ - break; - } - if(10==prevc && !backlines--){ - break; - } - prevline_ptr--; - if(123==prevc) { - break; // stop at `{` too - } + byte *buf = vcalloc_noscan(maxcontext_chars + 10); + if(error_pos > 0) { + int backlines = 1; + int backchars = error_pos < maxcontext_chars-7 ? (int)error_pos : maxcontext_chars-7 ; + char *prevline_ptr = (char*)error_ptr; + while(backchars--){ + char prevc = *(prevline_ptr - 1); + if(0==prevc){ + break; + } + if(10==prevc && !backlines--){ + break; + } + prevline_ptr--; + if(123==prevc) { + break; // stop at `{` too + } + } + int maxchars = vstrlen_char(prevline_ptr); + vmemcpy(buf, prevline_ptr, (maxchars < maxcontext_chars ? maxchars : maxcontext_chars)); } - byte *buf = _v_malloc(maxcontext_chars + 10); - vmemset(buf, 0, maxcontext_chars+10); - vmemcpy(buf, prevline_ptr, maxcontext_chars); - // for(int x=-10;x<10;x++){ char *xx = prevline_ptr+x; fprintf(stderr, "2 prevline_ptr + %d: %p | %c | %d \\n", x, xx, (int)(*(xx)), (int)(*(xx))); } fprintf(stderr, "--------\\n"); return (Option_$styp){.state = 2,.err = _v_error(tos2(buf)),.data = {0}}; } } -- 2.30.2