Files
whisper.cpp/tests/test-whisper-zero-samples.cpp
T
Ben Younes eacbd8234c whisper : default-initialize whisper_mel to avoid uninitialized read (#3981)
whisper_full()/whisper_full_with_state() only compute the mel spectrogram when
n_samples > 0. For n_samples == 0 on a freshly allocated state the mel is never
touched, but struct whisper_mel had no member initializers, so n_len / n_len_org
/ n_mel were indeterminate heap garbage (the state is allocated with
new whisper_state). seek_end is derived from that garbage and, depending on it,
the call either quietly returns 0 or runs the encoder with garbage dimensions
over an empty (NULL) mel buffer, dereferencing address 0 in the mel copy loop.

Give whisper_mel default member initializers so a never-computed mel reads as
0 frames and the n_samples == 0 case deterministically takes the existing
too-short path.

Fixes #3978
2026-08-31 05:53:57 +02:00

31 lines
758 B
C++

#include "whisper.h"
#include <cstdio>
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
int main() {
struct whisper_context_params cparams = whisper_context_default_params();
cparams.use_gpu = false;
struct whisper_context * ctx = whisper_init_from_file_with_params(WHISPER_MODEL_PATH, cparams);
assert(ctx != nullptr);
struct whisper_full_params params = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
params.no_timestamps = true;
params.print_progress = false;
params.print_realtime = false;
const int rc = whisper_full(ctx, params, nullptr, 0);
assert(rc == 0);
assert(whisper_full_n_segments(ctx) == 0);
whisper_free(ctx);
printf("test-whisper-zero-samples: OK\n");
return 0;
}