bug-gnulib
[Top][All Lists]
Advanced

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

Re: [PATCH] inttostr.h: add compile-time buffer overrun checks


From: Bruno Haible
Subject: Re: [PATCH] inttostr.h: add compile-time buffer overrun checks
Date: Sun, 17 Oct 2010 01:51:37 +0200
User-agent: KMail/1.9.9

Hi Jim,

> +# define inttostr(n, s)                                                 \
> +   ((void) verify_true (sizeof (s) == sizeof (void *)                   \
> +                        || INT_BUFSIZE_BOUND (int) <= sizeof (s)),      \
> +    (inttostr) (n, s))

Nice and clever trick.

Unfortunately, it does not work for variable-length arrays, which are allowed
in C99 and C++.

Test case:

  void foo (int n)
  {
    char buf[10 + (n < 0) + 1];
    char *result = inttostr (n, buf);
  }

Yields:

  foo.c: In function ‘foo’:
  foo.c:38: error: bit-field ‘verify_error_if_negative_size__’ width not an 
integer constant

How to fix this? I tried __builtin_constant_p and __builtin_choose_expr, but
haven't found the trick.

Bruno

reply via email to

[Prev in Thread] Current Thread [Next in Thread]