gcc-warning

Crashing threads with *(int*)NULL = 1; problematic?

Deadly 提交于 2019-12-06 03:53:42
问题 I found this in a multi-threaded c application. The authors commented that it's used to make a thread crash in a custom assert function. GCC is fine with it, but clang issues the following warning: note: consider using __builtin_trap() or qualifying pointer with 'volatile' and also issues one of those, for each usage of the assert function: warning: indirection of non-volatile null pointer will be deleted, not trap What is going on here? Is __builtin_trap specific to clang? Should I use it?

Strict aliasing warning, creating uint32_t reference to unsigned char array + offset

淺唱寂寞╮ 提交于 2019-12-06 03:37:58
With GNU GCC 4.7.0+ I got a few strict aliasing warnings, which I would like to resolve. I have a payload (from hardware): unsigned char payload[davidlt::PAYLOAD_SIZE]; I had this line: *(uint32_t*)(payload + davidlt::DATA_OFFSET) = (pid & davidlt::PID_MASK) << davidlt::PID_SHIFT; This creates a pointer to a specific location in payload and 4 bytes are interpreted as uint32_t . A new value uint32_t type is calculated and replaced in the payload. I get: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] I was hoping to solve it by using reinterpret

Why are no strict-aliasing warnings generated for this code?

假装没事ソ 提交于 2019-12-05 18:48:48
I have the following code: struct A { short b; }; struct B { double a; }; void foo (struct B* src) { struct B* b = src; struct A* a = (struct A*)src; b->a = sin(rand()); if(a->b == rand()) { printf("Where are you strict aliasing warnings?\n"); } } I'm compiling the code with the following command line: gcc -c -std=c99 -Wstrict-aliasing=2 -Wall -fstrict-aliasing -O3 foo.c I'm using GCC 4.5.0. I expected the compiler to print out the warning: warning: dereferencing type-punned pointer will break strict-aliasing rules But it never is. I can get the warning to be printed out for other cases, but I

How to obtain warning for forgotten cast in arithmetic?

穿精又带淫゛_ 提交于 2019-12-05 14:11:25
Consider this situation: uint64_t add(uint32_t a, uint32_t b) { return a + b; // programmer neglected (uint64_t) a + b. } How do we get the C or C++ front-end of GCC (or of any other compiler) to warn about this situation: that an operation is being done in a narrow type that is immediately widened? I've read through the current GCC documentation, and tried various warnings like -Wconversion , but nothing. I am not aware of a flag to GCC that will cause a warning. The Coverity static analyzer will issue an OVERFLOW_BEFORE_WIDEN warning as this is flagged in the CERT standards. Disclaimer: I

gcc warning flags for implicit conversions

。_饼干妹妹 提交于 2019-12-05 14:11:11
问题 I recently had a bug in a similar context to next one: double getSomeValue() { return 4.0; } ... std::string str; str = getSomeValue(); As you can see here is easy to spot the problem, but in a large code base where getSomeValue() is not in the same file with the calling code it might be difficult to spot this double to std::string silent conversion. GCC compiles this code fine with -Wall -Wextra -Werror (sample output here, I don't know what warning flags were used: http://ideone.com/BTXBFk)

Remove first char of string C

♀尐吖头ヾ 提交于 2019-12-05 13:26:50
Im trying to remove the first char of the string and keep the remainder, my current code doesnt compile and im confused on how to fix it. My code: char * newStr (char * charBuffer) { int len = strlen(charBuffer); int i = 1; char v; if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){ for(i=1;i<len;i++) v = v + charBuffer[i]; } v = v + '\0'; return v; } Gcc: "Warning: return makes pointer from integer without a cast" Also: "char * newStr (char * charBuffer)" needs to remain the same. Strings don't work like this in C. You're summing up all of the characters in the buffer into the v variable. You

How to define extern variable along with declaration?

…衆ロ難τιáo~ 提交于 2019-12-05 12:48:31
问题 Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable . That means, an extern declaration that initializes the variable serves as a definition for that variable . So, /* Just for testing purpose only */ #include <stdio.h> extern int y

What is the explanation for “warning: assuming that the loop is not infinite”

风格不统一 提交于 2019-12-05 12:43:04
问题 I had just taken the decision to change as many variables from unsigned to int and upon recompiling the code in question, was greeted by this warning message: freespace_state.c:203: warning: assuming that the loop is not infinite The line in question: for (x = startx; x <= endx; ++x, ++xptr) This loop is 60 lines of code (inc white space/brackets etc), and has a goto within it, and at least one occurrence of continue . In this case, I think I am appreciative that GCC is assuming this loop is

Disable gcc warning for incompatible options

☆樱花仙子☆ 提交于 2019-12-05 10:45:38
I'm curious if there is an option to disable gcc warnings about a parameter not being valid for the language being compiled. Ex: cc1: warning: command line option "-Wno-deprecated" is valid for C++/Java/ObjC++ but not for C Our build system passes the warnings we have decided on globally across a build. We have both C/C++ code and the warnings get real annoying when trying to find actual warnings. Any suggestions? It seems to me that if there were such an option, there would have to be a further option to turn off warnings about that option, and so on infinitely. So I suspect there isn't.

Why assignment of double to int does not trigger gcc warnings?

会有一股神秘感。 提交于 2019-12-05 08:23:25
int i = 3.1 / 2 does not cause any warnings,even with -Wall option.Sometimes,I would like to know where precision lose.Why gcc does not support this warning,while msvc support this one? thanks. EDIT: my gcc -v shows Configured with: ../../gcc-4.4.1/configure --prefix=/mingw --build=mingw32 --enable-languages=c,ada,c++,fortran,objc,obj-c++ --disable-nls --disable-win32-registry --enable-libgomp --enable-cxx-flags='-fno-function-sections -fno-data-sections' --disable-werror --enable-threads --disable-symvers --enable-version-specific-runtime-libs --enable-fully-dynamic-string --with-pkgversion=