On 4/29/20 12:29 PM, Marc Nieper-Wißkirchen wrote:
> It would be great if the flexmember exported another macro, say
> XFLEXSIZEOF, which returned SIZE_MAX in case of arithmetic overflow.

Something like this?

/* Like FLEXSIZEOF, except yield SIZE_MAX on arithmetic overflow,
   and N might be evaluated more than once.  */

#define XFLEXSIZEOF_XSIZE(type, member, n) \
  (((n) <= FLEXSIZEOF (type, member, n) \
    && FLEXSIZEOF (type, member, n) <= (size_t) -1) \
   ? (size_t) FLEXSIZEOF (type, member, n) : (size_t) -1)

A couple of problems with this approach:

  * It evaluates N more than once.

  * If the FLEXSIZEOF calls appears in a ptrdiff_t context it might not
    return the right value. ptrdiff_t is also a popular way
    to compute sizes.

But perhaps it's good enough.

Reply via email to