All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tomas Winkler <tomasw@gmail.com>
To: "Måns Rullgård" <mans@mansr.com>
Cc: Henrique de Moraes Holschuh <hmh@hmh.eng.br>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	linux-sparse@vger.kernel.org,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Al Viro <viro@zeniv.linux.org.uk>
Subject: Re: Arrays of variable length
Date: Thu, 9 Mar 2017 16:29:18 +0200	[thread overview]
Message-ID: <CA+i0qc5==KMapL9abAuhxHW7tn3OktqDM53=0Rx9LUPw5MwwpQ@mail.gmail.com> (raw)
In-Reply-To: <yw1xbmtabk6i.fsf@unicorn.mansr.com>

On Thu, Mar 9, 2017 at 4:26 PM, Måns Rullgård <mans@mansr.com> wrote:
> Tomas Winkler <tomasw@gmail.com> writes:
>
>> On Thu, Mar 9, 2017 at 4:16 PM, Måns Rullgård <mans@mansr.com> wrote:
>>> 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?
>>
>> This is a water mark and not  actual usage, but maybe I didn't
>> understand your comment.
>
> If there is an upper bound known at compile time, why not simply use
> that size statically?  If there is no upper bound, well, then you have a
> problem.

If the compiler can do the job, why not to use this flexibility ?

Tomas

  reply	other threads:[~2017-03-09 14:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-05  9:44 Arrays of variable length Tomas Winkler
2017-03-05 10:01 ` Al Viro
2017-03-05 14:27 ` Måns Rullgård
2017-03-05 21:12   ` Henrique de Moraes Holschuh
2017-03-05 21:49     ` Richard Weinberger
2017-03-06  0:31     ` Måns Rullgård
2017-03-09  7:54       ` Tomas Winkler
2017-03-09 13:02         ` Måns Rullgård
2017-03-09 13:02           ` Måns Rullgård
2017-03-09 13:40           ` Tomas Winkler
2017-03-09 14:16             ` Måns Rullgård
2017-03-09 14:16               ` Måns Rullgård
2017-03-09 14:21               ` Tomas Winkler
2017-03-09 14:26                 ` Måns Rullgård
2017-03-09 14:26                   ` Måns Rullgård
2017-03-09 14:29                   ` Tomas Winkler [this message]
2017-03-09 14:38                     ` Måns Rullgård
2017-03-09 14:38                       ` Måns Rullgård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+i0qc5==KMapL9abAuhxHW7tn3OktqDM53=0Rx9LUPw5MwwpQ@mail.gmail.com' \
    --to=tomasw@gmail.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=hmh@hmh.eng.br \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=mans@mansr.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.