Skip to content

Commit 235825e

Browse files
committed
Add extra byte to the allocated read_entire_file buffer so it could be safely casted to a cstring
1 parent 7e3e15a commit 235825e

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

core/os/os2/file_util.odin

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (d
129129

130130
if has_size && size > 0 {
131131
total: int
132-
data = make([]byte, size, allocator) or_return
133-
for total < len(data) {
132+
// one extra byte for null terminator
133+
buffer := make([]byte, size + 1, allocator) or_return
134+
data = buffer[:size]
135+
for total < size {
134136
n: int
135137
n, err = read(f, data[total:])
136138
total += n
@@ -156,6 +158,7 @@ read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (d
156158
if err == .EOF || err == .Broken_Pipe {
157159
err = nil
158160
}
161+
append_nothing(&out_buffer) // null terminator
159162
data = out_buffer[:total]
160163
return
161164
}

0 commit comments

Comments
 (0)