Why does a switch break cause a segmentation fault

adamgoldberg adamgoldberg at airmail.cc
Tue Jul 23 08:04:07 UTC 2019


On Tuesday, 23 July 2019 at 00:01:09 UTC, Exil wrote:
> On Monday, 22 July 2019 at 22:05:17 UTC, adamgoldberg wrote:
>> Hey, I just happened to be writing a program in D an stumbled 
>> upon a bug, that causes it to terminate after receiving a SEGV 
>> signal, nothing wierd so far but it looks everything I tried 
>> shows it is the break statement inside of a switch.
>> It seems to have a relatively random chance of occuring, and 
>> also somewhat dependant on the compiler, and build mode used.
>> I'm short on time so instead of rewriting my SO post I will 
>> just link it.
>>
>> Here! 
>> https://stackoverflow.com/questions/57153617/random-segmentation-fault-in-d-lang-on-switch-break
>>
>> Hope someone can help!
>
> Could be the statement in the actual switch(), which is 
> accessing a pointer "codecpar".
>
>     switch (stream.codecpar.codec_type)
>                     ^^^^^
>
> This could be null and you aren't checking for it. I find that 
> D sometimes doesn't have the correct line numbers for debug 
> info, even when not doing an optimized build. So it could 
> really be anything in that function.
>
> The root cause could be a lot of things though, some bad 
> codegen or otherwise. Could add a check to make sure though.

I ran the program after adding

    enforce (stream.codecpar != null);

and nothing changed, and by nothing I mean that DMD in debug mode 
still doesnt crash but DMD in release does (like 70% of the 
time), I think its more important to address that issue because I 
can't seem to find any explenation of this behaviour, yet it 
occurs.




More information about the Digitalmars-d-learn mailing list