[libcamera-devel] [PATCH 7/8] cam: options: Don't initialise variable-length arrays

Kieran Bingham kieran.bingham at ideasonboard.com
Fri Apr 26 18:20:47 CEST 2019


Hi Laurent,

On 26/04/2019 17:01, Laurent Pinchart wrote:
> According to clang, variable-length arrays can't be initialised. Don't
> do so, and explicitly set the last element to 0 instead.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

According to [0], C++11 only supports arrays with a constant-expression
(See 8.3.4 on page 179 of N3337)

C++14 brings in arrays with a 'simple' expression...

However, this works for us, and although it may be a gnu-extension, it
clearly seems to also work on clang as well (based on this series).
Therefore I'm fine with this if it works on our likely only supported
compilers.

[0] https://www.geeksforgeeks.org/variable-length-arrays-in-c-and-c/


Anyway, as this works for us... lets keep it simple rather than replace
with more complex code.

Reviewed-by: Kieran Bingham <kieran.bingham at ideasonboard.com>


> ---
>  src/cam/options.cpp | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/src/cam/options.cpp b/src/cam/options.cpp
> index 172d40f76a05..b80d361eaaf4 100644
> --- a/src/cam/options.cpp
> +++ b/src/cam/options.cpp
> @@ -382,8 +382,8 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv)
>  	 * Allocate short and long options arrays large enough to contain all
>  	 * options.
>  	 */
> -	char shortOptions[options_.size() * 3 + 2] = {};
> -	struct option longOptions[options_.size() + 1] = {};
> +	char shortOptions[options_.size() * 3 + 2];
> +	struct option longOptions[options_.size() + 1];
>  	unsigned int ids = 0;
>  	unsigned int idl = 0;
>  
> @@ -419,6 +419,9 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv)
>  		}
>  	}
>  
> +	shortOptions[ids] = '\0';
> +	memset(&longOptions[idl], 0, sizeof(longOptions[idl]));
> +
>  	opterr = 0;
>  
>  	while (true) {
> 

-- 
Regards
--
Kieran


More information about the libcamera-devel mailing list