none
Signed integer to unsigned conversion

    Question

  • Hi everybody

    Why the C++ standard does not provide a way to detect nonintuitive (misinterpreted) 
    signed/unsigned integral conversions that could lead to the program crashing code like the following:

    int num = -1;                    //meaning there is nothing to copy
    char dst[16] = {};
    char src[16] = {};
    memcpy_s(dst,16,src,num);// no warning

    ?

    The code above does not even generate a warning (VS2005)
    while the compiler knows that memcpy_s accepts unsigned integers
    only as its 4th parameter.

    If I had such a warning I could reconsider initializing num with -1 like this:

    size_t num = 0;                //meaning there is nothing to copy

    The values of type signed integer and unsigned integer have different meanings
    so it could be nice if a compiler to detect and warn about conversions between
    these types, isn't it?

    • Edited by mpoleg Monday, June 23, 2008 1:59 PM correction
    Monday, June 23, 2008 1:43 PM

Answers

  • We have a warning for that (C4365) which is off by default. Presumably because it adds too much for common codebases. You can however enable it for your code via #pragma warning or /w14365.

    -hg
    Visual C++ Libraries Team
    • Marked as answer by mpoleg Monday, June 23, 2008 2:50 PM
    • Unmarked as answer by mpoleg Monday, June 23, 2008 2:57 PM
    • Marked as answer by nobugzMVP, Moderator Tuesday, June 24, 2008 1:11 PM
    Monday, June 23, 2008 2:39 PM

  • Quote>#pragma warning( default : 4365) - has no effect

    What is the warning level you used to build?

    Note that the use of
    #pragma warning( default : 4365)
    will cause this warning to be issued only when Warning Level 4 is set,
    as that is the default level for this warning.

    To get the warning at all Warning Levels use:
    #pragma warning( 1 : 4365)

    - Wayne

    Monday, June 23, 2008 3:39 PM

All replies

  • We have a warning for that (C4365) which is off by default. Presumably because it adds too much for common codebases. You can however enable it for your code via #pragma warning or /w14365.

    -hg
    Visual C++ Libraries Team
    • Marked as answer by mpoleg Monday, June 23, 2008 2:50 PM
    • Unmarked as answer by mpoleg Monday, June 23, 2008 2:57 PM
    • Marked as answer by nobugzMVP, Moderator Tuesday, June 24, 2008 1:11 PM
    Monday, June 23, 2008 2:39 PM
  • Thank you Holger for your helpful answer.

    Is it possible to search for compiler (and linker) warnings by keywords somewhere?
    Or it is only possible to search by warning code in MSDN?

     Also:
    /w14365 - works fine
    but
    #pragma warning( default : 4365) - has no effect
    ?

    • Edited by mpoleg Monday, June 23, 2008 2:59 PM not all advises work
    Monday, June 23, 2008 2:50 PM
  • You can always use full text search. In the offline MSDN Library Viewer you can probably search for

    "Compiler Warning" AND "Error Message"

    with Language=Visual C++

    and getty pretty close to a list of warnings. Just add another search clause like

    AND mykeyword

    and that should do the trick.

    -hg
    Visual C++ Libraries Team
    Monday, June 23, 2008 3:13 PM

  • Quote>#pragma warning( default : 4365) - has no effect

    What is the warning level you used to build?

    Note that the use of
    #pragma warning( default : 4365)
    will cause this warning to be issued only when Warning Level 4 is set,
    as that is the default level for this warning.

    To get the warning at all Warning Levels use:
    #pragma warning( 1 : 4365)

    - Wayne

    Monday, June 23, 2008 3:39 PM
  • Thanks Wayne
    All that works fine.
    Tuesday, June 24, 2008 5:57 AM
  • Thank you Holger

    I will be using MSDN search feature from now on.

    Tuesday, June 24, 2008 6:00 AM