Skip to content

Commit da0fbb2

Browse files
committed
[fix Fst2Txt] Resolve memory allocation issue
This commit fixes "Not enough memory in new_buffer" when applying Fst2Txt on files larger than sizeof(int). For example, if int has 32 bits, this is for files larger than 2.1 GB.
1 parent cedfefa commit da0fbb2

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Buffer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ fseek(fileread,0,SEEK_END);
8686
long file_size=ftell(fileread);
8787
fseek(fileread,save_pos,SEEK_SET);
8888
int capacity=(int)((file_size/item_size)+0x10);
89-
if ((capacity_limit != 0) && (capacity>capacity_limit)) {
89+
// FIXME() if file_size is larger than sizeof(int) thus capacity
90+
// can be negative and a "Not enough memory in new_buffer" arises,
91+
// ideally capacity must not be of type int but rather of type size_t
92+
// in order to be use with subsequent malloc() calls. However, this
93+
// change implies to check all current calls to new_buffer() in order
94+
// to make sure that unsigned capacities (e.g -1) are not being casted
95+
// to signed numbers, e.g. (int) -1 ==> (size_t) 4294967295
96+
if ((capacity < 0) || ((capacity_limit != 0) && (capacity>capacity_limit))) {
9097
capacity=capacity_limit;
9198
}
9299
return new_buffer(capacity,type);

0 commit comments

Comments
 (0)