diff mbox

[libiberty] Avoid zero-length VLAs.

Message ID CAOxa4KrBsAQ7JuY=f2ZPf0SWboxa0p7wvrefxFGvqwmzUL_Mzw@mail.gmail.com
State New
Headers show

Commit Message

Brooks Moses June 13, 2016, 4:05 p.m. UTC
Zero-length variable-length-arrays are not allowed in standard C99,
and perhaps more importantly, they cause ASAN to complain.  (See,
e.g., https://gcc.gnu.org/ml/gcc-patches/2013-09/msg00917.html.)

With this patch, the libiberty tests, including demangler-fuzzer, are
ASAN-clean.

- Brooks


----

Comments

Brooks Moses July 11, 2016, 8:05 p.m. UTC | #1
Ping?

(I suspect I should have added a libiberty maintainer to cc in the first place.)

On Mon, Jun 13, 2016 at 9:05 AM, Brooks Moses <bmoses@google.com> wrote:
> Zero-length variable-length-arrays are not allowed in standard C99,
> and perhaps more importantly, they cause ASAN to complain.  (See,
> e.g., https://gcc.gnu.org/ml/gcc-patches/2013-09/msg00917.html.)
>
> With this patch, the libiberty tests, including demangler-fuzzer, are
> ASAN-clean.
>
> - Brooks
>
>
> ----
> ==== libiberty/ChangeLog ====
> --- a/libiberty/ChangeLog
> +++ b/libiberty/ChangeLog
> @@ -1,3 +1,8 @@
> +2016-06-12  Brooks Moses  <bmoses@google.com>
> +
> +       * cp-demangle.c (cplus_demangle_print_callback): Avoid zero-length
> +       VLAs.
> +
>  2016-05-31  Alan Modra  <amodra@gmail.com>
>
>         * xmemdup.c (xmemdup): Use xmalloc rather than xcalloc.
> ==== libiberty/cp-demangle.c ====
> --- a/libiberty/cp-demangle.c
> +++ b/libiberty/cp-demangle.c
> @@ -4120,8 +4120,10 @@
>
>    {
>  #ifdef CP_DYNAMIC_ARRAYS
> -    __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
> -    __extension__ struct d_print_template temps[dpi.num_copy_templates];
> +    __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
> +                                             ? dpi.num_saved_scopes : 1];
> +    __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
> +                                               ? dpi.num_copy_templates : 1];
>
>      dpi.saved_scopes = scopes;
>      dpi.copy_templates = temps;
Ian Lance Taylor July 11, 2016, 11:27 p.m. UTC | #2
On Mon, Jul 11, 2016 at 1:05 PM, Brooks Moses <bmoses@google.com> wrote:
> Ping?

This is fine, but please add a comment explaining why the code avoids
0-length VLAs.  Thanks.

Ian

> (I suspect I should have added a libiberty maintainer to cc in the first place.)
>
> On Mon, Jun 13, 2016 at 9:05 AM, Brooks Moses <bmoses@google.com> wrote:
>> Zero-length variable-length-arrays are not allowed in standard C99,
>> and perhaps more importantly, they cause ASAN to complain.  (See,
>> e.g., https://gcc.gnu.org/ml/gcc-patches/2013-09/msg00917.html.)
>>
>> With this patch, the libiberty tests, including demangler-fuzzer, are
>> ASAN-clean.
>>
>> - Brooks
>>
>>
>> ----
>> ==== libiberty/ChangeLog ====
>> --- a/libiberty/ChangeLog
>> +++ b/libiberty/ChangeLog
>> @@ -1,3 +1,8 @@
>> +2016-06-12  Brooks Moses  <bmoses@google.com>
>> +
>> +       * cp-demangle.c (cplus_demangle_print_callback): Avoid zero-length
>> +       VLAs.
>> +
>>  2016-05-31  Alan Modra  <amodra@gmail.com>
>>
>>         * xmemdup.c (xmemdup): Use xmalloc rather than xcalloc.
>> ==== libiberty/cp-demangle.c ====
>> --- a/libiberty/cp-demangle.c
>> +++ b/libiberty/cp-demangle.c
>> @@ -4120,8 +4120,10 @@
>>
>>    {
>>  #ifdef CP_DYNAMIC_ARRAYS
>> -    __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
>> -    __extension__ struct d_print_template temps[dpi.num_copy_templates];
>> +    __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
>> +                                             ? dpi.num_saved_scopes : 1];
>> +    __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
>> +                                               ? dpi.num_copy_templates : 1];
>>
>>      dpi.saved_scopes = scopes;
>>      dpi.copy_templates = temps;
Brooks Moses July 11, 2016, 11:49 p.m. UTC | #3
On Mon, Jul 11, 2016 at 4:27 PM, Ian Lance Taylor <iant@google.com> wrote:
> On Mon, Jul 11, 2016 at 1:05 PM, Brooks Moses <bmoses@google.com> wrote:
>> Ping?
>
> This is fine, but please add a comment explaining why the code avoids
> 0-length VLAs.  Thanks.

Thanks!

Committed as r238233.  This is the comment I added:

+    /* Avoid zero-length VLAs, which are prohibited by the C99 standard
+       and flagged as errors by Address Sanitizer.  */
+    __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
+                                              ? dpi.num_saved_scopes : 1];
+    __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
+                                                ? dpi.num_copy_templates : 1];

- Brooks
diff mbox

Patch

==== libiberty/ChangeLog ====
--- a/libiberty/ChangeLog
+++ b/libiberty/ChangeLog
@@ -1,3 +1,8 @@ 
+2016-06-12  Brooks Moses  <bmoses@google.com>
+
+       * cp-demangle.c (cplus_demangle_print_callback): Avoid zero-length
+       VLAs.
+
 2016-05-31  Alan Modra  <amodra@gmail.com>

        * xmemdup.c (xmemdup): Use xmalloc rather than xcalloc.
==== libiberty/cp-demangle.c ====
--- a/libiberty/cp-demangle.c
+++ b/libiberty/cp-demangle.c
@@ -4120,8 +4120,10 @@ 

   {
 #ifdef CP_DYNAMIC_ARRAYS
-    __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
-    __extension__ struct d_print_template temps[dpi.num_copy_templates];
+    __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
+                                             ? dpi.num_saved_scopes : 1];
+    __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
+                                               ? dpi.num_copy_templates : 1];

     dpi.saved_scopes = scopes;
     dpi.copy_templates = temps;