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

Segfault when casting a class with a 0 length array #812

Closed
hberntsen opened this issue Dec 17, 2014 · 3 comments
Closed

Segfault when casting a class with a 0 length array #812

hberntsen opened this issue Dec 17, 2014 · 3 comments

Comments

@hberntsen
Copy link

The following code runs fine with rdmd and gdc, but with ldc2 it generates a segfault after the first printline:

import std.stdio;

interface A {}

class B : A {
    ubyte[0] test;
}

void main() {
    A a = new B();
    writeln("Got new B!");
    B b = cast(B) a;
    writeln("all good!");
}

Simply compile this program with ldc2, run it and you get the segfault. I used the prebuilt 0.15.1 binary on Ubuntu 14.10 x86_64. After I changed the length of the ubyte array to 1 it runs fine.

@redstar
Copy link
Member

redstar commented Dec 19, 2014

I can reproduce it. Thanks for the small test case.

@redstar redstar added A-ice and removed A-ice labels Dec 19, 2014
@dnadlinger
Copy link
Member

Doesn't appear to be an ICE? ;)

@JohanEngelen
Copy link
Member

For ubyte[1], a snippet from the outputted LLVM IR:

%test.B_init = type { %test.B.__vtbl*, i8*, [1 x i8], { %object.Interface* }* }
%test.B = type { %test.B.__vtbl*, i8*, [1 x i8], { %object.Interface* }* }

and for ubyte[0]:

%test.B_init = type { %test.B.__vtbl*, i8*, [0 x i8]*, { %object.Interface* }* }
%test.B = type { %test.B.__vtbl*, i8*, { %object.Interface* }* }

In the case of a zero-length array, no space should be allocated. The LLVM struct type of B is correctly generated, but the initializer is not correct. The bug is fixed by skipping zero-size fields when generating the class initializer (in IrAggr::addFieldInitializers()). Submitting a PR soon.

Thanks for this bug report! I am very new to LDC, and it was a fun bug to fix, just at the right level of difficulty for me without knowledge of LDC's codebase :))

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

4 participants