diff mbox series

[19/47] libsemanage: avoid leak on realloc failure

Message ID 20241111141706.38039-19-cgoettsche@seltendoof.de (mailing list archive)
State New
Delegated to: Petr Lautrbach
Headers show
Series [01/47] libsemanage: white space cleanup | expand

Commit Message

Christian Göttsche Nov. 11, 2024, 2:16 p.m. UTC
From: Christian Göttsche <cgzones@googlemail.com>

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsemanage/src/direct_api.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libsemanage/src/direct_api.c b/libsemanage/src/direct_api.c
index 83addbee..66a6fa66 100644
--- a/libsemanage/src/direct_api.c
+++ b/libsemanage/src/direct_api.c
@@ -599,12 +599,16 @@  static int read_from_pipe_to_data(semanage_handle_t *sh, size_t initial_len, int
 	while ((read_len = read(fd, data_read + data_read_len, max_len - data_read_len)) > 0) {
 		data_read_len += read_len;
 		if (data_read_len == max_len) {
+			char *tmp;
+
 			max_len *= 2;
-			data_read = realloc(data_read, max_len);
-			if (data_read == NULL) {
+			tmp = realloc(data_read, max_len);
+			if (tmp == NULL) {
 				ERR(sh, "Failed to realloc, out of memory.\n");
+				free(data_read);
 				return -1;
 			}
+			data_read = tmp;
 		}
 	}