Skip to content

Commit 0cd4ab2

Browse files
mattmundellbjoernricks
authored andcommitted
Add: gvm_json_obj_check_int
There are only two cases that use this variant but there will be a similar function for strings which will have more cases.
1 parent ae8e790 commit 0cd4ab2

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

openvasd/openvasd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,12 +1257,12 @@ openvasd_get_scan_status (openvasd_connector_t conn)
12571257
static int
12581258
get_member_value_or_fail (cJSON *reader, const gchar *member)
12591259
{
1260-
cJSON *item = NULL;
1261-
if ((item = cJSON_GetObjectItem (reader, member)) == NULL
1262-
&& cJSON_IsNumber (item))
1260+
int ret;
1261+
1262+
if (gvm_json_obj_check_int (reader, member, &ret))
12631263
return -1;
12641264

1265-
return item->valueint;
1265+
return ret;
12661266
}
12671267

12681268
static int

openvasd/vtparser.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,11 @@ add_preferences_to_nvt (nvti_t *nvt, cJSON *vt_obj)
246246
}
247247
class = prefs_item->valuestring;
248248

249-
if ((prefs_item = cJSON_GetObjectItem (prefs_obj, "id")) == NULL
250-
|| !cJSON_IsNumber (prefs_item))
249+
if (gvm_json_obj_check_int (prefs_obj, "id", &id))
251250
{
252251
g_warning ("%s: PREF missing id attribute", __func__);
253252
continue;
254253
}
255-
id = prefs_item->valueint;
256254

257255
if ((prefs_item = cJSON_GetObjectItem (prefs_obj, "name")) == NULL
258256
|| !cJSON_IsString (prefs_item))

util/json.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ gvm_json_obj_double (cJSON *obj, const gchar *key)
8383
return 0;
8484
}
8585

86+
/**
87+
* @brief Get an int field from a JSON object.
88+
*
89+
* @param[in] obj Object
90+
* @param[in] key Field name.
91+
* @param[out] val Return location for int if int exists.
92+
*
93+
* @return 0 if such an int field exists, else 1.
94+
*/
95+
int
96+
gvm_json_obj_check_int (cJSON *obj, const gchar *key, int *val)
97+
{
98+
cJSON *item;
99+
100+
item = cJSON_GetObjectItem (obj, key);
101+
if (item && cJSON_IsNumber (item)) {
102+
if (val)
103+
*val = item->valueint;
104+
return 0;
105+
}
106+
return 1;
107+
}
108+
86109
/**
87110
* @brief Get an int field from a JSON object.
88111
*

util/json.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ gvm_json_string_escape (const char *, gboolean);
1717
double
1818
gvm_json_obj_double (cJSON *, const gchar *);
1919

20+
int
21+
gvm_json_obj_check_int (cJSON *, const gchar *, int *);
22+
2023
int
2124
gvm_json_obj_int (cJSON *, const gchar *);
2225

0 commit comments

Comments
 (0)