ci: fix clang sanitize errors for json_decode

pull/13953/head
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;
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);

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);
/* 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);

View File

@ -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}};
}
}