On Mon, Nov 04, 2024 at 08:42:37AM +0100, Ján Tomko wrote: > 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@xxxxxxxxxx> > Reported-by: Simon Pilkington > --- > src/util/virjson.c | 8 +++++++- > tools/nss/libvirt_nss_leases.c | 7 ++++++- > tools/nss/libvirt_nss_macs.c | 7 ++++++- > 3 files changed, 19 insertions(+), 3 deletions(-) > > diff --git a/src/util/virjson.c b/src/util/virjson.c > index 4a95e84f5b..18a4585e7b 100644 > --- a/src/util/virjson.c > +++ b/src/util/virjson.c > @@ -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,7 +1480,8 @@ virJSONValueFromString(const char *jsonstring) > > cleanup: > json_object_put(jobj); > - json_tokener_free(tok); > + if (tok) > + json_tokener_free(tok); > return ret; > } > > diff --git a/tools/nss/libvirt_nss_leases.c b/tools/nss/libvirt_nss_leases.c > index 01e965c4a1..aea81bb56e 100644 > --- a/tools/nss/libvirt_nss_leases.c > +++ b/tools/nss/libvirt_nss_leases.c > @@ -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,7 +305,8 @@ findLeases(const char *file, > > cleanup: > json_object_put(jobj); > - json_tokener_free(tok); > + if (tok) > + json_tokener_free(tok); > if (ret != 0) { > free(*addrs); > *addrs = NULL; > diff --git a/tools/nss/libvirt_nss_macs.c b/tools/nss/libvirt_nss_macs.c > index 430023abec..23229a18f3 100644 > --- a/tools/nss/libvirt_nss_macs.c > +++ b/tools/nss/libvirt_nss_macs.c > @@ -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,7 +166,8 @@ findMACs(const char *file, > > cleanup: > json_object_put(jobj); > - json_tokener_free(tok); > + if (tok) > + json_tokener_free(tok); > if (ret != 0) { > for (i = 0; i < *nmacs; i++) { > char *mac = (*macs)[i]; > -- > 2.47.0 Reviewed-by: Richard W.M. Jones <rjones@xxxxxxxxxx> Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org