no warning for data truncation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • david

    no warning for data truncation?

    I've noticed that the following compiles (as C) under both VS8 and gcc
    with no warnings, even though there's a possibility of data truncation
    from enum to unsigned char. It does generate a warning under VS6,
    however. Under VS8, I enabled run-time checks, including /RTCc
    (smaller type check), but still no warning. The only way I can get a
    warning under VS8 is if I configure it to compile as C++.

    Two questions:
    (1) why doesn't /RTCc pick up the data truncation (enum to unsigned
    char)?
    (2) is there any way to configure VS8 (aside from compiling as C++)
    that will generate a warning?

    Here's the code:

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    void foo(unsigned char result);
    void bar(void);

    typedef enum {

    OK = 0,
    NOT_OK = 1,
    NO_STATUS = 0xffff

    } STATUS;

    int main(int argc, char* argv[])
    {
    bar();
    printf("%d %s", argc, argv[0]);
    return 0;
    }

    void bar(void)
    {
    STATUS status = OK;
    foo(status);
    }

    void foo(unsigned char result)
    {
    result++;
    return;
    }
  • Richard Heathfield

    #2
    Re: no warning for data truncation?

    david said:
    I've noticed that the following compiles (as C) under both VS8 and gcc
    with no warnings, even though there's a possibility of data truncation
    from enum to unsigned char.
    That's not a syntax error or a constraint violation, so implementations are
    not required to diagnose it. They may *choose* to diagnose it.

    I get a warning from gcc:

    foo.c:26: warning: passing arg 1 of `foo' with different width due to
    prototype
    (2) is there any way to configure VS8 (aside from compiling as C++)
    that will generate a warning?
    Firstly, make sure /W4 and /Za are switched on. Failing that, I suggest you
    take this up in a Windows group, comp.os.ms-windows.program mer.win32 being
    the obvious choice to start off with, since that group is more likely to
    have a good solid core of VS8 experts than is comp.lang.c. But you may
    find that they, in turn, refer you to a Microsoft-hosted group.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • david

      #3
      Re: no warning for data truncation?

      On Oct 23, 4:40 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      >
      Firstly, make sure /W4 and /Za are switched on. Failing that, I suggest you
      Thanks for the tip. Using /W4 and /Za caused the following warning:

      warning C4244: 'function' : conversion from 'STATUS' to 'unsigned
      char', possible loss of data

      Which is what I was hoping to see.

      Cheers.


      Comment

      Working...