Xcode 15.2.0 has a compilation bug leading to incorrect code generation without warnings

environment: mac os 14.2.1 (23C71)

3.2 GHz 六核Intel Core i7

Mac mini 2018

Xcode-15.2.0

our application has errors after update xCode from 14 to 15, minimal reproducible code is as follows:

    1. use Xcode-15.2.0 create a c++ command-line program
    1. paste the code into main.cpp
#include<iostream>
using namespace std;

void test(int var){
    uint64_t value = var;
    int size = sizeof(uint64_t);
    int mostSignificantBit = size * 8;
     
    //    uint64_t var2 = value & (1 << (mostSignificantBit  - 1)); // using this is ok  
    uint64_t var2 = value & (1 << (mostSignificantBit ));            // using this results in program error
    if(var2 > 0){
        std::cout<<value<<std::endl;

    }
    std::cout<<var2<<std::endl;
}

int main(){
    test(32);
return 0;
}
  • 3 set the program into Release mode then build and run, then that corrupt stack shows an assemble instruction **ud2 (undefine) **

affect

 uint64_t var2 = value & (1 << (mostSignificantBit ));  

Perhaps the expression 1 << mostSignificantBit causes an overflow, leading the compiler to generate a "ud2" instruction in release mode. In our actual program, it resulted in a "brk #0x1" instruction, also causing a crash.

The most concerning aspect of this issue is that we didn't make any changes to our code; we simply upgraded from Xcode 14 to version 15. The compilation process went smoothly without any errors, then the testing team didn't notice any significant changes during their simple tests. Consequently, the program ran well, until it encountered these erroneous lines of code after the application was released online, leading to widespread crashes.

you can enable the undefined behavior sanitizer in the Scheme settings under Diagnostics. This produces this warning for me, even for debug builds:

runtime error: shift exponent 64 is too large for 32-bit type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

There's another, separate build setting, CLANG_UNDEFINED_BEHAVIOR_SANITIZER_INTEGER, which did not help here.

This forum isn't a place to report bugs - use Feedback Assistant for that.

Undefined behavior really is undefined, you just got away with it before.

Xcode 15.2.0 has a compilation bug leading to incorrect code generation without warnings
 
 
Q