Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -1821,10 +1821,14 @@ const SherpaOnnxWave *SherpaOnnxReadWave(const char *filename) {

const SherpaOnnxWave *SherpaOnnxReadWaveFromBinaryData(const char *data,
int32_t n) {
if (!data || n <= 0) {
return nullptr;
}

int32_t sample_rate = -1;
bool is_ok = false;

std::istrstream is(data, n);
std::istringstream is(std::string(data, n));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Constructing a std::string from a null pointer with a non-zero size is undefined behavior. Since this is a C-API, it would be safer to add a check to ensure data is not null when n > 0 before this line is executed, for instance at the beginning of the function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The original istrstream(data, n) had the same null-pointer UB, but since we're touching this line anyway, I've added an early if (\!data || n <= 0) return nullptr; guard at the top of the function. Updated in the force-push.


std::vector<float> samples = sherpa_onnx::ReadWave(is, &sample_rate, &is_ok);
if (!is_ok) {
Expand Down
4 changes: 1 addition & 3 deletions sherpa-onnx/csrc/audio-tagging-label-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <string>

#if __ANDROID_API__ >= 9
#include <strstream>

#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
Expand All @@ -30,7 +28,7 @@ AudioTaggingLabels::AudioTaggingLabels(const std::string &filename) {
AudioTaggingLabels::AudioTaggingLabels(AAssetManager *mgr,
const std::string &filename) {
auto buf = ReadFile(mgr, filename);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
Init(is);
}
#endif
Expand Down
5 changes: 2 additions & 3 deletions sherpa-onnx/csrc/character-lexicon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <regex> // NOLINT
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -65,14 +64,14 @@ class CharacterLexicon::Impl {

{
auto buf = ReadFile(mgr, tokens);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));

InitTokens(is);
}

{
auto buf = ReadFile(mgr, lexicon);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitLexicon(is);
}
}
Expand Down
5 changes: 2 additions & 3 deletions sherpa-onnx/csrc/homophone-replacer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <memory>
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -112,7 +111,7 @@ class HomophoneReplacer::Impl {
{
auto buf = ReadFile(mgr, config.lexicon);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitLexicon(is);
}

Expand All @@ -125,7 +124,7 @@ class HomophoneReplacer::Impl {
SHERPA_ONNX_LOGE("hr rule fst: %s", f.c_str());
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
replacer_list_.push_back(
std::make_unique<kaldifst::TextNormalizer>(is));
}
Expand Down
4 changes: 2 additions & 2 deletions sherpa-onnx/csrc/keyword-spotter-transducer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <memory>
#include <regex> // NOLINT
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -321,7 +321,7 @@ class KeywordSpotterTransducerImpl : public KeywordSpotterImpl {

auto buf = ReadFile(mgr, config_.keywords_file);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));

if (!is) {
#if __OHOS__
Expand Down
5 changes: 2 additions & 3 deletions sherpa-onnx/csrc/kokoro-multi-lang-lexicon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <regex>
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -438,7 +437,7 @@ class KokoroMultiLangLexicon::Impl {
void InitTokens(Manager *mgr, const std::string &tokens) {
auto buf = ReadFile(mgr, tokens);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitTokens(is);
}

Expand Down Expand Up @@ -491,7 +490,7 @@ class KokoroMultiLangLexicon::Impl {
for (const auto &f : files) {
auto buf = ReadFile(mgr, f);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitLexicon(is);
}
}
Expand Down
5 changes: 2 additions & 3 deletions sherpa-onnx/csrc/lexicon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <memory>
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -131,13 +130,13 @@ Lexicon::Lexicon(Manager *mgr, const std::string &lexicon,

{
auto buf = ReadFile(mgr, tokens);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitTokens(is);
}

{
auto buf = ReadFile(mgr, lexicon);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitLexicon(is);
}

Expand Down
5 changes: 2 additions & 3 deletions sherpa-onnx/csrc/matcha-tts-lexicon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <regex> // NOLINT
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -174,7 +173,7 @@ class MatchaTtsLexicon::Impl {

{
auto buf = ReadFile(mgr, tokens);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));

InitTokens(is);
}
Expand All @@ -184,7 +183,7 @@ class MatchaTtsLexicon::Impl {
for (const auto &f : files) {
auto buf = ReadFile(mgr, f);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitLexicon(is);
}

Expand Down
5 changes: 2 additions & 3 deletions sherpa-onnx/csrc/melo-tts-lexicon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <regex> // NOLINT
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -53,14 +52,14 @@ class MeloTtsLexicon::Impl {
{
auto buf = ReadFile(mgr, tokens);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitTokens(is);
}

{
auto buf = ReadFile(mgr, lexicon);

std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
InitLexicon(is);
}
}
Expand Down
6 changes: 3 additions & 3 deletions sherpa-onnx/csrc/offline-recognizer-impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -806,7 +806,7 @@ OfflineRecognizerImpl::OfflineRecognizerImpl(
SHERPA_ONNX_LOGE("rule fst: %s", f.c_str());
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
itn_list_.push_back(std::make_unique<kaldifst::TextNormalizer>(is));
}
}
Expand All @@ -824,7 +824,7 @@ OfflineRecognizerImpl::OfflineRecognizerImpl(
auto buf = ReadFile(mgr, f);

std::unique_ptr<std::istream> s(
new std::istrstream(buf.data(), buf.size()));
new std::istringstream(std::string(buf.data(), buf.size())));

std::unique_ptr<fst::FarReader<fst::StdArc>> reader(
fst::FarReader<fst::StdArc>::Open(std::move(s)));
Expand Down
3 changes: 1 addition & 2 deletions sherpa-onnx/csrc/offline-tts-character-frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <memory>
#include <sstream>
#include <string>
#include <strstream>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -96,7 +95,7 @@ OfflineTtsCharacterFrontend::OfflineTtsCharacterFrontend(
const OfflineTtsVitsModelMetaData &meta_data)
: meta_data_(meta_data) {
auto buf = ReadFile(mgr, tokens);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
token2id_ = ReadTokens(is);
}

Expand Down
6 changes: 3 additions & 3 deletions sherpa-onnx/csrc/offline-tts-kitten-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <ios>
#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -102,7 +102,7 @@ class OfflineTtsKittenImpl : public OfflineTtsImpl {
#endif
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
tn_list_.push_back(std::make_unique<kaldifst::TextNormalizer>(is));
}
}
Expand All @@ -124,7 +124,7 @@ class OfflineTtsKittenImpl : public OfflineTtsImpl {
auto buf = ReadFile(mgr, f);

std::unique_ptr<std::istream> s(
new std::istrstream(buf.data(), buf.size()));
new std::istringstream(std::string(buf.data(), buf.size())));

std::unique_ptr<fst::FarReader<fst::StdArc>> reader(
fst::FarReader<fst::StdArc>::Open(std::move(s)));
Expand Down
6 changes: 3 additions & 3 deletions sherpa-onnx/csrc/offline-tts-kokoro-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <ios>
#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -103,7 +103,7 @@ class OfflineTtsKokoroImpl : public OfflineTtsImpl {
#endif
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
tn_list_.push_back(std::make_unique<kaldifst::TextNormalizer>(is));
}
}
Expand All @@ -125,7 +125,7 @@ class OfflineTtsKokoroImpl : public OfflineTtsImpl {
auto buf = ReadFile(mgr, f);

std::unique_ptr<std::istream> s(
new std::istrstream(buf.data(), buf.size()));
new std::istringstream(std::string(buf.data(), buf.size())));

std::unique_ptr<fst::FarReader<fst::StdArc>> reader(
fst::FarReader<fst::StdArc>::Open(std::move(s)));
Expand Down
6 changes: 3 additions & 3 deletions sherpa-onnx/csrc/offline-tts-matcha-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <algorithm>
#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -151,7 +151,7 @@ class OfflineTtsMatchaImpl : public OfflineTtsImpl {
#endif
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
tn_list_.push_back(std::make_unique<kaldifst::TextNormalizer>(is));
}
}
Expand All @@ -173,7 +173,7 @@ class OfflineTtsMatchaImpl : public OfflineTtsImpl {
auto buf = ReadFile(mgr, f);

std::unique_ptr<std::istream> s(
new std::istrstream(buf.data(), buf.size()));
new std::istringstream(std::string(buf.data(), buf.size())));

std::unique_ptr<fst::FarReader<fst::StdArc>> reader(
fst::FarReader<fst::StdArc>::Open(std::move(s)));
Expand Down
5 changes: 3 additions & 2 deletions sherpa-onnx/csrc/offline-tts-pocket-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <list>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -115,7 +116,7 @@ class OfflineTtsPocketImpl : public OfflineTtsImpl {
#endif
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
tn_list_.push_back(std::make_unique<kaldifst::TextNormalizer>(is));
}
}
Expand All @@ -137,7 +138,7 @@ class OfflineTtsPocketImpl : public OfflineTtsImpl {
auto buf = ReadFile(mgr, f);

std::unique_ptr<std::istream> s(
new std::istrstream(buf.data(), buf.size()));
new std::istringstream(std::string(buf.data(), buf.size())));

std::unique_ptr<fst::FarReader<fst::StdArc>> reader(
fst::FarReader<fst::StdArc>::Open(std::move(s)));
Expand Down
6 changes: 3 additions & 3 deletions sherpa-onnx/csrc/offline-tts-vits-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -103,7 +103,7 @@ class OfflineTtsVitsImpl : public OfflineTtsImpl {
#endif
}
auto buf = ReadFile(mgr, f);
std::istrstream is(buf.data(), buf.size());
std::istringstream is(std::string(buf.data(), buf.size()));
tn_list_.push_back(std::make_unique<kaldifst::TextNormalizer>(is));
}
}
Expand All @@ -125,7 +125,7 @@ class OfflineTtsVitsImpl : public OfflineTtsImpl {
auto buf = ReadFile(mgr, f);

std::unique_ptr<std::istream> s(
new std::istrstream(buf.data(), buf.size()));
new std::istringstream(std::string(buf.data(), buf.size())));

std::unique_ptr<fst::FarReader<fst::StdArc>> reader(
fst::FarReader<fst::StdArc>::Open(std::move(s)));
Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/csrc/offline-tts-zipvoice-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cmath>
#include <memory>
#include <string>
#include <strstream>
#include <sstream>
#include <utility>
#include <vector>

Expand Down
Loading