[PATCH] info: Handle failure of call to file
The buffer allocated by getline() for the lineptr argument needs to be freed even if getline() fails. The corollary of this is that even in a failure case the pointer is non-null but points to garbage data. Properly handle the failure by freeing the pointer and returning NULL to indicate that failure to the caller. Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de> --- info/show.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/info/show.c b/info/show.c index a3a57ef00ce6..1596eb77e62a 100644 --- a/info/show.c +++ b/info/show.c @@ -398,8 +398,11 @@ get_content (struct nbd_handle *nbd, int64_t size) if (fp == NULL) goto out; r = getline (&ret, &len, fp); - if (r == -1) + if (r == -1) { + free(ret); + ret = NULL; goto out; + } /* Remove trailing \n. */ if (r > 0 && ret[r-1] == '\n') base-commit: 673a8c38571e128e6581c7e6ed6c45461c30bc8f prerequisite-patch-id: 637ca3e7b5e88873d34b2865a134427338700b72 -- 2.44.0
On Tue, Mar 05, 2024 at 09:35:48PM +0100, Thomas Weißschuh wrote:
The buffer allocated by getline() for the lineptr argument needs to be freed even if getline() fails. The corollary of this is that even in a failure case the pointer is non-null but points to garbage data.
Properly handle the failure by freeing the pointer and returning NULL to indicate that failure to the caller.
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de> --- info/show.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/info/show.c b/info/show.c index a3a57ef00ce6..1596eb77e62a 100644 --- a/info/show.c +++ b/info/show.c @@ -398,8 +398,11 @@ get_content (struct nbd_handle *nbd, int64_t size) if (fp == NULL) goto out; r = getline (&ret, &len, fp); - if (r == -1) + if (r == -1) { + free(ret); + ret = NULL; goto out; + }
/* Remove trailing \n. */ if (r > 0 && ret[r-1] == '\n')
base-commit: 673a8c38571e128e6581c7e6ed6c45461c30bc8f prerequisite-patch-id: 637ca3e7b5e88873d34b2865a134427338700b72
Thanks, upstream in cf9f2c1df5 Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
participants (2)
-
Richard W.M. Jones -
Thomas Weißschuh