Welcome to the Builder Academy

Question Latest TBA Version - Lots of warnings on fresh copy

More
22 Nov 2016 05:26 #6288 by Halenbane
Anyone else noticed a ton of warnings about unused variables in the latest TBA release?


Small example of some of them
puu.sh/sqrzm/e8fc63fcb5.png

Please Log in or Create an account to join the conversation.

More
26 Nov 2016 13:28 #6316 by thomas
I've had alook at them, and most of them are harmless, in the sense that they are telling us that we have some result we're not using. Generally, the variables are pointers or ints assigned the return value of a system call (fgets(), system(), or similar).
When debugging, it is interesting to look at those values, even if we aren't actually using them in the code.

A couple of the warnings are more sinister, though.
Code:
gcc -g -O2 -Wall -Wno-char-subscripts -c -o act.item.o act.item.c act.item.c: In function ‘do_drink’: act.item.c:883:61: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses] if ((GET_OBJ_VAL(temp, 1) == 0) || (!GET_OBJ_VAL(temp, 0) == 1)) {
Which is obviously a bug (the !x == 1 should be x != 1), and
Code:
gcc -g -O2 -Wall -Wno-char-subscripts -c -o dg_olc.o dg_olc.c dg_olc.c: In function ‘script_syntax_highlighting’: dg_olc.c:464:24: warning: iteration 35u invokes undefined behavior [-Waggressive-loop-optimizations] line = str_replace(line, command_color_replacement[i][0], command_color_replacement[i][1]); ^ dg_olc.c:463:13: note: containing loop for (i=0;i <= COMMAND_TERMS;i++) { ^ dg_olc.c:459:24: warning: iteration 49u invokes undefined behavior [-Waggressive-loop-optimizations] line = str_replace(line, syntax_color_replacement[i][0], syntax_color_replacement[i][1]); ^ dg_olc.c:458:13: note: containing loop for (i=0;i <= SYNTAX_TERMS;i++) { ^
which is a more subtle bug - off-by-one.

Because the compiler can determine everything that is happening in this function it will unwind it - making it faster. In the process, it determines that at the end of the loops, we are trying to dereference unassigned memory.

The fix is simple enough - use < instead of <= when comparing in the loops.

I have pushed those changesto tbamud master branch on github.
The following user(s) said Thank You: Halenbane

Please Log in or Create an account to join the conversation.

More
26 Nov 2016 14:29 #6317 by thomas
To avoid all of those surplus warnings, add alter this line in the Makefile
Code:
# Any special flags you want to pass to the compiler -MYFLAGS = -Wall -Wno-char-subscripts +MYFLAGS = -Wall -Wno-char-subscripts -Wno-unused-but-set-variable
The following user(s) said Thank You: Halenbane, WhiskyTest

Please Log in or Create an account to join the conversation.

Time to create page: 0.231 seconds