yjson: debug error format object, add check NULL before free

This commit is contained in:
2025-10-18 00:30:56 +02:00
parent c0e610641f
commit fe3a0cf11e
2 changed files with 38 additions and 3 deletions
+26 -3
View File
@@ -499,10 +499,25 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
// key section // key section
// js->type.object.key = create_js_value_string(cur, js); // js->type.object.key = create_js_value_string(cur, js);
if(*cur == '"') ++cur; if(*cur == '"'){
++cur;
}else{// no key ! problem format
printf("debug: format json error, no string with \" symbole as key\n");
free(js->type.object.iter);
free(js);
return NULL;
}
char *key = cur; char *key = cur;
// /1/js->type.object.key = cur; // /1/js->type.object.key = cur;
for(; *cur != '"'; ++cur); for(; *cur && *cur != '"'; ++cur);
if(*cur=='\0'){// no \" ! problem format
printf("debug: format json error: no \" symbole after key!\n");
free(js->type.object.iter);
free(js);
return NULL;
}
//js->key = cur; //js->key = cur;
//for(; *cur != '"'; ++cur); //for(; *cur != '"'; ++cur);
//js->key_length = cur - js->key; //js->key_length = cur - js->key;
@@ -513,7 +528,15 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
*(js->type.object.key + js->type.object.key_length) ='\0'; *(js->type.object.key + js->type.object.key_length) ='\0';
//js->type.object.key = cur; //js->type.object.key = cur;
for(; *cur != ':'; ++cur); for(; *cur && *cur != ':'; ++cur);
if(*cur=='\0'){// no \" ! problem format
printf("debug: format json error, no : after key\n");
free(js->type.object.key);
free(js->type.object.iter);
free(js);
return NULL;
}
++cur; ++cur;
for(; is_js_space(*cur); ++cur); for(; is_js_space(*cur); ++cur);
// printf("cur=%s| lenKey :%ld \n",cur,js->type.object.key_length); // printf("cur=%s| lenKey :%ld \n",cur,js->type.object.key_length);
+12
View File
@@ -500,6 +500,18 @@ printf("\n##############################################\n");
} }
TEST(errorFormat){
char *val1="{ \"hey\" }";
struct js_value *js = create_js_value(val1, NULL);
EXPECT_EQ(js==NULL,1);
char *val2="{ \"hey }";
js = create_js_value(val2, NULL);
EXPECT_EQ(js==NULL,1);
char *val3="{ 123 }";
js = create_js_value(val3, NULL);
EXPECT_EQ(js==NULL,1);
}
int main(int argc, char **argv){ int main(int argc, char **argv){