Skip to content

Non-zero offsets to null pointers leads to UBSAN/ASAN errors #2110

@MarcusKlik

Description

@MarcusKlik

Describe the bug

The use of BYTE pointers to calculate offsets in the byte stream (such as in this code) leads to errors when ZSTD is compiled with clang 10 and UBSAN/ASAN sanitizer options.

In cases where the BYTE pointer equals a null pointer, the sanitizer checks fail (the reason is explained here).

A possible fix is to use (uintptr_t) instead of BYTE* to calculate the offsets.

To Reproduce

These errors were generated on UBSAN/ASAN tests run by the CRAN infrastructure (in this case on my R package fst). The exact compiler options used for those runs are documented here. The errors can be reproduced by using compiler options -fsanitize=address,undefined -fno-sanitize=float-divide-by-zero -fno-sanitize=alignment -fno-omit-frame-pointer -frtti.

Additional context

The errors were found on ZSTD release v1.4.4. For completeness, I've pasted the errors below:

ZSTD/compress/zstd_compress.c:1323:46:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_compress_internal.h:876:41:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_compress_internal.h:877:33:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_double_fast.c:99:62:

runtime error: applying zero offset to null pointer

ZSTD/compress/zstd_opt.c:420:42:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_opt.c:568:42:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_opt.c:612:55:

runtime error: applying non-zero offset 4294967294 to null pointer

ZSTD/compress/zstd_lazy.c:663:61:

runtime error: applying zero offset to null pointer

ZSTD/compress/zstd_lazy.c:495:42:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_compress.c:1323:46:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_compress_internal.h:876:41:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_compress_internal.h:877:33:

runtime error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_opt.c:420:42: runtime

error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_opt.c:568:42: runtime

error: applying non-zero offset 1 to null pointer

ZSTD/compress/zstd_opt.c:612:55: runtime

error: applying non-zero offset 8445 to null pointer

thanks for considering my issue and providing a fantastic library!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions