Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: gvm_json_obj_int #881

Merged
merged 7 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,6 @@
cJSON *result_obj = NULL;
const gchar *err = NULL;
openvasd_result_t result = NULL;
int port = 0;
gchar *detail_name = NULL;
gchar *detail_value = NULL;
gchar *detail_source_type = NULL;
Expand All @@ -1120,10 +1119,6 @@
// error
goto res_cleanup;

if ((item = cJSON_GetObjectItem (result_obj, "port")) != NULL
&& cJSON_IsNumber (item))
port = item->valueint;

if ((item = cJSON_GetObjectItem (result_obj, "detail")) != NULL
&& cJSON_IsObject (item))
{
Expand Down Expand Up @@ -1162,7 +1157,7 @@
gvm_json_obj_str (result_obj, "ip_address"),
gvm_json_obj_str (result_obj, "hostname"),
gvm_json_obj_str (result_obj, "oid"),
port,
gvm_json_obj_int (result_obj, "port"),
gvm_json_obj_str (result_obj, "protocol"),
gvm_json_obj_str (result_obj, "message"),
detail_name, detail_value,
Expand Down Expand Up @@ -1262,12 +1257,12 @@
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;

Check warning on line 1265 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1265

Added line #L1265 was not covered by tests
}

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
45 changes: 45 additions & 0 deletions util/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,51 @@ 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 Either NULL or a return location for the int (only set if
* int field 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.
*
* @param[in] obj Object
* @param[in] key Field name.
*
* @return An int.
*/
int
gvm_json_obj_int (cJSON *obj, const gchar *key)
{
cJSON *item;

item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsNumber (item))
return item->valueint;

return 0;
}

/**
* @brief Get a string field from a JSON object.
*
Expand Down
6 changes: 6 additions & 0 deletions util/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ 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 *);

gchar *
gvm_json_obj_str (cJSON *, const gchar *);

Expand Down
92 changes: 92 additions & 0 deletions util/json_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,88 @@ Ensure (json, gvm_json_obj_str_null_when_missing)
cJSON_Delete (json);
}

/* gvm_json_obj_int */

Ensure (json, gvm_json_obj_int_gets_value)
{
cJSON *json;
int i;

json = cJSON_Parse ("{ \"eg\": 33 }");
assert_that (json, is_not_null);
i = gvm_json_obj_int (json, "eg");
assert_that (i, is_equal_to (33));
cJSON_Delete (json);
}

Ensure (json, gvm_json_obj_int_0_when_missing)
{
cJSON *json;
int i;

json = cJSON_Parse ("{ \"eg\": 33 }");
assert_that (json, is_not_null);
i = gvm_json_obj_int (json, "err");
assert_that (i, is_equal_to (0));
cJSON_Delete (json);
}

Ensure (json, gvm_json_obj_int_0_when_str)
{
cJSON *json;
int i;

json = cJSON_Parse ("{ \"eg\": \"abc\" }");
assert_that (json, is_not_null);
i = gvm_json_obj_int (json, "eg");
assert_that (i, is_equal_to (0));
cJSON_Delete (json);
}

/* gvm_json_obj_check_int */

Ensure (json, gvm_json_obj_check_int_0_when_has)
{
cJSON *json;

json = cJSON_Parse ("{ \"eg\": 33 }");
assert_that (json, is_not_null);
assert_that (gvm_json_obj_check_int (json, "eg", NULL), is_equal_to (0));
cJSON_Delete (json);
}

Ensure (json, gvm_json_obj_check_int_1_when_missing)
{
cJSON *json;

json = cJSON_Parse ("{ \"eg\": 33 }");
assert_that (json, is_not_null);
assert_that (gvm_json_obj_check_int (json, "err", NULL), is_equal_to (1));
cJSON_Delete (json);
}

Ensure (json, gvm_json_obj_check_int_1_when_str)
{
cJSON *json;

json = cJSON_Parse ("{ \"eg\": \"33\" }");
assert_that (json, is_not_null);
assert_that (gvm_json_obj_check_int (json, "eg", NULL), is_equal_to (1));
cJSON_Delete (json);
}

Ensure (json, gvm_json_obj_check_int_0_and_val_when_has)
{
cJSON *json;
int ret;

json = cJSON_Parse ("{ \"eg\": 33 }");
assert_that (json, is_not_null);
assert_that (gvm_json_obj_check_int (json, "eg", &ret), is_equal_to (0));
assert_that (ret, is_equal_to (33));
cJSON_Delete (json);
}

int
main (int argc, char **argv)
{
Expand All @@ -103,6 +185,16 @@ main (int argc, char **argv)
add_test_with_context (suite, json, gvm_json_obj_str_gets_value);
add_test_with_context (suite, json, gvm_json_obj_str_null_when_missing);

add_test_with_context (suite, json, gvm_json_obj_int_gets_value);
add_test_with_context (suite, json, gvm_json_obj_int_0_when_missing);
add_test_with_context (suite, json, gvm_json_obj_int_0_when_str);

add_test_with_context (suite, json, gvm_json_obj_check_int_0_when_has);
add_test_with_context (suite, json, gvm_json_obj_check_int_1_when_missing);
add_test_with_context (suite, json, gvm_json_obj_check_int_1_when_str);
add_test_with_context (suite, json,
gvm_json_obj_check_int_0_and_val_when_has);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());
return run_test_suite (suite, create_text_reporter ());
Expand Down