@@ -733,7 +733,9 @@ class BaseVectorizer {
733733 throw std::runtime_error (" Unable to load tfidf model file to " + model_filename);
734734 } else {
735735 size_t total_features = 0 ;
736- fscanf (fp, " %ld" , &total_features);
736+ if (1 != fscanf (fp, " %ld" , &total_features)) {
737+ throw std::runtime_error (" Invalid tfidf model file (total_features)." );
738+ }
737739 feature_vocab.reserve (total_features);
738740 idx_idf.reserve (total_features);
739741
@@ -742,12 +744,16 @@ class BaseVectorizer {
742744 int32_t idx = 0 ;
743745 float32_t idf = 0.0 ;
744746 uint64_t ngram_len = 0 ;
745- fscanf (fp, " %d%f%ld" , &idx, &idf, &ngram_len);
747+ if (3 != fscanf (fp, " %d%f%ld" , &idx, &idf, &ngram_len)) {
748+ throw std::runtime_error (" Invalid tfidf model file (idx, idf, ngram_len)." );
749+ }
746750 idx_idf[idx] = idf;
747751 idx_vec_t ngram (ngram_len);
748752 for (size_t tid = 0 ; tid < ngram_len; tid++) {
749753 int32_t tok_idx;
750- fscanf (fp, " %d" , &tok_idx);
754+ if (1 != fscanf (fp, " %d" , &tok_idx)) {
755+ throw std::runtime_error (" Invalid tfidf model file (tok_idx)." );
756+ }
751757 ngram[tid] = tok_idx;
752758 }
753759 feature_vocab[ngram] = idx;
0 commit comments