4

Say I have the following program:

#include <iostream>

float foo(float f)
    { return (f / 0); }

int main(void) {
    foo(1.0f);
    std::cout << "hello" << std::endl;
}

If I invoke clang++ -fsanitize=undefined main.cpp && ./a.out then it will output:

main.cpp:4:32: runtime error: division by zero
hello

Is there a way to terminate a.out as soon as an error is detected? I.e. in such a way that it displays:

main.cpp:4:32: runtime error: division by zero

without displaying hello on the following line? (because it will have terminated before)

Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
  • Related: http://stackoverflow.com/q/30809022/420683 --> you can use the environment variable `UBSAN_OPTIONS=halt_on_error=1` – dyp Jan 13 '17 at 14:50

1 Answers1

4

You need to use the -fno-sanitize-recover command line argument:

clang++ -fsanitize=undefined -fno-sanitize-recover=undefined main.cpp
Holt
  • 36,600
  • 7
  • 92
  • 139