Skip to content

Commit 1211a88

Browse files
Fix UB in ZstdDecompressor.unused_data when a frame is decompressed in one call
1 parent 2d7a74e commit 1211a88

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix undefined behavior in
2+
:attr:`compression.zstd.ZstdDecompressor.unused_data` when a complete frame
3+
was decompressed in a single call.

Modules/_zstd/decompressor.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,14 @@ _zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self)
594594
}
595595
else {
596596
if (self->unused_data == NULL) {
597-
self->unused_data = PyBytes_FromStringAndSize(
598-
self->input_buffer + self->in_begin,
599-
self->in_end - self->in_begin);
597+
if (self->input_buffer == NULL) {
598+
self->unused_data = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
599+
}
600+
else {
601+
self->unused_data = PyBytes_FromStringAndSize(
602+
self->input_buffer + self->in_begin,
603+
self->in_end - self->in_begin);
604+
}
600605
ret = self->unused_data;
601606
Py_XINCREF(ret);
602607
}

Tools/ubsan/suppressions.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@ shift-base:Modules/_ctypes/cfield.c
1515
# Modules/_ctypes/cfield.c:640:1: runtime error: signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'
1616
signed-integer-overflow:Modules/_ctypes/cfield.c
1717

18-
# Modules/_zstd/decompressor.c:598:56: runtime error: applying non-zero offset 18446744073709551615 to null pointer
19-
pointer-overflow:Modules/_zstd/decompressor.c
20-
2118
# Modules/_io/stringio.c:350:24: runtime error: addition of unsigned offset to 0x7fd01ec25850 overflowed to 0x7fd01ec2584c
2219
pointer-overflow:Modules/_io/stringio.c

0 commit comments

Comments
 (0)