Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752553AbdCEKB6 (ORCPT ); Sun, 5 Mar 2017 05:01:58 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:33814 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751736AbdCEKB5 (ORCPT ); Sun, 5 Mar 2017 05:01:57 -0500 Date: Sun, 5 Mar 2017 10:01:35 +0000 From: Al Viro To: Tomas Winkler Cc: "linux-kernel@vger.kernel.org" , sparse@vger.kernel.org Subject: Re: Arrays of variable length Message-ID: <20170305100116.GH29622@ZenIV.linux.org.uk> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.7.1 (2016-10-04) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1136 Lines: 25 On Sun, Mar 05, 2017 at 11:44:33AM +0200, Tomas Winkler wrote: > 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. That depends. For structure members the answer is simply "don't, it's not a valid C to start with". Note that this is about actual VLA, not struct foo { int bar; struct baz[]; } - that is valid C99 and sparse is just fine with it. For local variables... keep in mind that kernel stack is _small_, so any VLA there needs to be done very carefully. For heap it's more or less usable, but keep in mind that gcc support of VLA (and variably-modified types in general) has seriously unpleasant corner cases, especially when combined with the ({...}) thing. IOW, "doesn't have problem" is overoptimistic; use with care.