nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.

Home Page:https://nim-works.github.io/nimskull/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NULL dereference when an empty seq is passed to openArray

alaviss opened this issue · comments

Example

proc foo(a: openArray[byte]) =
  echo "some side-effect"

var s: seq[byte]
foo(s)

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:5:14: runtime error: member access within null pointer of type 'struct tySequence__UlGJBex9a7w9bnVuD9ayEVrzg_Content'
some side-effect

Expected Output

some side-effect

Possible Solution

Guard the pointer access

Additional Information

Generated C code:

	s__test_3.len = 0; s__test_3.p = NIM_NIL;
	_1.Field0 = s__test_3.p->data; // NULL dereference
        _1.Field1 = s__test_3.len;
	foo__test_1(_1.Field0, _1.Field1);

Fixed by #1197