]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-148286: Fix UB in `ZstdDecompressor.unused_data` when a frame is decompress...
authorStan Ulbrych <stan@python.org>
Mon, 13 Jul 2026 07:31:44 +0000 (09:31 +0200)
committerGitHub <noreply@github.com>
Mon, 13 Jul 2026 07:31:44 +0000 (07:31 +0000)
(cherry picked from commit adebb68153346043c0671fa5725d269c32cc40e4)

Co-authored-by: Emma Smith <emma@emmatyping.dev>
Misc/NEWS.d/next/Library/2026-07-07-13-31-52.gh-issue-148286.-qu-em.rst [new file with mode: 0644]
Modules/_zstd/decompressor.c

diff --git a/Misc/NEWS.d/next/Library/2026-07-07-13-31-52.gh-issue-148286.-qu-em.rst b/Misc/NEWS.d/next/Library/2026-07-07-13-31-52.gh-issue-148286.-qu-em.rst
new file mode 100644 (file)
index 0000000..60cd490
--- /dev/null
@@ -0,0 +1,3 @@
+Fix undefined behavior in
+:attr:`compression.zstd.ZstdDecompressor.unused_data` when a complete frame
+was decompressed in a single call.
index c9b57a898e7903f98dc138eb8b3b3c4732406bf6..85b67d468864890cc9fdf31c8f702f953fc563f0 100644 (file)
@@ -594,9 +594,14 @@ _zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self)
     }
     else {
         if (self->unused_data == NULL) {
-            self->unused_data = PyBytes_FromStringAndSize(
-                                    self->input_buffer + self->in_begin,
-                                    self->in_end - self->in_begin);
+            if (self->input_buffer == NULL) {
+                self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
+            }
+            else {
+                self->unused_data = PyBytes_FromStringAndSize(
+                                        self->input_buffer + self->in_begin,
+                                        self->in_end - self->in_begin);
+            }
             ret = self->unused_data;
             Py_XINCREF(ret);
         }