Skip to content

Commit 1ad258c

Browse files
authored
stream : add nullptr check of whisper_context (#3283)
* stream : add nullptr check of whisper_context This commit adds a check to ensure that the `whisper_context` is not null after initialization. The motivation for this is that currently, if the initialization fails, the program continues to run leading to a segmentation fault. This sort of check is performed by others examples like whisper-cli. Refs: #3280 (comment) * examples : add nullptr check for whisper_context
1 parent 7dd2997 commit 1ad258c

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

examples/bench/bench.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ static int whisper_bench_full(const whisper_params & params) {
6767
cparams.flash_attn = params.flash_attn;
6868

6969
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
70+
if (ctx == nullptr) {
71+
fprintf(stderr, "error: failed to initialize whisper context\n");
72+
return 2;
73+
}
7074

7175
{
7276
fprintf(stderr, "\n");

examples/command/command.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,10 @@ int main(int argc, char ** argv) {
709709
cparams.flash_attn = params.flash_attn;
710710

711711
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
712+
if (ctx == nullptr) {
713+
fprintf(stderr, "error: failed to initialize whisper context\n");
714+
return 2;
715+
}
712716

713717
// print some info about the processing
714718
{

examples/stream/stream.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ int main(int argc, char ** argv) {
163163
cparams.flash_attn = params.flash_attn;
164164

165165
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
166+
if (ctx == nullptr) {
167+
fprintf(stderr, "error: failed to initialize whisper context\n");
168+
return 2;
169+
}
166170

167171
std::vector<float> pcmf32 (n_samples_30s, 0.0f);
168172
std::vector<float> pcmf32_old;

examples/vad-speech-segments/speech.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ int main(int argc, char ** argv) {
111111
struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(
112112
cli_params.vad_model.c_str(),
113113
ctx_params);
114+
if (vctx == nullptr) {
115+
fprintf(stderr, "error: failed to initialize whisper context\n");
116+
return 2;
117+
}
114118

115119
// Detect speech in the input audio file.
116120
if (!whisper_vad_detect_speech(vctx, pcmf32.data(), pcmf32.size())) {

0 commit comments

Comments
 (0)