mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-17 19:30:36 +00:00
Fix #3545
This commit is contained in:
1 parent
04ca25e4d8
commit
b773e33f4e
3 files changed
+15
-5
No files matched your search
+2
-2
@@ -3969,9 +3969,9 @@ size_t mg_json_unescape(struct mg_str json, const char *path, char *to,
|
|||||||
char *mg_json_get_str(struct mg_str json, const char *path) {
|
char *mg_json_get_str(struct mg_str json, const char *path) {
|
||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
int len = 0, off = mg_json_get(json, path, &len);
|
int len = 0, off = mg_json_get(json, path, &len);
|
||||||
if (off >= 0 && len > 2 && json.buf[off] == '"') {
|
if (off >= 0 && len >= 2 && json.buf[off] == '"') {
|
||||||
if ((result = (char *) mg_calloc(1, (size_t) len)) != NULL &&
|
if ((result = (char *) mg_calloc(1, (size_t) len)) != NULL &&
|
||||||
mg_json_unescape(json, path, result, (size_t) len) == 0) {
|
len > 2 && mg_json_unescape(json, path, result, (size_t) len) == 0) {
|
||||||
mg_free(result);
|
mg_free(result);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -338,9 +338,9 @@ size_t mg_json_unescape(struct mg_str json, const char *path, char *to,
|
|||||||
char *mg_json_get_str(struct mg_str json, const char *path) {
|
char *mg_json_get_str(struct mg_str json, const char *path) {
|
||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
int len = 0, off = mg_json_get(json, path, &len);
|
int len = 0, off = mg_json_get(json, path, &len);
|
||||||
if (off >= 0 && len > 2 && json.buf[off] == '"') {
|
if (off >= 0 && len >= 2 && json.buf[off] == '"') {
|
||||||
if ((result = (char *) mg_calloc(1, (size_t) len)) != NULL &&
|
if ((result = (char *) mg_calloc(1, (size_t) len)) != NULL &&
|
||||||
mg_json_unescape(json, path, result, (size_t) len) == 0) {
|
len > 2 && mg_json_unescape(json, path, result, (size_t) len) == 0) {
|
||||||
mg_free(result);
|
mg_free(result);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-1
@@ -3565,13 +3565,23 @@ static void test_json(void) {
|
|||||||
int len;
|
int len;
|
||||||
char *str = NULL;
|
char *str = NULL;
|
||||||
|
|
||||||
json = mg_str("{\"a\":\"b\"}");
|
json = mg_str("{\"a\":\"b\", \"c\":\"\"}");
|
||||||
str = mg_json_get_str(json, "$.a");
|
str = mg_json_get_str(json, "$.a");
|
||||||
ASSERT(str != NULL);
|
ASSERT(str != NULL);
|
||||||
// printf("---> [%s]\n", str);
|
// printf("---> [%s]\n", str);
|
||||||
ASSERT(strcmp(str, "b") == 0);
|
ASSERT(strcmp(str, "b") == 0);
|
||||||
mg_free(str);
|
mg_free(str);
|
||||||
|
|
||||||
|
// Non existent string
|
||||||
|
str = mg_json_get_str(json, "$.d");
|
||||||
|
ASSERT(str == NULL);
|
||||||
|
|
||||||
|
// Empty string
|
||||||
|
str = mg_json_get_str(json, "$.c");
|
||||||
|
ASSERT(str != NULL);
|
||||||
|
ASSERT(strcmp(str, "") == 0);
|
||||||
|
mg_free(str);
|
||||||
|
|
||||||
json = mg_str("{\"a\": \"hi\\nthere\",\"b\": [12345, true]}");
|
json = mg_str("{\"a\": \"hi\\nthere\",\"b\": [12345, true]}");
|
||||||
str = mg_json_get_str(json, "$.a");
|
str = mg_json_get_str(json, "$.a");
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user