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

Message ID 20190426150155.18652-8-laurent.pinchart@ideasonboard.com
State Accepted
Commit 5d987629d9df6f9eeb76079a99810d57c820219e
Headers show
Series
  • Fix clang compilation warnings and errors
Related show

Commit Message

Laurent Pinchart April 26, 2019, 3:01 p.m. UTC
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@ideasonboard.com>
---
 src/cam/options.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Kieran Bingham April 26, 2019, 4:20 p.m. UTC | #1
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@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@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) {
>

Patch

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) {