1

Possible Duplicate:
Can I make GCC warn on passing too-wide types to functions?

Is there a way to make gcc or g++ produce a warning when I pass a signed int to a function that takes an unsigned int?

For instance:

int main(){
        char buf[8];
        int i;
        for(i=0;i<6;i++)
                buf[i] = 'a';
        buf[6]='\0';
        strcat(buf, " ");
        strncat(buf, "happystacksmashingstring",-1 );
        return 0;

}

will cause stack smashing because strncat takes a size_t as its third argument, which is often an unsigned int. Yet, the command:

g++ -Wall -Wextra -Werror -pedantic -W -Weffc++ -Wconversion test.c

which contains every warning flag I know, produces no errors or warnings at compile time, and a smashed stack at runtime.

gcc -Wall -Wextra -Werror -pedantic -W -Wconversion test.c

will produce errors about the implicit conversions in contrast. Why doesn't the -Wconversion flag work properly with g++?

Community
  • 1
  • 1
John Doucette
  • 4,370
  • 5
  • 37
  • 61
  • 2
    -Wsign-conversion. [In C++, ..., Warnings about conversions between signed and unsigned integers are disabled by default in C ++ unless -Wsign-conversion is explicitly enabled.](http://linux.die.net/man/1/g++) – remi Oct 23 '12 at 10:32
  • @remi sorry, saw your comment too late. if you post an answer, I will delete mine – codeling Oct 23 '12 at 10:36
  • @RandolphCarter regarding the duplication: The other question involves autocasts, but of a rather different sort. I did not encounter it during my own searching for an answer to my question, and the answer it proposes (use -Wconversion) does not solve the problem in anycase. – John Doucette Oct 23 '12 at 10:48
  • Thanks for the -Wsign-conversion bit though! That's exactly what I seemed to be missing. – John Doucette Oct 23 '12 at 10:49

1 Answers1

2

From the man page of g++:

Warnings about conversions between signed and unsigned integers are disabled by default in C++ unless-Wsign-conversion is explicitly enabled.

codeling
  • 11,056
  • 4
  • 42
  • 71