Skip to content

whisper : guard null source in buffer loader read callback - #3982

Open
ousamabenyounes wants to merge 1 commit into
ggml-org:masterfrom
ousamabenyounes:fix/issue-3879
Open

whisper : guard null source in buffer loader read callback#3982
ousamabenyounes wants to merge 1 commit into
ggml-org:masterfrom
ousamabenyounes:fix/issue-3879

Conversation

@ousamabenyounes

Copy link
Copy Markdown

What

The buffer-based model loader (whisper_init_from_buffer_with_params) installs a
read callback that copies from buf->buffer + current_offset with memcpy.
When the buffer is exhausted — or the supplied buffer is empty/NULL —
size_to_copy is 0 and the source pointer is NULL. Passing a NULL pointer to
memcpy is undefined behavior even for a zero-length copy.

Skip the copy when there is nothing to copy.

This addresses bug 1 of #3879 (the null-pointer-to-memcpy UB). Bug 2 (the
int32 overflow when sizing the mel-filter buffer) is a distinct issue already
covered by the open PR #3780, so it is intentionally left out here.

Why

Loading a crafted/short model through the buffer loader (e.g. via
whisper_init_from_buffer) drives the callback with nothing left to copy.
UBSan flags the memcpy directly.

Test verification (RED → GREEN)

Added tests/test-whisper-buffer-loader.cpp: loading from a NULL/empty buffer
and from a truncated non-model buffer must return NULL without UB. It is
labelled gh so CI's ctest -L gh runs it.

The undefined behavior is deterministic under UBSan (built with
-fsanitize=address,undefined -fno-sanitize-recover=all):

RED (upstream base — memcpy unguarded, new test applied):

/src/src/whisper.cpp:3698:15: runtime error: null pointer passed as argument 2, which is declared to never be null
    #0 ... in operator() /src/src/whisper.cpp:3698
test exit: 1

GREEN (with the fix):

test-whisper-buffer-loader: OK
test exit: 0

Normal-build run (regression guard): the new test plus the existing whisper-cli
tiny test and the UTF-8 unit test all pass.

Refs #3879 (bug 1). Bug 2 is tracked by #3780.

whisper_init_from_buffer_with_params_no_state installs a read callback that
copies from buf->buffer + current_offset. When the buffer is exhausted (or the
supplied buffer is empty), size_to_copy is 0 and the source pointer can be null;
passing a null pointer to memcpy is undefined behavior even for a zero-length
copy (UBSan: 'null pointer passed as argument 2' at the memcpy). Loading a
crafted/short model through the buffer loader could hit this.

Skip the memcpy when there is nothing to copy. Loading from a null/empty or
truncated buffer now fails gracefully (returns NULL) with no UB.

This addresses bug 1 of ggml-org#3879. Bug 2 (integer overflow when sizing the mel
filter buffer) is covered by the open PR ggml-org#3780.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant