json: do not call json_tokener_free with NULL

Add an error message for the rare case if json_tokener_new
fails (allocation failure) and guard any use of json_tokener_free
where tok might be NULL (this was possible in libvirt-nss
when the json file could not be opened).

https://gitlab.com/libvirt/libvirt/-/issues/581

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reported-by: Simon Pilkington
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
This commit is contained in:
Ján Tomko 2024-11-04 08:24:39 +01:00
parent 23d78e1c58
commit faf6edfa74
3 changed files with 19 additions and 3 deletions

View File

@ -1462,6 +1462,11 @@ virJSONValueFromString(const char *jsonstring)
VIR_DEBUG("string=%s", jsonstring);
tok = json_tokener_new();
if (!tok) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to create JSON tokener"));
return NULL;
}
json_tokener_set_flags(tok, jsonflags);
jobj = json_tokener_parse_ex(tok, jsonstring, strlen(jsonstring));
jerr = json_tokener_get_error(tok);
@ -1475,6 +1480,7 @@ virJSONValueFromString(const char *jsonstring)
cleanup:
json_object_put(jobj);
if (tok)
json_tokener_free(tok);
return ret;
}

View File

@ -272,6 +272,10 @@ findLeases(const char *file,
}
tok = json_tokener_new();
if (!tok) {
ERROR("failed to create JSON tokener");
goto cleanup;
}
json_tokener_set_flags(tok, jsonflags);
do {
@ -301,6 +305,7 @@ findLeases(const char *file,
cleanup:
json_object_put(jobj);
if (tok)
json_tokener_free(tok);
if (ret != 0) {
free(*addrs);

View File

@ -134,6 +134,10 @@ findMACs(const char *file,
}
tok = json_tokener_new();
if (!tok) {
ERROR("failed to create JSON tokener");
goto cleanup;
}
json_tokener_set_flags(tok, jsonflags);
do {
@ -162,6 +166,7 @@ findMACs(const char *file,
cleanup:
json_object_put(jobj);
if (tok)
json_tokener_free(tok);
if (ret != 0) {
for (i = 0; i < *nmacs; i++) {