Richard W.M. Jones
2010-Feb-05 11:45 UTC
[Libguestfs] -Woverlength-strings / assert unhelpful interaction
$ gcc -O2 -c -Woverlength-strings stupid.c stupid.c: In function ?insert_lf_record?: stupid.c:22: warning: string length ?7394? is greater than the length ?509? ISO C90 compilers are required to support Without -O2 the error message goes away. Even ISO C99 compilers don't help - they raise the limit to a mere 4095 bytes. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones New in Fedora 11: Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 70 libraries supprt'd http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw -------------- next part -------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> struct hive_h { void *addr; }; struct ntreg_hbin_block { char id[2]; }; typedef size_t hive_node_h; #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0) #define BLOCK_ID_EQ(h,offs,eqid) \ (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2)) static size_t insert_lf_record (struct hive_h *h, size_t old_offs, size_t posn, const char *name, hive_node_h node) { assert (BLOCK_ID_EQ (h, old_offs, "lf") || BLOCK_ID_EQ (h, old_offs, "lh")); }
Jim Meyering
2010-Feb-05 12:20 UTC
[Libguestfs] -Woverlength-strings / assert unhelpful interaction
Richard W.M. Jones wrote:> $ gcc -O2 -c -Woverlength-strings stupid.c > stupid.c: In function ?insert_lf_record?: > stupid.c:22: warning: string length ?7394? is greater than the length ?509? ISO C90 compilers are required to support > > Without -O2 the error message goes away. > > Even ISO C99 compilers don't help - they raise the limit to a mere > 4095 bytes.Hi Rich, As you no doubt realize, that is because the expansion of your assert expression is so long. Think of it as encouragement to use inline functions in place of macros whenever possible. This is a good example of how inline functions are superior, in addition to the usual more-type-safe argument. E.g., #define STREQ(a,b) (strcmp(a,b) == 0) static inline int block_id_eq (const struct hive_h *h, size_t offset, const char *s) { return STREQ (((struct ntreg_hbin_block *)(h->addr + offset))->id, s); } static size_t insert_lf_record (struct hive_h *h, size_t old_offs, size_t posn, const char *name, hive_node_h node) { assert (block_id_eq (h, old_offs, "lf") || block_id_eq (h, old_offs, "lh")); }