tools/libxl: remove usage of VLA arrays
PATCH 1/1 tools/libxl: remove usage of VLA arrays
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12];
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of strlen() with ARRAY_SIZE(), which allows the literal string length to be known at build time. Note ARRAY_SIZE() accounts for the NUL terminator while strlen() didn't, hence subtract 1 from the total size calculation.
--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libs/light/libxl_utils.c +2 -2
@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);
@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);
Signature · 1 line
2.46.0
Quoted 31 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +1
-1@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);
tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 8 lines
tools/libs/light/libxl_utils.c +1
-1@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);
Minor: why not sizeof instead of ARRAY_SIZE ?
Not a regression, but looking at final strings and size added, it looks like the numbers should be more uint16_t than uint32_t. Maybe we want something like
char path[sizeof("/local/pool//name") + 12];BTW,
Frediano
Quoted 30 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +1
-1@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);
tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 8 lines
tools/libs/light/libxl_utils.c +1
-1@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);
Although I have a minor preference for sizeof() as suggested by Frediano.
Can fix on commit, if you're happy?
~Andrew
Quoted 38 lines · Andrew Cooper
Quoted 37 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +2
-2@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;
Quoted 7 lines
tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 1 line
tools/libs/light/libxl_utils.c +0
-0snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);Acked-by Andrew Cooper <[email protected]>Although I have a minor preference for
sizeof()as suggested by Frediano.Can fix on commit, if you're happy?
Please can we stick to ARRAY_SIZE() when it comes to strings? It's the same as sizeof() when the base type is char, but the difference becomes relevant if the base type was e.g. wchar_t.
Jan
Quoted 46 lines · Jan Beulich
Quoted 44 lines · Andrew Cooper
Quoted 30 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +1
-1@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 8 lines
tools/libs/light/libxl_utils.c +1
-1@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);Acked-by Andrew Cooper <[email protected]>Although I have a minor preference for
sizeof()as suggested by Frediano.
Quoted 8 lines
Quoted 1 line
Can fix on commit, if you're happy?
Please can we stick to
ARRAY_SIZE()when it comes to strings? It's the same assizeof()when the base type is char, but the difference becomes relevant if the base type was e.g.wchar_t.Jan
But "literal" is not a wide string, and the type is "char" which is not wide too.
BTW, both me and Andrew are not strong about.
Frediano
Quoted 53 lines · Frediano Ziglio
Quoted 52 lines · Jan Beulich
Quoted 38 lines · Andrew Cooper
Quoted 37 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +2
-2@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;Quoted 7 lines
tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 1 line
tools/libs/light/libxl_utils.c +0
-0snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);Acked-by Andrew Cooper <[email protected]>Although I have a minor preference for
sizeof()as suggested by Frediano.Can fix on commit, if you're happy?
Please can we stick to
ARRAY_SIZE()when it comes to strings? It's the same assizeof()when the base type is char, but the difference becomes relevant if the base type was e.g.wchar_t.
Quoted 7 lines
Quoted 2 lines
Jan
But "literal" is not a wide string, and the type is "char" which is not wide too.
BTW, both me and Andrew are not strong about.
No strong opinion either, I've assumed it was clearer to not make implicit assumptions about the size of the string literal array elements. I would rather like to get this committed, and Jan seems to prefer to use ARRAY_SIZE(), so I suggest we get the patch committed as-is.
Thanks, Roger.
Quoted 60 lines · Roger Pau Monné
Quoted 59 lines · Frediano Ziglio
Quoted 46 lines · Jan Beulich
Quoted 44 lines · Andrew Cooper
Quoted 30 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +1
-1@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 8 lines
tools/libs/light/libxl_utils.c +1
-1@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);Acked-by Andrew Cooper <[email protected]>Although I have a minor preference for
sizeof()as suggested by Frediano.Quoted 8 lines
Quoted 1 line
Can fix on commit, if you're happy?
Please can we stick to
ARRAY_SIZE()when it comes to strings? It's the same assizeof()when the base type is char, but the difference becomes relevant if the base type was e.g.wchar_t.Jan
But "literal" is not a wide string, and the type is "char" which is not wide too.
Quoted 7 lines
Quoted 1 line
BTW, both me and Andrew are not strong about.
No strong opinion either, I've assumed it was clearer to not make implicit assumptions about the size of the string literal array elements. I would rather like to get this committed, and Jan seems to prefer to use
ARRAY_SIZE(), so I suggest we get the patch committed as-is.
Well, ultimately it's Anthony's call, for still being the sole maintainer.
Jan
Anthony PERARD
Acked-byQuoted 38 lines · Andrew Cooper
Quoted 37 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +2
-2@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;
Quoted 5 lines
tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 1 line
tools/libs/light/libxl_utils.c +0
-0snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);Acked-by Andrew Cooper <[email protected]>Although I have a minor preference for
sizeof()as suggested by Frediano.
I have a preference for sizeof() too, we even used it this way (more or less) in libxl before, for eom here:
https://elixir.bootlin.com/xen/v4.19.0/source/tools/libs/light/libxl_qmp.c#L1608
I was a bit supprised by the use of ARRAY_SIZE on a string literal but it's just an array of char :-).
For the patch, with sizeof() or ARRAY_SIZE():
Thanks,
--
Anthony Perard | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
Quoted 45 lines · Anthony PERARD
Quoted 38 lines · Andrew Cooper
Quoted 37 lines · Roger Pau Monne
Clang 19 complains with the following error when building libxl:
libxl_utils.c:48:15: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
48 | char path[strlen("/local/domain") + 12]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~Replace the usage of
strlen()withARRAY_SIZE(), which allows the literal string length to be known at build time. NoteARRAY_SIZE()accounts for the NUL terminator whilestrlen()didn't, hence subtract 1 from the total size calculation.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/libs/light/libxl_utils.c +2
-2@@ -45,7 +45,7 @@ unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsignedchar *libxl_domid_to_name (libxl_ctx *ctx,uint32_t domid) {unsigned int len; -char path[strlen ("/local/domain" ) +12 ]; +char path[ARRAY_SIZE ("/local/domain" ) +11 ];char *s;snprintf (path,sizeof (path),"/local/domain/%d/name" , domid);@@ -141,7 +141,7 @@ int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p,char *libxl_cpupoolid_to_name (libxl_ctx *ctx,uint32_t poolid) {unsigned int len; -char path[strlen ("/local/pool" ) +12 ]; +char path[ARRAY_SIZE ("/local/pool" ) +11 ];char *s;Quoted 4 lines
tools/libs/light/libxl_utils.c int libxl_cpupool_qualifier_to_cpupoolid(libxl_ctx *ctx, const char *p, · line 147
Quoted 1 line
tools/libs/light/libxl_utils.c +0
-0snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid);Acked-by Andrew Cooper <[email protected]>Although I have a minor preference for
sizeof()as suggested by Frediano.I have a preference for
sizeof()too, we even used it this way (more or less) in libxl before, foreomhere:
Quoted 7 lines
https://elixir.bootlin.com/xen/v4.19.0/source/tools/libs/light/libxl_qmp.c#L1608I was a bit supprised by the use of
ARRAY_SIZEon a string literal but it's just an array of char :-).For the patch, with
sizeof()orARRAY_SIZE():Acked-by Anthony PERARD <[email protected]>
That's 3 for sizeof(), one ambivalent, and one for ARRAY_SIZE(), with the maintainer (who gets the majority say) already in the largest group already.
I've adjusted and committed.
~Andrew