Skip to contentSkip to conversation

lib: decompress_unxz: fix memory leak of 'in' buffer in single-call mode

PATCH In review Ivy Lopez 2 participants 4 messages Aug 25 – Sep 1

Subsystems (2)
lore
Share & tools
lore

Ivy Lopez

author
#0 lore raw

PATCH 1/1 lib: decompress_unxz: fix memory leak of 'in' buffer in single-call mode

When fill and flush are both NULL (single-call mode), unxz() takes the xz_dec_run() fast path and skips straight to xz_dec_end(s), bypassing the free(in)/free(b.out) cleanup that only runs inside the multi-call (fill/flush) branch. If 'in' was NULL on entry, it gets allocated locally (must_free_in = true) and is never freed on this path, leaking XZ_IOBUF_SIZE bytes on every single-call decompression that doesn't supply its own input buffer.

Move the must_free_in/flush cleanup out of the multi-call branch so it runs after both paths.

---
 lib/decompress_unxz.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/decompress_unxz.c +5 -5
index 05d5cb490a44..9ccded9934c6 100644
@@ -342,13 +342,13 @@ STATIC int INIT unxz(unsigned char *in, long in_size,
 				b.out_pos = 0;
 			}
 		} while (ret == XZ_OK);
+	}
 
-		if (must_free_in)
-			free(in);
+	if (must_free_in)
+		free(in);
 
-		if (flush != NULL)
-			free(b.out);
-	}
+	if (flush != NULL)
+		free(b.out);
 
 	if (in_used != NULL)
 		*in_used += b.in_pos;
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 7 lines · On 2026-08-25 Ivy Lopez wrote:

When fill and flush are both NULL (single-call mode), unxz() takes the xz_dec_run() fast path and skips straight to xz_dec_end(s), bypassing the free(in)/free(b.out) cleanup that only runs inside the multi-call (fill/flush) branch. If 'in' was NULL on entry, it gets allocated locally (must_free_in = true) and is never freed on this path, leaking XZ_IOBUF_SIZE bytes on every single-call decompression that doesn't supply its own input buffer.

There's no leak because calling with fill == NULL && flush == NULL && in == NULL is invalid. See this thread:

https://lore.kernel.org/lkml/[email protected]/T/

Maybe the code needs to be changed to prevent repeated attempts to "fix" it. I will get back to this in 1-3 days.

Signature · 1 line
Lasse Collin
maintainer Reviewed-by
#2 lore raw

The diff is good, but I would like a different commit message because there was no bug. I wrote a suggestion below. If you think it's OK, please submit v2 with the same diff and the new commit message to Andrew Morton. Thanks!

----%<----
lib: decompress_unxz: Make it obvious that there is no memory leak

Calling __decompress() or unxz() with fill == NULL && flush == NULL && in == NULL is invalid, thus there were no memory leaks even though it might have looked like that. Move the conditional free() calls so that it's obvious that there are no leaks.

----%<----

Signature · 1 line
Lasse Collin
author
#3 lore raw

PATCH v2 1/1 lib: decompress_unxz: Make it obvious that there is no memory leak

In reply to Ivy Lopez

Calling __decompress() or unxz() with fill == NULL && flush == NULL && in == NULL is invalid, thus there were no memory leaks even though it might have looked like that. Move the conditional free() calls so that it's obvious that there are no leaks.

---
 lib/decompress_unxz.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/decompress_unxz.c +5 -5
index 05d5cb490a44..9ccded9934c6 100644
@@ -342,13 +342,13 @@ STATIC int INIT unxz(unsigned char *in, long in_size,
 				b.out_pos = 0;
 			}
 		} while (ret == XZ_OK);
+	}
 
-		if (must_free_in)
-			free(in);
+	if (must_free_in)
+		free(in);
 
-		if (flush != NULL)
-			free(b.out);
-	}
+	if (flush != NULL)
+		free(b.out);
 
 	if (in_used != NULL)
 		*in_used += b.in_pos;
Signature · 1 line
2.55.0