unconditional pointer member access is generated in C for inheritance
alaviss opened this issue · comments
Example
type
O = ref object of RootObj
proc foo(o: ref RootObj) =
echo "some side-effect"
var o: O = nil
foo(o)
Actual Output
$ nim r -d:release -d:useMalloc --passC:-fno-omit-frame-pointer --passC:-mno-omit-leaf-frame-pointer --passC:-fsanitize=undefined --passL:-fsanitize=undefined --debuginfo --linedir test.nim
test.nim:8:5: runtime error: member access within null pointer of type 'struct tyObject_OcolonObjectType___31J4fwfXSYbejl8P6EY8zw'
some side-effect
some side-effect
Expected Output
some side-effect
Possible Solution
Don't generate unconditional access
Additional Information
Generated C code:
o__test_5 = ((tyobject_ocolonobjecttype___31j4fwfxsybejl8p6ey8zw*) nim_nil);
_1 = &o__test_5->sup; // Unconditional NULL dereference
foo__test_3(_1);
While most compiler will generate rather benign code for this (ie. just a pointer offset), this is still undefined behavior.
It's not high priority, but it prevents effective usage of undefined behavior sanitizer with nimskull.
@alaviss pretty sure this can be closed to of you want to do a quick confirmation?