Skip to content

Commit b219873

Browse files
author
Henry Qin
committed
Add extra character to fileGetContents for null terminator
1 parent 44e97a4 commit b219873

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/Util.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ fileGetContents(FILE* f) {
149149
char* output;
150150
// Determine file size
151151
fseek(f, 0, SEEK_END);
152-
size_t size = ftell(f);
152+
// Need an extra byte for the null terminator.
153+
size_t size = ftell(f) + 1;
153154

154155
// We don't want to allocate memory if the file size is zero, because it is
155156
// undefined behavior to dereference such memory.
@@ -158,6 +159,7 @@ fileGetContents(FILE* f) {
158159
output[0] = '\0';
159160
} else {
160161
output = new char[size];
162+
output[size - 1] = '\0';
161163
rewind(f);
162164
fread(output, sizeof(char), size, f);
163165
}

0 commit comments

Comments
 (0)