]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
stream : add nullptr check of whisper_context (#3283)
authorDaniel Bevenius <redacted>
Wed, 25 Jun 2025 12:16:31 +0000 (14:16 +0200)
committerGitHub <redacted>
Wed, 25 Jun 2025 12:16:31 +0000 (14:16 +0200)
* 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: https://github.com/ggml-org/whisper.cpp/issues/3280#issuecomment-3003778035

* examples : add nullptr check for whisper_context

examples/bench/bench.cpp
examples/command/command.cpp
examples/stream/stream.cpp
examples/vad-speech-segments/speech.cpp

index 979dca8ee476efa59db6a998ac6c7364c4e10026..1512f316e54ea9089491f9221746f7679c1a3680 100644 (file)
@@ -67,6 +67,10 @@ static int whisper_bench_full(const whisper_params & params) {
     cparams.flash_attn = params.flash_attn;
 
     struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
+    if (ctx == nullptr) {
+        fprintf(stderr, "error: failed to initialize whisper context\n");
+        return 2;
+    }
 
     {
         fprintf(stderr, "\n");
index 7a09d30a319ea4530cbf86ef5debf04f20b14f90..0f87710cefa3a0dd309b36e4d0715ca6ca011808 100644 (file)
@@ -709,6 +709,10 @@ int main(int argc, char ** argv) {
     cparams.flash_attn = params.flash_attn;
 
     struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
+    if (ctx == nullptr) {
+        fprintf(stderr, "error: failed to initialize whisper context\n");
+        return 2;
+    }
 
     // print some info about the processing
     {
index bc6f13fb267b17701d982f253866d071032d598f..37b23886821b5c76d9d0272379c051612d27b5ef 100644 (file)
@@ -163,6 +163,10 @@ int main(int argc, char ** argv) {
     cparams.flash_attn = params.flash_attn;
 
     struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
+    if (ctx == nullptr) {
+        fprintf(stderr, "error: failed to initialize whisper context\n");
+        return 2;
+    }
 
     std::vector<float> pcmf32    (n_samples_30s, 0.0f);
     std::vector<float> pcmf32_old;
index 26241d950a00c769eaa0194ed6b9b8ae0c681d25..a22425c4b8cee71092d93b1cf4c19f8315efdea7 100644 (file)
@@ -111,6 +111,10 @@ int main(int argc, char ** argv) {
     struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(
             cli_params.vad_model.c_str(),
             ctx_params);
+    if (vctx == nullptr) {
+        fprintf(stderr, "error: failed to initialize whisper context\n");
+        return 2;
+    }
 
     // Detect speech in the input audio file.
     if (!whisper_vad_detect_speech(vctx, pcmf32.data(), pcmf32.size())) {