Skip to content

Commit

Permalink
Add: gvm_json_obj_check_int
Browse files Browse the repository at this point in the history
There are only two cases that use this variant but there will be
a similar function for strings which will have more cases.
  • Loading branch information
mattmundell authored and bjoernricks committed Feb 6, 2025
1 parent ae8e790 commit 0cd4ab2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 4 additions & 4 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,12 @@ openvasd_get_scan_status (openvasd_connector_t conn)
static int
get_member_value_or_fail (cJSON *reader, const gchar *member)
{
cJSON *item = NULL;
if ((item = cJSON_GetObjectItem (reader, member)) == NULL
&& cJSON_IsNumber (item))
int ret;

if (gvm_json_obj_check_int (reader, member, &ret))
return -1;

return item->valueint;
return ret;
}

static int
Expand Down
4 changes: 1 addition & 3 deletions openvasd/vtparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,11 @@ add_preferences_to_nvt (nvti_t *nvt, cJSON *vt_obj)
}
class = prefs_item->valuestring;

if ((prefs_item = cJSON_GetObjectItem (prefs_obj, "id")) == NULL
|| !cJSON_IsNumber (prefs_item))
if (gvm_json_obj_check_int (prefs_obj, "id", &id))
{
g_warning ("%s: PREF missing id attribute", __func__);
continue;
}
id = prefs_item->valueint;

if ((prefs_item = cJSON_GetObjectItem (prefs_obj, "name")) == NULL
|| !cJSON_IsString (prefs_item))
Expand Down
23 changes: 23 additions & 0 deletions util/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ gvm_json_obj_double (cJSON *obj, const gchar *key)
return 0;
}

/**
* @brief Get an int field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
* @param[out] val Return location for int if int exists.
*
* @return 0 if such an int field exists, else 1.
*/
int
gvm_json_obj_check_int (cJSON *obj, const gchar *key, int *val)
{
cJSON *item;

item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsNumber (item)) {
if (val)
*val = item->valueint;
return 0;
}
return 1;
}

/**
* @brief Get an int field from a JSON object.
*
Expand Down
3 changes: 3 additions & 0 deletions util/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ gvm_json_string_escape (const char *, gboolean);
double
gvm_json_obj_double (cJSON *, const gchar *);

int
gvm_json_obj_check_int (cJSON *, const gchar *, int *);

int
gvm_json_obj_int (cJSON *, const gchar *);

Expand Down

0 comments on commit 0cd4ab2

Please sign in to comment.