Cubitect / cubiomes

C library that mimics the Minecraft biome generation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Undefined behaviour

CubedProgrammer opened this issue · comments

I compiled using gcc with the -O3 option and the code doesn't work. I looked into the code and found a lot of arithmetic with ints
that could overflow, such as lines 739 to 746 in layers.c. Signed integer overflow is undefined behaviour and although it will work as they are two's compliment and should not cause a problem in the mentioned lines, the compiler assumes the user isn't relying on undefined behaviour while optimising and can cause the produced binary to not work. You can change it by changing some of the ints to unsigneds.

I'm aware that signed integer overflow is undefined in the C standard which is why the code is compiled using -fwrapv which enforces two's complement. Given that Java only has signed integers and always uses two's complement, it makes life a whole lot easier to simply tell the compiler that we want this behavior here as well. Actually using the C standard here would be messy and prone to bugs that are difficult to find, while providing no optimization benefit for this library.

I was unaware of such a command line option, sorry.