@@ -110,14 +110,14 @@ pub fn read_file(path string) ?string {
110
110
C.rewind (fp)
111
111
unsafe {
112
112
mut str := malloc (fsize + 1 )
113
- nelements := int (C.fread (str, fsize, 1 , fp))
113
+ nelements := int (C.fread (str, 1 , fsize , fp))
114
114
is_eof := int (C.feof (fp))
115
115
is_error := int (C.ferror (fp))
116
116
if is_eof == 0 && is_error != 0 {
117
117
free (str)
118
118
return error ('fread failed' )
119
119
}
120
- str[fsize ] = 0
120
+ str[nelements ] = 0
121
121
if nelements == 0 {
122
122
// It is highly likely that the file was a virtual file from
123
123
// /sys or /proc, with information generated on the fly, so
@@ -129,7 +129,7 @@ pub fn read_file(path string) ?string {
129
129
// get a V string with .len = 4096 and .str = "PCH\n\\000".
130
130
return str.vstring ()
131
131
}
132
- return str.vstring_with_len (fsize )
132
+ return str.vstring_with_len (nelements )
133
133
}
134
134
}
135
135
@@ -586,13 +586,13 @@ pub fn read_file_array<T>(path string) []T {
586
586
// read the actual data from the file
587
587
len := fsize / tsize
588
588
buf := unsafe { malloc (fsize) }
589
- C.fread (buf, fsize, 1 , fp)
589
+ nread := C.fread (buf, tsize, len , fp)
590
590
C.fclose (fp)
591
591
return unsafe {
592
592
array{
593
593
element_size: tsize
594
594
data: buf
595
- len: len
595
+ len: int (nread)
596
596
cap: len
597
597
}
598
598
}
0 commit comments