Detecting this undefined behavior in gcc/clang? - c++

I am trying to detect the following undefined behavior:
% cat undef.cxx
#include <iostream>
class C
{
int I;
public:
int getI() { return I; }
};
int main()
{
C c;
std::cout << c.getI() << std::endl;
return 0;
}
For some reason all my naive attempts have failed so far:
% g++ -Wall -pedantic -o undef -fsanitize=undefined undef.cxx && ./undef
21971
same goes for:
% clang++ -Weverything -o undef -fsanitize=undefined undef.cxx && ./undef
0
Is there a way to use a magic flag in gcc/clang to report a warning/error for the above code at compile time ? at run time ?
References:
% g++ --version
g++ (Debian 10.2.1-6) 10.2.1 20210110
and
% clang++ --version
Debian clang version 11.0.1-2

Turns out my g++ version seems to handle it just fine, all I was missing is the optimization flag:
% g++ -O2 -Wall -pedantic -o undef -fsanitize=undefined undef.cxx && ./undef
undef.cxx: In function ‘int main()’:
undef.cxx:7:25: warning: ‘c.C::I’ is used uninitialized in this function [-Wuninitialized]
7 | int getI() { return I; }
| ^
0
This is clearly documented upstream:
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wuninitialized
Because these warnings depend on optimization, the exact variables or
elements for which there are warnings depend on the precise
optimization options and version of GCC used.
Here is upstream 'meta'-bug to track all those related issues:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=24639

Related

G++ raise compile errors instead of warnings for narrowing conversions

I want to get compile errors instead of warnings for this code:
#include <iostream>
int main(int argc, char ** argv)
{
float a = 1.3f;
int b = 2.0 * a;
std::cout << b << "\n";
}
If I compile it with:
g++ test.cpp -o test
I have no errors.
But If I compile the same code with:
g++ test.cpp -o test -Wconversion
I got the following warning:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:17: warning: conversion from ‘double’ to ‘int’ may change value [-Wfloat-conversion]
6 | int b = 2.0 * a;
I'm looking for a way to get compile errors instead of warnings only for this particular type of warning.
Obs.1: -Werror can make all warnings become errors but it is not what I am looking for
Obs.2: I'm using g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Use -Werror= to treat specific warnings as errors only:
g++ test.cpp -o test -Werror=conversion

g++ ignores inline keyword (odr) unless diagnostic, optimizer or standard flags are given?

I have these three files:
test.h
inline int foo(int i) {
return i;
}
asdf1.cpp
#include "test.h"
int main () {
}
asdf2.cpp
#include "test.h"
int bar () {
return 42;
}
When compiling with g++ asdf1.cpp asdf2.cpp, this happens:
$ g++ asdf1.cpp asdf2.cpp
/usr/bin/ld: /tmp/ccTPkxPK.o: in function `foo(int)':
asdf2.cpp:(.text+0x0): multiple definition of `foo(int)'; /tmp/ccJLbLeL.o:asdf1.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
$ g++ asdf1.cpp asdf2.cpp -O1
$ g++ asdf1.cpp asdf2.cpp -Wpedantic
$ g++ asdf1.cpp asdf2.cpp -std=c++14
$ # all of the above work
Why does g++ ignore the inline keyword unless some unrelated flag is given?
Especially weird since, with the -v flag, g++ tells me that it's using c++14 even when nothing is specified. Also, as far as I'm aware, the inline keyword (in this usage) exists in every C++ standard.

g++ - warning: extended initializer lists only available with -std=c++11 or -std=gnu++11

I've following program.
#include <iostream>
#include <string>
int main() {
int i {0};
std::string str {"Hello World"};
std::cout << i << " : " << str << std::endl;
return 0;
}
When I compile this with g++ I got following error.
I'm using g++ 5.4. g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
I want to know what is the standard way to compile program in g++ using std::C++14 with necessary flags.
Thanks in Advance.
Update:
I've done it with following: g++ ./ex01.cpp -o ex01.out -std=c++14 -Wall -o2
Compile with the flag:
-std=c++14

Which compilation flags should I use to avoid run time errors

Just learned here that -Wsequence-point comiplation flag will pop a warning when the code can invoke UB. I tried it on a statement like
int x = 1;
int y = x+ ++x;
and it worked very nicely. Until now I have compiled with gcc or g++ only using -ansi -pedantic -Wall . Do you have any other helpful flags to make the code more safe and robust?
As alk summed up, use these flags:
-pedantic -Wall -Wextra -Wconversion
First, I think you don't want to use the -ansi flag, as suggested in Should I use "-ansi" or explicit "-std=..." as compiler flags?
Secondly, -Wextra seems to be quite useful too, as suggested in -Wextra how useful is it really?
Thirdly, -Wconversion seems also useful, as suggested in Can I make GCC warn on passing too-wide types to functions?
Fourthly, -pedantic is also helpul,
as suggested in What is the purpose of using -pedantic in GCC/G++ compiler?.
Lastly, enabling -Wall should be fine in this case, so I am pretty doubtful about what you said.
Example with gcc:
Georgioss-MacBook-Pro:~ gsamaras$ cat main.c
int main(void)
{
int x = 1;
int y = x+ ++x;
return 0;
}
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:4:16: warning: unsequenced modification and access to 'x' [-Wunsequenced]
int y = x+ ++x;
~ ^
main.c:4:9: warning: unused variable 'y' [-Wunused-variable]
int y = x+ ++x;
^
2 warnings generated.
Georgioss-MacBook-Pro:~ gsamaras$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.38)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Example with g++, same version:
Georgioss-MacBook-Pro:~ gsamaras$ cp main.c main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp
main.cpp:4:16: warning: unsequenced modification and access to 'x'
[-Wunsequenced]
int y = x+ ++x;
~ ^
main.cpp:4:9: warning: unused variable 'y' [-Wunused-variable]
int y = x+ ++x;
^
2 warnings generated.
Relevant answer of mine, that Wall saves the day once more with a similar problem.

resolved: c++ : put normal method definition into source file while the templates method in header file

Since the definition of template must be put in header file, so I don't like it if the template class is big. so I want to make a normal class with some templated methods. Putting the defintion of templated method into header file, for others, put them into c++ source files. So here is what I am thinking.
// lambda.h
#include <iostream>
class X {
public:
std::function<bool(int)> filter;
template <class F>
void setFilter(F fn) {
filter = fn;
}
void big_function(int x);
};
// cat lambda.cpp
#include <iostream>
#include "lambda.h"
void X::big_function(int x) {
if (filter(x)) std::cout << x << std::endl;
}
// main2.cpp
#include <stdlib.h>
#include "lambda.h"
class Filter {
public:
bool operator()(int x) { return true; }
};
int main() {
X x;
x.setFilter(Filter());
x.big_function(3);
return 0;
}
// cat 2.sh
g++ -c lambda.cpp -ggdb
g++ -c main2.cpp -ggdb -std=c++11
g++ -o main2 main2.o lambda.o -ggdb
this program can compile, but got segment fault during executing (x.big_function(3));
#update
Q1: is my thinking is reasonable? is there any obvious error in my code?
Answer: Yes, it is reasonable, and no obvious error. Thanks to the first 4 comments, I did more test and works.
Q2: actually if I compile with -std=c++11, I will got segment fault. but no segment fault if I don't use std=c++11. ( I tried c++11 yesterday because I used lambda expression rather than function object for "Filter" at beginning). And it my real case, I can't discard c++11 features.
Answer: shame about my fault. fixed the issue by adding -std=c++11 for every compile unit.
zhifan$ sh -x 2.sh
+ g++ -c lambda.cpp -ggdb
+ g++ -c main2.cpp -ggdb
+ g++ -o main2 main2.o lambda.o -ggdb
zhifan$ ./main2
3
zhifan$ vim 2.sh
hifan$ sh -x 2.sh
+ g++ -c lambda.cpp -ggdb **-std=c++11**
+ g++ -c main2.cpp -ggdb -std=c++11
+ g++ -o main2 main2.o lambda.o -ggdb
zhifan$ ./main2
Segmentation fault: 11
zhifan$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix