1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ci: fix clang sanitize errors for json_decode

This commit is contained in:
Delyan Angelov 2022-04-05 17:51:26 +03:00
parent 11ccf06441
commit 2ecfd1b351
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 27 additions and 19 deletions

View File

@ -74,6 +74,11 @@ typedef struct {
} error; } error;
static error global_error = { NULL, 0 }; static error global_error = { NULL, 0 };
CJSON_PUBLIC(size_t) cJSON_GetErrorPos(void)
{
return global_error.position;
}
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
{ {
return (const char*) (global_error.json + global_error.position); return (const char*) (global_error.json + global_error.position);

View File

@ -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); 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. */ /* 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(const char *) cJSON_GetErrorPtr(void);
CJSON_PUBLIC(size_t) cJSON_GetErrorPos(void);
/* Check if the item is a string and return its valuestring */ /* Check if the item is a string and return its valuestring */
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item); CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);

View File

@ -73,10 +73,13 @@ $dec_fn_dec {
if (!root) { if (!root) {
const char *error_ptr = cJSON_GetErrorPtr(); const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) { if (error_ptr != NULL) {
char *prevline_ptr = (char*)error_ptr; const size_t error_pos = cJSON_GetErrorPos();
int maxcontext_chars = 30; int maxcontext_chars = 30;
byte *buf = vcalloc_noscan(maxcontext_chars + 10);
if(error_pos > 0) {
int backlines = 1; int backlines = 1;
int backchars = maxcontext_chars-7; int backchars = error_pos < maxcontext_chars-7 ? (int)error_pos : maxcontext_chars-7 ;
char *prevline_ptr = (char*)error_ptr;
while(backchars--){ while(backchars--){
char prevc = *(prevline_ptr - 1); char prevc = *(prevline_ptr - 1);
if(0==prevc){ if(0==prevc){
@ -90,10 +93,9 @@ $dec_fn_dec {
break; // stop at `{` too break; // stop at `{` too
} }
} }
byte *buf = _v_malloc(maxcontext_chars + 10); int maxchars = vstrlen_char(prevline_ptr);
vmemset(buf, 0, maxcontext_chars+10); vmemcpy(buf, prevline_ptr, (maxchars < maxcontext_chars ? maxchars : maxcontext_chars));
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}}; return (Option_$styp){.state = 2,.err = _v_error(tos2(buf)),.data = {0}};
} }
} }