lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 09/05/2020 20:03, Hugo Musso Gualandi wrote:
Hi,

GCC 10 just came out and its -Wstringop-overflow warning is pickier
than it used to be. Attached to this message is the warning that I get
when compiling 5.4-rc3 using GCC 10.0.1.

The culprit seems to be the getstr macro in lobject.h. It appears that
GCC doesn't like that the string bytes are after the struct and would
prefer if the byte array were a member of the struct:

  typedef struct TString {
    CommonHeader;
    lu_byte extra;
    lu_byte shrlen;
    unsigned int hash;
    union {
      size_t lnglen;
      struct TString *hnext;
    } u;
    char bytes[1];
  } TString;


  #define getstr(ts) check_exp(sizeof((ts)->extra), ts->bytes)

  #define sizelstring(l)  (sizeof(TString) + (l) * sizeof(char))

-- Hugo

I'm not a C great expert, but isn't that a common C idiom, I think it is called "flexible array member". I read somewhere that C standard made special provisions for that idiom because it is expected that the bytes making up the array (except the first element, which must be in the struct) will be allocated dynamically "outside" the boundary of the struct object itself.

Funny that a standard-compliant compiler complains about that, unless it is a "belt and braces" approach that signals any spot in code that usually goes wrong unless "you know what you are doing".

-- Lorenzo




_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org

_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org