odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.55k stars 570 forks source link

core:os/os2 read_entire_file creates infinite loop #3279

Open marcs-feh opened 6 months ago

marcs-feh commented 6 months ago

Context

odin report

Odin:    dev-2024-03:04f0fbf23
OS:      Arch Linux, Linux 6.6.21-1-lts
CPU:     Intel(R) Celeron(R) N4020 CPU @ 1.10GHz
RAM:     3471 MiB
Backend: LLVM 17.0.5

Expected Behavior

read_entire_file should simply read the whole file into a buffer allocated with a particular allocator.

Current Behavior

An infinite loop is created when reading a file, the issue seems to be in core/os/os2/file_util.odin in the procedure read_entire_file_from_file. Running it on a debugger shows the bug happens during the reading loop: image

Steps to Reproduce

Example bug.odin

package bug

import "core:fmt"
import os "core:os/os2"

main :: proc(){
    f, err := os.read_entire_file("foo.txt", context.allocator)
    fmt.println("Error:", err)
    fmt.println("File data:", f)
}
echo 'Hellope' > foo.txt
odin build bug.odin -file
./bug
JesseRMeyer commented 6 months ago

Is read returning 0 into n?

laytan commented 6 months ago

Obligatory os2 isn't ready for use yet + only really tested on Windows. This is an infinite loop on unix because read returns 0 bytes when at eof on Unix, while on Windows it actually returns an error saying it is eof. So to fix it would need to check if n == 0 and return.

gingerBill commented 6 months ago

We also need to make the behaviour the same across OSes too for this. So that it will work.