Skip to content

Commit

Permalink
FAPI: Add result check for json_object_object_add.
Browse files Browse the repository at this point in the history
json-c versions < 0.13 are not supported. In these versions
it was not possible to check the result of json_object_object_add.
The check is now added.

Signed-off-by: Juergen Repp <juergen_repp@web.de>
  • Loading branch information
JuergenReppSIT authored and AndreasFuchsTPM committed Feb 21, 2024
1 parent 7e5e413 commit 2e53b68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/tss2-fapi/api/Fapi_NvExtend.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ Fapi_NvExtend_Finish(
/* libjson-c does not deliver an array if array has only one element */
if (jsoType != json_type_array) {
json_object *jsonArray = json_object_new_array();
json_object_array_add(jsonArray, command->jso_event_log);
if (json_object_array_add(jsonArray, command->jso_event_log)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
command->jso_event_log = jsonArray;
}
} else {
Expand All @@ -423,7 +425,9 @@ Fapi_NvExtend_Finish(
r = ifapi_json_IFAPI_EVENT_serialize(&command->pcr_event, &jso);
goto_if_error(r, "Error serialize event", error_cleanup);

json_object_array_add(command->jso_event_log, jso);
if (json_object_array_add(command->jso_event_log, jso)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
SAFE_FREE(object->misc.nv.event_log);
strdup_check(object->misc.nv.event_log,
json_object_to_json_string_ext(command->jso_event_log,
Expand Down
24 changes: 18 additions & 6 deletions src/tss2-fapi/ifapi_eventlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ ifapi_eventlog_get_async(
r = ifapi_json_IFAPI_EVENT_serialize(&cel_event, &jso);
goto_if_error(r, "Error serialize event", error);

json_object_array_add(eventlog->log, jso);
if (json_object_array_add(eventlog->log, jso)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
}
}

Expand Down Expand Up @@ -155,7 +157,9 @@ ifapi_eventlog_get_async(
r = ifapi_json_IFAPI_EVENT_serialize(&cel_event, &jso);
goto_if_error(r, "Error serialize event", error);

json_object_array_add(eventlog->log, jso);
if (json_object_array_add(eventlog->log, jso)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
}
}
if (eventlog->ima_log_file) {
Expand Down Expand Up @@ -286,15 +290,19 @@ ifapi_eventlog_get_finish(
json_type jso_type = json_object_get_type(logpart);
if (jso_type != json_type_array) {
/* libjson-c does not deliver an array if array has only one element */
json_object_array_add(eventlog->log, logpart);
if (json_object_array_add(eventlog->log, logpart)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
} else {
/* Iterate through the array of logpart and add each item to the eventlog */
/* The return type of json_object_array_length() was changed, thus the case */
for (int i = 0; i < (int)json_object_array_length(logpart); i++) {
jso_event = json_object_array_get_idx(logpart, i);
/* Increment the refcount of event so it does not get freed on put(logpart) below */
json_object_get(jso_event);
json_object_array_add(eventlog->log, jso_event);
if (json_object_array_add(eventlog->log, jso_event)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
}
json_object_put(logpart);
}
Expand Down Expand Up @@ -365,7 +373,9 @@ ifapi_eventlog_append_check(
json_type jso_type = json_object_get_type(eventlog->log);
if (jso_type != json_type_array) {
json_object *json_array = json_object_new_array();
json_object_array_add(json_array, eventlog->log);
if (json_object_array_add(json_array, eventlog->log)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
eventlog->log = json_array;
}
} else {
Expand Down Expand Up @@ -444,7 +454,9 @@ ifapi_eventlog_append_finish(
goto_error(r, TSS2_FAPI_RC_BAD_VALUE, "Error serializing event data", error_cleanup);
}

json_object_array_add(eventlog->log, event);
if (json_object_array_add(eventlog->log, event)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
logstr2 = json_object_to_json_string_ext(eventlog->log, JSON_C_TO_STRING_PRETTY);

/* Construct the filename for the eventlog file */
Expand Down
4 changes: 3 additions & 1 deletion test/integration/fapi-quote-destructive-eventlog.int.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,9 @@ test_fapi_quote_destructive(FAPI_CONTEXT *context)
jso_duplicate = json_object_get(jso_event);
goto_if_null(jso_duplicate, "Out of memory.", TSS2_FAPI_RC_MEMORY, error);

json_object_array_add(jso_log2, jso_duplicate);
if (json_object_array_add(jso_log2, jso_duplicate)) {
return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Could not add json object.");
}
}

pcrEventLog2 = strdup(json_object_to_json_string_ext(jso_log2, JSON_C_TO_STRING_PRETTY));
Expand Down

0 comments on commit 2e53b68

Please sign in to comment.