Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

var result array - bad codegen (null pointer dereference) #8053

Closed
mratsim opened this issue Jun 16, 2018 · 1 comment
Closed

var result array - bad codegen (null pointer dereference) #8053

mratsim opened this issue Jun 16, 2018 · 1 comment

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jun 16, 2018

The following test case generates a null pointer dereference in the C code which results in SIGSEGV:

It should probably be catched by #124 or #7373, but I'm not sure how to slice part of
an array with compile-time known boundaries in a way compatible with JS and compile-time function evaluation.

type
  Foo[N: static[int]] = object
    data: array[N, int]

proc lo[N: static[int]](x: Foo[N]): Foo[N div 2] =
  for i in 0 ..< N div 2:
    result.data[i] = x.data[i]

proc lo[N: static[int]](x: var Foo[N]): var Foo[N div 2] =
  for i in 0 ..< N div 2:
    result.data[i] = x.data[i]

proc `lo=`[N: static[int]](x: var Foo[N], y: Foo[N div 2]) =
  for i in 0 ..< N div 2:
    x.data[i] = y.data[i]

let a = Foo[4](data: [1, 2, 3, 4])

echo a
echo a.lo    # OK

var b: Foo[4]

echo b.lo    # crash
b.lo = a.lo  # crash
N_LIB_PRIVATE N_NIMCALL(tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ, lo_axkQ9bkuqVKw9aIqFQKaK0zw)(tyObject_Foo_fudkhWAqx7S1trfINt0x9ag* x) {
	tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ result;
	memset((void*)(&result), 0, sizeof(tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ));
	{
		NI i;
		NI i_2;
		i = (NI)0;
		i_2 = ((NI) 0);
		{
			while (1) {
				if (!(i_2 < ((NI) 2))) goto LA3;
				i = i_2;
				result.data[(i)- 0] = (*x).data[(i)- 0];
				i_2 += ((NI) 1);
			} LA3: ;
		}
	}
	return result;
}

N_LIB_PRIVATE N_NIMCALL(tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ*, lo_JQlhvEq9aj9b5ARpjNX2rtlw)(tyObject_Foo_fudkhWAqx7S1trfINt0x9ag* x) {
	tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ* result;
	result = (tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ*)0;  // <------------ null pointer
	{
		NI i;
		NI i_2;
		i = (NI)0;
		i_2 = ((NI) 0);
		{
			while (1) {
				if (!(i_2 < ((NI) 2))) goto LA3;
				i = i_2;
				(*result).data[(i)- 0] = (*x).data[(i)- 0]; // <------------ dereference
				i_2 += ((NI) 1);
			} LA3: ;
		}
	}
	return result;
}

N_LIB_PRIVATE N_NIMCALL(void, loeq__2v3e0euqbo9bm3oN3Lkc73w)(tyObject_Foo_fudkhWAqx7S1trfINt0x9ag* x, tyObject_Foo_9ctxI3pPBaeTK4lcwG7MCPQ y) {
	{
		NI i;
		NI i_2;
		i = (NI)0;
		i_2 = ((NI) 0);
		{
			while (1) {
				if (!(i_2 < ((NI) 2))) goto LA3;
				i = i_2;
				(*x).data[(i)- 0] = y.data[(i)- 0];
				i_2 += ((NI) 1);
			} LA3: ;
		}
	}
}
@LemonBoy
Copy link
Contributor

Minimal test case:

proc foo(): var array[1,int] = discard
echo foo()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants