[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CA+i0qc4iwENN0HyG2--suhgc3_Q=RpFt0tFNgCqWQvALqot4dA@mail.gmail.com>
Date: Thu, 9 Mar 2017 15:40:37 +0200
From: Tomas Winkler <tomasw@...il.com>
To: Måns Rullgård <mans@...sr.com>
Cc: Henrique de Moraes Holschuh <hmh@....eng.br>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
linux-sparse@...r.kernel.org,
Herbert Xu <herbert@...dor.apana.org.au>,
Al Viro <viro@...iv.linux.org.uk>
Subject: Re: Arrays of variable length
On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@...sr.com> wrote:
> Tomas Winkler <tomasw@...il.com> writes:
>
>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@...sr.com> wrote:
>>> Henrique de Moraes Holschuh <hmh@....eng.br> writes:
>>>
>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>> Tomas Winkler <tomasw@...il.com> writes:
>>>>> > Sparse complains for arrays declared with variable length
>>>>> >
>>>>> > 'warning: Variable length array is used'
>>>>> >
>>>>> > Prior to c99 this was not allowed but lgcc (c99) doesn't have problem
>>>>> > with that https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.
>>>>> > And also Linux kernel compilation with W=1 doesn't complain.
>>>>> >
>>>>> > Since sparse is used extensively would like to ask what is the correct
>>>>> > usage of arrays of variable length
>>>>> > within Linux Kernel.
>>>>>
>>>>> Variable-length arrays are a very bad idea. Don't use them, ever.
>>>>> If the size has a sane upper bound, just use that value statically.
>>>>> Otherwise, you have a stack overflow waiting to happen and should be
>>>>> using some kind of dynamic allocation instead.
>>>>>
>>>>> Furthermore, use of VLAs generally results in less efficient code. For
>>>>> instance, it forces gcc to waste a register for the frame pointer, and
>>>>> it often prevents inlining.
>>>>
>>>> Well, if we're going to forbid VLAs in the kernel, IMHO the kernel build
>>>> system should call gcc with -Werror=vla to get that point across early,
>>>> and flush out any offenders.
>>>
>>> If it were up to me, that's exactly what I'd do.
>>
>>>
>> Some parts of the kernel depends on VLA such as ___ON_STACK macros in
>> include/crypto/hash.h
>> It's actually pretty neat implementation, maybe it's too harsh to
>> disable VLA completely.
>
> And what happens if the requested size is insane?
One option is to add '-Wvla-larger-than=n' other option is to selectively
shut down the warning on ON_STACK macros using #pragma
warning(disable:) though this looks rather ugly.
Just a thought
Tomas
Powered by blists - more mailing lists