aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2020-02-24 18:45:29 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2020-02-24 18:47:52 +0200
commita8d1e087fb0c7d378b0104810ecb4b7ab97a1329 (patch)
tree84850268d5e4f23bc5c84ad41e710cd4de3c43a2
parent80049026737f46a71bebd4aeebbf7fef3ab312ec (diff)
downloadping903-a8d1e087fb0c7d378b0104810ecb4b7ab97a1329.tar.gz
ping903-a8d1e087fb0c7d378b0104810ecb4b7ab97a1329.tar.bz2
Minor fixes in json parser.
* src/json.c (json_strerror): New function. (json_parse_string): Fix coredump on invalid objects. * src/json.h (json_strerror): New proto. * src/ping903q.c (json_parse_string): Remove.
-rw-r--r--src/json.c20
-rw-r--r--src/json.h2
-rw-r--r--src/ping903q.c15
3 files changed, 22 insertions, 15 deletions
diff --git a/src/json.c b/src/json.c
index b7ad44a..d65eb2c 100644
--- a/src/json.c
+++ b/src/json.c
@@ -42,6 +42,21 @@ json_2nrealloc(void *p, size_t *pn, size_t s)
return newp;
}
+char const *
+json_strerror(int ec)
+{
+ static char *json_errstr[] = {
+ [JSON_E_NOERR] = "No error",
+ [JSON_E_NOMEM] = "Not enough memory",
+ [JSON_E_BADTOK] = "Unrecognized token",
+ [JSON_E_BADDELIM] = "Bad delimiter",
+ [JSON_E_BADSTRING] = "Malformed string"
+ };
+ if (ec < 0 || ec >= sizeof(json_errstr) / sizeof(json_errstr[0]))
+ return "Unknown error";
+ return json_errstr[ec];
+}
+
static void
json_format_writez(struct json_format *fmt, char const *str)
{
@@ -966,6 +981,11 @@ json_parse_string(char const *input, struct json_value **retval, char **endp)
break;
case json_object:
+ if (!ctx->key) {
+ json_value_free(val);
+ ecode = JSON_E_BADTOK;
+ goto err;
+ }
rc = json_object_set(ctx->obj,
ctx->key, val);
free(ctx->key);
diff --git a/src/json.h b/src/json.h
index f5ca92c..81f6db8 100644
--- a/src/json.h
+++ b/src/json.h
@@ -53,6 +53,8 @@ int json_value_format(struct json_value *obj, struct json_format *fmt,
void json_value_free(struct json_value *);
int json_value_copy(struct json_value *val, struct json_value **new_val);
+char const *json_strerror(int ec);
+
void *json_2nrealloc(void *p, size_t *pn, size_t s);
struct json_value *json_new_null(void);
diff --git a/src/ping903q.c b/src/ping903q.c
index 20c0ebc..e1faacd 100644
--- a/src/ping903q.c
+++ b/src/ping903q.c
@@ -380,21 +380,6 @@ http_query(char const *meth, char const *url, char **hdr)
fprintf(http, "\r\n");
}
-static char const *
-json_strerror(int ec)
-{
- static char *json_errstr[] = {
- [JSON_E_NOERR] = "No error",
- [JSON_E_NOMEM] = "Not enough memory",
- [JSON_E_BADTOK] = "Unrecognized token",
- [JSON_E_BADDELIM] = "Bad delimiter",
- [JSON_E_BADSTRING] = "Malformed string"
- };
- if (ec < 0 || ec >= sizeof(json_errstr) / sizeof(json_errstr[0]))
- return "Unknown error";
- return json_errstr[ec];
-}
-
static struct json_value *
ejson_get(struct json_value *obj, char *name, int type)
{

Return to:

Send suggestions and report system problems to the System administrator.