File tree Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -49,10 +49,22 @@ class Dictionary {
4949 }
5050
5151 SizeType size = static_cast <SizeType>(base_size);
52- std::vector<DictionaryUnit> units_buf (size);
53- if (!input->read (reinterpret_cast <char *>(&units_buf[0 ]),
54- sizeof (DictionaryUnit) * size)) {
55- return false ;
52+ std::vector<DictionaryUnit> units_buf;
53+
54+ // read the file in batches to avoid a corrupted file from asking to allocate
55+ // a very large amount of memory
56+ SizeType batch_size = 1000 ;
57+ while ( size > 0 ) {
58+ SizeType size_to_read = std::min (size, batch_size);
59+ SizeType cur_size = units_buf.size ();
60+ units_buf.resize (cur_size + size_to_read);
61+ if (!input->read (reinterpret_cast <char *>(&units_buf[cur_size]),
62+ sizeof (DictionaryUnit) * size_to_read)) {
63+ return false ;
64+ }
65+ // subtract size_to_read (not batch_size)
66+ // so size does not integer overflow on becoming negative
67+ size -= size_to_read;
5668 }
5769
5870 SwapUnitsBuf (&units_buf);
You can’t perform that action at this time.
0 commit comments