Odin icon indicating copy to clipboard operation
Odin copied to clipboard

MemorySanitizer complains about uninitialized (`---`) variables being passed to procedures, should it?

Open joaocarvalhoopen opened this issue 7 months ago • 7 comments

Context

Operating System & Odin Version: Linux OpenSuse
My version of Odin is: odin version dev-2024-05:c091b0d06 Please paste odin report output:

 make run
./main.exe
==123765==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x4de60d in os.read_entire_file_from_handle /home/joaocarvalho/odin/Odin/core/os/os.odin:117:2
    #1 0x4dd943 in os.read_entire_file_from_filename /home/joaocarvalho/odin/Odin/core/os/os.odin:105:2
    #2 0x4d654a in main.main /home/joaocarvalho/odin_proj/tests/test_read_entire_file.odin:9:5
    #3 0x596df1 in main /home/joaocarvalho/odin/Odin/base/runtime/entry_unix.odin:54:4
    #4 0x7f54bc02a1ef in __libc_start_call_main (/lib64/libc.so.6+0x2a1ef) (BuildId: a2c0942c27fb9483b47886a1b937337a797bbceb)
    #5 0x7f54bc02a2b8 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2a2b8) (BuildId: a2c0942c27fb9483b47886a1b937337a797bbceb)
    #6 0x4360f4 in _start /home/abuild/rpmbuild/BUILD/glibc-2.39/csu/../sysdeps/x86_64/start.S:115

SUMMARY: MemorySanitizer: use-of-uninitialized-value /home/joaocarvalho/odin/Odin/core/os/os.odin:117:2 in os.read_entire_file_from_handle
Exiting
make: *** [Makefile:5: run] Error 1

Expected Behavior

Please describe the behavior you are expecting

Current Behavior

What is the current behavior?

If you run the following code compiled with

-sanitize:memory
package main

import "core:fmt"
import "core:os"

main :: proc ( ) {
    file_path := "test_read_entire_file.odin"

    data, sucess := os.read_entire_file_from_filename( file_path )
}

It will give the following warning:

==123765==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x4de60d in os.read_entire_file_from_handle /home/joaocarvalho/odin/Odin/core/os/os.odin:117:2
    #1 0x4dd943 in os.read_entire_file_from_filename /home/joaocarvalho/odin/Odin/core/os/os.odin:105:2
    #2 0x4d654a in main.main /home/joaocarvalho/odin_proj/tests/test_read_entire_file.odin:9:5

And looking at the code, we can see that this is a problem (length) inicialized outside the if with atribution:

read_entire_file_from_handle :: proc(fd: Handle, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
   context.allocator = allocator

   length: i64
   err: Errno
   if length, err = file_size(fd); err != 0 {
       return nil, false
   }

   if length <= 0 {
      return nil, true
   }

It is saying that length scope is inside the if and that it is shadoing so the outside lenght is zero or unitialized.

Best regards, João Carvalho

joaocarvalhoopen avatar Jul 07 '24 22:07 joaocarvalhoopen