Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1596091

Re: Arrays of variable length

From Måns Rullgård <mans@mansr.com>
Newsgroups linux.kernel
Subject Re: Arrays of variable length
Date 2017-03-09 15:20 +0100
Message-ID <tj758-6kn-15@gated-at.bofh.it> (permalink)
References (2 earlier) <thLJo-6ax-23@gated-at.bofh.it> <thOQV-8jX-1@gated-at.bofh.it> <tj1j5-2og-69@gated-at.bofh.it> <tj5Zn-5Ce-7@gated-at.bofh.it> <tj6LL-5W9-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Tomas Winkler <tomasw@gmail.com> writes:

> On Thu, Mar 9, 2017 at 3:02 PM, Måns Rullgård <mans@mansr.com> wrote:
>> Tomas Winkler <tomasw@gmail.com> writes:
>>
>>> On Mon, Mar 6, 2017 at 2:31 AM, Måns Rullgård <mans@mansr.com> wrote:
>>>> Henrique de Moraes Holschuh <hmh@hmh.eng.br> writes:
>>>>
>>>>> On Sun, 05 Mar 2017, Måns Rullgård wrote:
>>>>>> Tomas Winkler <tomasw@gmail.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'

If you know the upper bound, why use VLAs in the first place?

-- 
Måns Rullgård

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-05 10:50 +0100
  Re: Arrays of variable length Al Viro <viro@ZenIV.linux.org.uk> - 2017-03-05 11:10 +0100
  Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-05 16:50 +0100
    Re: Arrays of variable length Henrique de Moraes Holschuh <hmh@hmh.eng.br> - 2017-03-05 22:20 +0100
      Re: Arrays of variable length Richard Weinberger <richard.weinberger@gmail.com> - 2017-03-05 23:00 +0100
      Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-06 01:40 +0100
        Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 09:10 +0100
          Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 14:10 +0100
            Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 15:00 +0100
              Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 15:20 +0100
                Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 15:30 +0100
                Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 15:50 +0100
                Re: Arrays of variable length Tomas Winkler <tomasw@gmail.com> - 2017-03-09 15:30 +0100
                Re: Arrays of variable length Måns Rullgård <mans@mansr.com> - 2017-03-09 15:50 +0100

csiph-web