Erik Mouw wrote:
> On Thu, Apr 13, 2006 at 10:29:19AM -0700, Stephen Hemminger wrote:
> > Shouldn't compiler have gagged on this?
> 
> Apparently not. At least the compiler I use doesn't warn about it (gcc
> version 3.3.5 (Debian 1:3.3.5-13)).
> 
> Linus, this might be be something for sparse to check:
> 
>   struct mystruct {
>       char name[16];
>   };
> 
>   mystruct ms = { .name = "muchlongerthan16characters" };

If you actually try that example you'd see that gcc (even really old
versions) will warn you about that code.

What gcc WON'T warn about is if everything but the final '\0' fits in
the array.

So the code:
        struct mystruct { char name[16]; };
        struct mystruct exact_length = { .name = "0123456789abcdef" };
        struct mystruct one_too_long = { .name = "0123456789abcdefg" };

Produces:
        a.c:3: warning: initializer-string for array of chars is too long
        a.c:3: warning: (near initialization for 'one_too_long.name')

I believe this is intentional and in line with the C specs.  It's also
useful since it allows you to define macros like:

        #define DECLARE_NON_TERMINATED_STRING(name, val) \
                        const name[sizeof(val "") - 1] = val;

Making sparse a bit stricter than gcc in this case might not be a bad idea,
though.

-Mitch
-
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to