Fangjun Kuang
Committed by GitHub

Fix possible segfault in C API. (#1059)

@@ -222,7 +222,7 @@ const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult( @@ -222,7 +222,7 @@ const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
222 } 222 }
223 r->tokens_arr = tokens_temp; 223 r->tokens_arr = tokens_temp;
224 224
225 - if (!result.timestamps.empty()) { 225 + if (!result.timestamps.empty() && result.timestamps.size() == r->count) {
226 r->timestamps = new float[r->count]; 226 r->timestamps = new float[r->count];
227 std::copy(result.timestamps.begin(), result.timestamps.end(), 227 std::copy(result.timestamps.begin(), result.timestamps.end(),
228 r->timestamps); 228 r->timestamps);
@@ -490,7 +490,7 @@ const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult( @@ -490,7 +490,7 @@ const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
490 } 490 }
491 r->tokens_arr = tokens_temp; 491 r->tokens_arr = tokens_temp;
492 492
493 - if (!result.timestamps.empty()) { 493 + if (!result.timestamps.empty() && result.timestamps.size() == r->count) {
494 r->timestamps = new float[r->count]; 494 r->timestamps = new float[r->count];
495 std::copy(result.timestamps.begin(), result.timestamps.end(), 495 std::copy(result.timestamps.begin(), result.timestamps.end(),
496 r->timestamps); 496 r->timestamps);