Skip to content

Commit 4bc79a1

Browse files
authored
Check for nullptr in c/cxx API (#3515)
1 parent f9375be commit 4bc79a1

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

sherpa-onnx/c-api/c-api.cc

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,15 @@ void SherpaOnnxDestroyOnlineRecognizer(
212212

213213
const SherpaOnnxOnlineStream *SherpaOnnxCreateOnlineStream(
214214
const SherpaOnnxOnlineRecognizer *recognizer) {
215+
if (!recognizer) return nullptr;
215216
SherpaOnnxOnlineStream *stream =
216217
new SherpaOnnxOnlineStream(recognizer->impl->CreateStream());
217218
return stream;
218219
}
219220

220221
const SherpaOnnxOnlineStream *SherpaOnnxCreateOnlineStreamWithHotwords(
221222
const SherpaOnnxOnlineRecognizer *recognizer, const char *hotwords) {
223+
if (!recognizer) return nullptr;
222224
SherpaOnnxOnlineStream *stream =
223225
new SherpaOnnxOnlineStream(recognizer->impl->CreateStream(hotwords));
224226
return stream;
@@ -232,23 +234,27 @@ void SherpaOnnxDestroyOnlineStream(const SherpaOnnxOnlineStream *stream) {
232234
void SherpaOnnxOnlineStreamAcceptWaveform(const SherpaOnnxOnlineStream *stream,
233235
int32_t sample_rate,
234236
const float *samples, int32_t n) {
237+
if (!stream) return;
235238
stream->impl->AcceptWaveform(sample_rate, samples, n);
236239
}
237240

238241
int32_t SherpaOnnxIsOnlineStreamReady(
239242
const SherpaOnnxOnlineRecognizer *recognizer,
240243
const SherpaOnnxOnlineStream *stream) {
244+
if (!recognizer || !stream) return 0;
241245
return recognizer->impl->IsReady(stream->impl.get());
242246
}
243247

244248
void SherpaOnnxDecodeOnlineStream(const SherpaOnnxOnlineRecognizer *recognizer,
245249
const SherpaOnnxOnlineStream *stream) {
250+
if (!recognizer || !stream) return;
246251
recognizer->impl->DecodeStream(stream->impl.get());
247252
}
248253

249254
void SherpaOnnxDecodeMultipleOnlineStreams(
250255
const SherpaOnnxOnlineRecognizer *recognizer,
251256
const SherpaOnnxOnlineStream **streams, int32_t n) {
257+
if (!recognizer || !streams) return;
252258
std::vector<sherpa_onnx::OnlineStream *> ss(n);
253259
for (int32_t i = 0; i != n; ++i) {
254260
ss[i] = streams[i]->impl.get();
@@ -259,6 +265,7 @@ void SherpaOnnxDecodeMultipleOnlineStreams(
259265
const SherpaOnnxOnlineRecognizerResult *SherpaOnnxGetOnlineStreamResult(
260266
const SherpaOnnxOnlineRecognizer *recognizer,
261267
const SherpaOnnxOnlineStream *stream) {
268+
if (!recognizer || !stream) return nullptr;
262269
sherpa_onnx::OnlineRecognizerResult result =
263270
recognizer->impl->GetResult(stream->impl.get());
264271
const auto &text = result.text;
@@ -351,10 +358,12 @@ void SherpaOnnxDestroyOnlineStreamResultJson(const char *s) {
351358

352359
void SherpaOnnxOnlineStreamReset(const SherpaOnnxOnlineRecognizer *recognizer,
353360
const SherpaOnnxOnlineStream *stream) {
361+
if (!recognizer || !stream) return;
354362
recognizer->impl->Reset(stream->impl.get());
355363
}
356364

357365
void SherpaOnnxOnlineStreamInputFinished(const SherpaOnnxOnlineStream *stream) {
366+
if (!stream) return;
358367
stream->impl->InputFinished();
359368
}
360369

@@ -379,6 +388,7 @@ int32_t SherpaOnnxOnlineStreamHasOption(const SherpaOnnxOnlineStream *stream,
379388
int32_t SherpaOnnxOnlineStreamIsEndpoint(
380389
const SherpaOnnxOnlineRecognizer *recognizer,
381390
const SherpaOnnxOnlineStream *stream) {
391+
if (!recognizer || !stream) return 0;
382392
return recognizer->impl->IsEndpoint(stream->impl.get());
383393
}
384394

@@ -681,6 +691,7 @@ const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer(
681691
void SherpaOnnxOfflineRecognizerSetConfig(
682692
const SherpaOnnxOfflineRecognizer *recognizer,
683693
const SherpaOnnxOfflineRecognizerConfig *config) {
694+
if (!recognizer || !config) return;
684695
sherpa_onnx::OfflineRecognizerConfig recognizer_config =
685696
GetOfflineRecognizerConfig(config);
686697
recognizer->impl->SetConfig(recognizer_config);
@@ -694,13 +705,15 @@ void SherpaOnnxDestroyOfflineRecognizer(
694705

695706
const SherpaOnnxOfflineStream *SherpaOnnxCreateOfflineStream(
696707
const SherpaOnnxOfflineRecognizer *recognizer) {
708+
if (!recognizer) return nullptr;
697709
SherpaOnnxOfflineStream *stream =
698710
new SherpaOnnxOfflineStream(recognizer->impl->CreateStream());
699711
return stream;
700712
}
701713

702714
const SherpaOnnxOfflineStream *SherpaOnnxCreateOfflineStreamWithHotwords(
703715
const SherpaOnnxOfflineRecognizer *recognizer, const char *hotwords) {
716+
if (!recognizer) return nullptr;
704717
SherpaOnnxOfflineStream *stream =
705718
new SherpaOnnxOfflineStream(recognizer->impl->CreateStream(hotwords));
706719
return stream;
@@ -714,6 +727,7 @@ void SherpaOnnxDestroyOfflineStream(const SherpaOnnxOfflineStream *stream) {
714727
void SherpaOnnxAcceptWaveformOffline(const SherpaOnnxOfflineStream *stream,
715728
int32_t sample_rate, const float *samples,
716729
int32_t n) {
730+
if (!stream) return;
717731
stream->impl->AcceptWaveform(sample_rate, samples, n);
718732
}
719733

@@ -738,12 +752,14 @@ int32_t SherpaOnnxOfflineStreamHasOption(const SherpaOnnxOfflineStream *stream,
738752
void SherpaOnnxDecodeOfflineStream(
739753
const SherpaOnnxOfflineRecognizer *recognizer,
740754
const SherpaOnnxOfflineStream *stream) {
755+
if (!recognizer || !stream) return;
741756
recognizer->impl->DecodeStream(stream->impl.get());
742757
}
743758

744759
void SherpaOnnxDecodeMultipleOfflineStreams(
745760
const SherpaOnnxOfflineRecognizer *recognizer,
746761
const SherpaOnnxOfflineStream **streams, int32_t n) {
762+
if (!recognizer || !streams) return;
747763
std::vector<sherpa_onnx::OfflineStream *> ss(n);
748764
for (int32_t i = 0; i != n; ++i) {
749765
ss[i] = streams[i]->impl.get();
@@ -753,6 +769,7 @@ void SherpaOnnxDecodeMultipleOfflineStreams(
753769

754770
const SherpaOnnxOfflineRecognizerResult *SherpaOnnxGetOfflineStreamResult(
755771
const SherpaOnnxOfflineStream *stream) {
772+
if (!stream) return nullptr;
756773
const sherpa_onnx::OfflineRecognitionResult &result =
757774
stream->impl->GetResult();
758775
const auto &text = result.text;
@@ -1035,36 +1052,42 @@ void SherpaOnnxDestroyKeywordSpotter(const SherpaOnnxKeywordSpotter *spotter) {
10351052

10361053
const SherpaOnnxOnlineStream *SherpaOnnxCreateKeywordStream(
10371054
const SherpaOnnxKeywordSpotter *spotter) {
1055+
if (!spotter) return nullptr;
10381056
SherpaOnnxOnlineStream *stream =
10391057
new SherpaOnnxOnlineStream(spotter->impl->CreateStream());
10401058
return stream;
10411059
}
10421060

10431061
const SherpaOnnxOnlineStream *SherpaOnnxCreateKeywordStreamWithKeywords(
10441062
const SherpaOnnxKeywordSpotter *spotter, const char *keywords) {
1063+
if (!spotter) return nullptr;
10451064
SherpaOnnxOnlineStream *stream =
10461065
new SherpaOnnxOnlineStream(spotter->impl->CreateStream(keywords));
10471066
return stream;
10481067
}
10491068

10501069
int32_t SherpaOnnxIsKeywordStreamReady(const SherpaOnnxKeywordSpotter *spotter,
10511070
const SherpaOnnxOnlineStream *stream) {
1071+
if (!spotter || !stream) return 0;
10521072
return spotter->impl->IsReady(stream->impl.get());
10531073
}
10541074

10551075
void SherpaOnnxDecodeKeywordStream(const SherpaOnnxKeywordSpotter *spotter,
10561076
const SherpaOnnxOnlineStream *stream) {
1077+
if (!spotter || !stream) return;
10571078
spotter->impl->DecodeStream(stream->impl.get());
10581079
}
10591080

10601081
void SherpaOnnxResetKeywordStream(const SherpaOnnxKeywordSpotter *spotter,
10611082
const SherpaOnnxOnlineStream *stream) {
1083+
if (!spotter || !stream) return;
10621084
spotter->impl->Reset(stream->impl.get());
10631085
}
10641086

10651087
void SherpaOnnxDecodeMultipleKeywordStreams(
10661088
const SherpaOnnxKeywordSpotter *spotter,
10671089
const SherpaOnnxOnlineStream **streams, int32_t n) {
1090+
if (!spotter || !streams || n <= 0) return;
10681091
std::vector<sherpa_onnx::OnlineStream *> ss(n);
10691092
for (int32_t i = 0; i != n; ++i) {
10701093
ss[i] = streams[i]->impl.get();
@@ -1075,6 +1098,7 @@ void SherpaOnnxDecodeMultipleKeywordStreams(
10751098
const SherpaOnnxKeywordResult *SherpaOnnxGetKeywordResult(
10761099
const SherpaOnnxKeywordSpotter *spotter,
10771100
const SherpaOnnxOnlineStream *stream) {
1101+
if (!spotter || !stream) return nullptr;
10781102
const sherpa_onnx::KeywordResult &result =
10791103
spotter->impl->GetResult(stream->impl.get());
10801104
const auto &keyword = result.keyword;
@@ -1152,6 +1176,7 @@ void SherpaOnnxDestroyKeywordResult(const SherpaOnnxKeywordResult *r) {
11521176
const char *SherpaOnnxGetKeywordResultAsJson(
11531177
const SherpaOnnxKeywordSpotter *spotter,
11541178
const SherpaOnnxOnlineStream *stream) {
1179+
if (!spotter || !stream) return nullptr;
11551180
const sherpa_onnx::KeywordResult &result =
11561181
spotter->impl->GetResult(stream->impl.get());
11571182

@@ -1189,15 +1214,21 @@ void SherpaOnnxDestroyCircularBuffer(const SherpaOnnxCircularBuffer *buffer) {
11891214

11901215
void SherpaOnnxCircularBufferPush(const SherpaOnnxCircularBuffer *buffer,
11911216
const float *p, int32_t n) {
1217+
if (!buffer) return;
11921218
buffer->impl->Push(p, n);
11931219
}
11941220

11951221
const float *SherpaOnnxCircularBufferGet(const SherpaOnnxCircularBuffer *buffer,
11961222
int32_t start_index, int32_t n) {
1223+
if (!buffer) return nullptr;
11971224
std::vector<float> v = buffer->impl->Get(start_index, n);
11981225

1199-
float *p = new float[n];
1200-
std::copy(v.begin(), v.end(), p);
1226+
float *p = nullptr;
1227+
if (!v.empty()) {
1228+
p = new float[v.size()]();
1229+
std::copy(v.begin(), v.end(), p);
1230+
}
1231+
12011232
return p;
12021233
}
12031234

@@ -1208,18 +1239,22 @@ void SherpaOnnxCircularBufferFree(const float *p) {
12081239

12091240
void SherpaOnnxCircularBufferPop(const SherpaOnnxCircularBuffer *buffer,
12101241
int32_t n) {
1242+
if (!buffer) return;
12111243
buffer->impl->Pop(n);
12121244
}
12131245

12141246
int32_t SherpaOnnxCircularBufferSize(const SherpaOnnxCircularBuffer *buffer) {
1247+
if (!buffer) return 0;
12151248
return buffer->impl->Size();
12161249
}
12171250

12181251
int32_t SherpaOnnxCircularBufferHead(const SherpaOnnxCircularBuffer *buffer) {
1252+
if (!buffer) return 0;
12191253
return buffer->impl->Head();
12201254
}
12211255

12221256
void SherpaOnnxCircularBufferReset(const SherpaOnnxCircularBuffer *buffer) {
1257+
if (!buffer) return;
12231258
buffer->impl->Reset();
12241259
}
12251260

@@ -2686,6 +2721,7 @@ void SherpaOnnxDestroyLinearResampler(const SherpaOnnxLinearResampler *p) {
26862721
const SherpaOnnxResampleOut *SherpaOnnxLinearResamplerResample(
26872722
const SherpaOnnxLinearResampler *p, const float *input, int32_t input_dim,
26882723
int32_t flush) {
2724+
if (!p) return nullptr;
26892725
std::vector<float> o;
26902726
p->impl->Resample(input, input_dim, flush, &o);
26912727

@@ -2707,15 +2743,18 @@ void SherpaOnnxLinearResamplerResampleFree(const SherpaOnnxResampleOut *p) {
27072743

27082744
int32_t SherpaOnnxLinearResamplerResampleGetInputSampleRate(
27092745
const SherpaOnnxLinearResampler *p) {
2746+
if (!p) return 0;
27102747
return p->impl->GetInputSamplingRate();
27112748
}
27122749

27132750
int32_t SherpaOnnxLinearResamplerResampleGetOutputSampleRate(
27142751
const SherpaOnnxLinearResampler *p) {
2752+
if (!p) return 0;
27152753
return p->impl->GetOutputSamplingRate();
27162754
}
27172755

27182756
void SherpaOnnxLinearResamplerReset(const SherpaOnnxLinearResampler *p) {
2757+
if (!p) return;
27192758
p->impl->Reset();
27202759
}
27212760

sherpa-onnx/c-api/cxx-api.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ OnlineRecognizerResult OnlineRecognizer::GetResult(
189189
auto r = SherpaOnnxGetOnlineStreamResult(p_, s->Get());
190190

191191
OnlineRecognizerResult ans;
192+
if (!r) {
193+
return ans;
194+
}
195+
192196
ans.text = r->text;
193197

194198
ans.tokens.resize(r->count);
@@ -753,6 +757,8 @@ KeywordResult KeywordSpotter::GetResult(const OnlineStream *s) const {
753757
auto r = SherpaOnnxGetKeywordResult(p_, s->Get());
754758

755759
KeywordResult ans;
760+
if (!r) return ans;
761+
756762
ans.keyword = r->keyword;
757763

758764
ans.tokens.resize(r->count);
@@ -894,6 +900,8 @@ void CircularBuffer::Push(const float *samples, int32_t n) const {
894900

895901
std::vector<float> CircularBuffer::Get(int32_t start_index, int32_t n) const {
896902
const float *samples = SherpaOnnxCircularBufferGet(p_, start_index, n);
903+
if (!samples) return {};
904+
897905
std::vector<float> ans(n);
898906
std::copy(samples, samples + n, ans.begin());
899907

@@ -1021,6 +1029,7 @@ std::vector<float> LinearResampler::Resample(const float *input,
10211029
int32_t input_dim,
10221030
bool flush) const {
10231031
auto out = SherpaOnnxLinearResamplerResample(p_, input, input_dim, flush);
1032+
if (!out) return {};
10241033

10251034
std::vector<float> ans{out->samples, out->samples + out->n};
10261035

sherpa-onnx/csrc/wave-reader.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ std::vector<std::vector<float>> ReadWaveImpl(std::istream &is,
147147
is.read(reinterpret_cast<char *>(&header.num_channels),
148148
sizeof(header.num_channels));
149149

150+
if (header.num_channels <= 0) {
151+
SHERPA_ONNX_LOGE("Invalid num_channels: %d. Expected > 0",
152+
header.num_channels);
153+
*is_ok = false;
154+
return {};
155+
}
156+
150157
is.read(reinterpret_cast<char *>(&header.sample_rate),
151158
sizeof(header.sample_rate));
152159

sherpa-onnx/csrc/wave-writer.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ struct WaveHeader {
3838
} // namespace
3939

4040
int64_t WaveFileSize(int32_t n_samples, int32_t num_channels /*= 1*/) {
41-
return sizeof(WaveHeader) + n_samples * sizeof(int16_t) * num_channels;
41+
return sizeof(WaveHeader) +
42+
static_cast<int64_t>(n_samples) * sizeof(int16_t) * num_channels;
4243
}
4344

4445
void WriteWave(char *buffer, int32_t sampling_rate, const float *samples,
@@ -91,7 +92,8 @@ void WriteWave(char *buffer, int32_t sampling_rate, const float *samples_ch0,
9192
header.block_align = num_channels * bits_per_sample / 8;
9293
header.bits_per_sample = bits_per_sample;
9394
header.subchunk2_id = 0x61746164; // atad
94-
header.subchunk2_size = n * num_channels * bits_per_sample / 8;
95+
header.subchunk2_size =
96+
static_cast<int64_t>(n) * num_channels * bits_per_sample / 8;
9597

9698
header.chunk_size = 36 + header.subchunk2_size;
9799

@@ -166,7 +168,8 @@ void WriteWaveMultiChannel(char *buffer, int32_t sampling_rate,
166168
header.block_align = num_channels * bits_per_sample / 8;
167169
header.bits_per_sample = bits_per_sample;
168170
header.subchunk2_id = 0x61746164; // atad
169-
header.subchunk2_size = n * num_channels * bits_per_sample / 8;
171+
header.subchunk2_size =
172+
static_cast<int64_t>(n) * num_channels * bits_per_sample / 8;
170173

171174
header.chunk_size = 36 + header.subchunk2_size;
172175

0 commit comments

Comments
 (0)