[PATCH v3 2/3] qga/commands-win32: Do not set matrix_lookup_t/win_10_0_t arrays size

Philippe Mathieu-Daudé posted 3 patches 9 months, 1 week ago
Maintainers: Konstantin Kostiuk <kkostiuk@redhat.com>, Michael Roth <michael.roth@amd.com>
There is a newer version of this series
[PATCH v3 2/3] qga/commands-win32: Do not set matrix_lookup_t/win_10_0_t arrays size
Posted by Philippe Mathieu-Daudé 9 months, 1 week ago
ga_get_win_name() iterates over all elements in the arrays by
checking the 'version' field is non-NULL. Since the arrays are
guarded by a NULL terminating element, we don't need to specify
their size:

  static char *ga_get_win_name(...)
  {
      ...
      const ga_matrix_lookup_t *table = WIN_VERSION_MATRIX[tbl_idx];
      const ga_win_10_0_t *win_10_0_table = ...
      ...
      while (table->version != NULL) {
                    ^^^^^^^^^^^^^^^
              while (win_10_0_table->version != NULL) {
                                     ^^^^^^^^^^^^^^^

This will simplify maintenance when adding new entries to these
arrays.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 qga/commands-win32.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 79b5a580c9..87ce6e2870 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -2124,7 +2124,7 @@ typedef struct _ga_matrix_lookup_t {
     const char *version_id;
 } ga_matrix_lookup_t;
 
-static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][7] = {
+static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][] = {
     {
         /* Desktop editions */
         { 5, 0, "Microsoft Windows 2000",   "2000"},
@@ -2133,7 +2133,7 @@ static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][7] = {
         { 6, 1, "Microsoft Windows 7"       "7"},
         { 6, 2, "Microsoft Windows 8",      "8"},
         { 6, 3, "Microsoft Windows 8.1",    "8.1"},
-        { 0, 0, 0}
+        { }
     },{
         /* Server editions */
         { 5, 2, "Microsoft Windows Server 2003",        "2003"},
@@ -2141,28 +2141,27 @@ static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][7] = {
         { 6, 1, "Microsoft Windows Server 2008 R2",     "2008r2"},
         { 6, 2, "Microsoft Windows Server 2012",        "2012"},
         { 6, 3, "Microsoft Windows Server 2012 R2",     "2012r2"},
-        { 0, 0, 0},
-        { 0, 0, 0}
+        { },
     }
 };
 
 typedef struct _ga_win_10_0_t {
     int first_build;
-    const char *version;
-    const char *version_id;
+    char const *version;
+    char const *version_id;
 } ga_win_10_0_t;
 
-static const ga_win_10_0_t WIN_10_0_SERVER_VERSION_MATRIX[4] = {
+static const ga_win_10_0_t WIN_10_0_SERVER_VERSION_MATRIX[] = {
     {14393, "Microsoft Windows Server 2016",    "2016"},
     {17763, "Microsoft Windows Server 2019",    "2019"},
     {20344, "Microsoft Windows Server 2022",    "2022"},
-    {0, 0}
+    { }
 };
 
-static const ga_win_10_0_t WIN_10_0_CLIENT_VERSION_MATRIX[3] = {
+static const ga_win_10_0_t WIN_10_0_CLIENT_VERSION_MATRIX[] = {
     {10240, "Microsoft Windows 10",    "10"},
     {22000, "Microsoft Windows 11",    "11"},
-    {0, 0}
+    { }
 };
 
 static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp)
-- 
2.41.0


Re: [PATCH v3 2/3] qga/commands-win32: Do not set matrix_lookup_t/win_10_0_t arrays size
Posted by Philippe Mathieu-Daudé 9 months ago
On 22/2/24 16:28, Philippe Mathieu-Daudé wrote:
> ga_get_win_name() iterates over all elements in the arrays by
> checking the 'version' field is non-NULL. Since the arrays are
> guarded by a NULL terminating element, we don't need to specify
> their size:
> 
>    static char *ga_get_win_name(...)
>    {
>        ...
>        const ga_matrix_lookup_t *table = WIN_VERSION_MATRIX[tbl_idx];
>        const ga_win_10_0_t *win_10_0_table = ...
>        ...
>        while (table->version != NULL) {
>                      ^^^^^^^^^^^^^^^
>                while (win_10_0_table->version != NULL) {
>                                       ^^^^^^^^^^^^^^^
> 
> This will simplify maintenance when adding new entries to these
> arrays.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   qga/commands-win32.c | 19 +++++++++----------
>   1 file changed, 9 insertions(+), 10 deletions(-)


>   typedef struct _ga_win_10_0_t {
>       int first_build;
> -    const char *version;
> -    const char *version_id;
> +    char const *version;
> +    char const *version_id;

Oops, missed rebase.

>   } ga_win_10_0_t;

Re: [PATCH v3 2/3] qga/commands-win32: Do not set matrix_lookup_t/win_10_0_t arrays size
Posted by Konstantin Kostiuk 9 months ago
On Thu, Feb 22, 2024 at 5:28 PM Philippe Mathieu-Daudé <philmd@linaro.org>
wrote:

> ga_get_win_name() iterates over all elements in the arrays by
> checking the 'version' field is non-NULL. Since the arrays are
> guarded by a NULL terminating element, we don't need to specify
> their size:
>
>   static char *ga_get_win_name(...)
>   {
>       ...
>       const ga_matrix_lookup_t *table = WIN_VERSION_MATRIX[tbl_idx];
>       const ga_win_10_0_t *win_10_0_table = ...
>       ...
>       while (table->version != NULL) {
>                     ^^^^^^^^^^^^^^^
>               while (win_10_0_table->version != NULL) {
>                                      ^^^^^^^^^^^^^^^
>
> This will simplify maintenance when adding new entries to these
> arrays.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  qga/commands-win32.c | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
>
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index 79b5a580c9..87ce6e2870 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -2124,7 +2124,7 @@ typedef struct _ga_matrix_lookup_t {
>      const char *version_id;
>  } ga_matrix_lookup_t;
>
> -static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][7] = {
> +static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][] = {
>

I love this idea but mingw-gcc - no.

../qga/commands-win32.c:2125:33: error: array type has incomplete element
type ‘ga_matrix_lookup_t[]’ {aka ‘struct _ga_matrix_lookup_t[]’}
 2125 | static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][] = {
      |                                 ^~~~~~~~~~~~~~~~~~
../qga/commands-win32.c:2125:33: note: declaration of ‘WIN_VERSION_MATRIX’
as multidimensional array must have bounds for all dimensions except the
first

I think we can do the same as with Win10 and create 2 variables:
 WIN_SERVER_VERSION_MATRIX and WIN_CLIENT_VERSION_MATRIX

If you want I can fix this by myself.


Best Regards,
Konstantin Kostiuk.

     {
>          /* Desktop editions */
>          { 5, 0, "Microsoft Windows 2000",   "2000"},
> @@ -2133,7 +2133,7 @@ static const ga_matrix_lookup_t
> WIN_VERSION_MATRIX[2][7] = {
>          { 6, 1, "Microsoft Windows 7"       "7"},
>          { 6, 2, "Microsoft Windows 8",      "8"},
>          { 6, 3, "Microsoft Windows 8.1",    "8.1"},
> -        { 0, 0, 0}
> +        { }
>      },{
>          /* Server editions */
>          { 5, 2, "Microsoft Windows Server 2003",        "2003"},
> @@ -2141,28 +2141,27 @@ static const ga_matrix_lookup_t
> WIN_VERSION_MATRIX[2][7] = {
>          { 6, 1, "Microsoft Windows Server 2008 R2",     "2008r2"},
>          { 6, 2, "Microsoft Windows Server 2012",        "2012"},
>          { 6, 3, "Microsoft Windows Server 2012 R2",     "2012r2"},
> -        { 0, 0, 0},
> -        { 0, 0, 0}
> +        { },
>      }
>  };
>
>  typedef struct _ga_win_10_0_t {
>      int first_build;
> -    const char *version;
> -    const char *version_id;
> +    char const *version;
> +    char const *version_id;
>  } ga_win_10_0_t;
>
> -static const ga_win_10_0_t WIN_10_0_SERVER_VERSION_MATRIX[4] = {
> +static const ga_win_10_0_t WIN_10_0_SERVER_VERSION_MATRIX[] = {
>      {14393, "Microsoft Windows Server 2016",    "2016"},
>      {17763, "Microsoft Windows Server 2019",    "2019"},
>      {20344, "Microsoft Windows Server 2022",    "2022"},
> -    {0, 0}
> +    { }
>  };
>
> -static const ga_win_10_0_t WIN_10_0_CLIENT_VERSION_MATRIX[3] = {
> +static const ga_win_10_0_t WIN_10_0_CLIENT_VERSION_MATRIX[] = {
>      {10240, "Microsoft Windows 10",    "10"},
>      {22000, "Microsoft Windows 11",    "11"},
> -    {0, 0}
> +    { }
>  };
>
>  static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp)
> --
> 2.41.0
>
>
Re: [PATCH v3 2/3] qga/commands-win32: Do not set matrix_lookup_t/win_10_0_t arrays size
Posted by Konstantin Kostiuk 9 months, 1 week ago
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>

On Thu, Feb 22, 2024 at 5:28 PM Philippe Mathieu-Daudé <philmd@linaro.org>
wrote:

> ga_get_win_name() iterates over all elements in the arrays by
> checking the 'version' field is non-NULL. Since the arrays are
> guarded by a NULL terminating element, we don't need to specify
> their size:
>
>   static char *ga_get_win_name(...)
>   {
>       ...
>       const ga_matrix_lookup_t *table = WIN_VERSION_MATRIX[tbl_idx];
>       const ga_win_10_0_t *win_10_0_table = ...
>       ...
>       while (table->version != NULL) {
>                     ^^^^^^^^^^^^^^^
>               while (win_10_0_table->version != NULL) {
>                                      ^^^^^^^^^^^^^^^
>
> This will simplify maintenance when adding new entries to these
> arrays.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  qga/commands-win32.c | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
>
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index 79b5a580c9..87ce6e2870 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -2124,7 +2124,7 @@ typedef struct _ga_matrix_lookup_t {
>      const char *version_id;
>  } ga_matrix_lookup_t;
>
> -static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][7] = {
> +static const ga_matrix_lookup_t WIN_VERSION_MATRIX[2][] = {
>      {
>          /* Desktop editions */
>          { 5, 0, "Microsoft Windows 2000",   "2000"},
> @@ -2133,7 +2133,7 @@ static const ga_matrix_lookup_t
> WIN_VERSION_MATRIX[2][7] = {
>          { 6, 1, "Microsoft Windows 7"       "7"},
>          { 6, 2, "Microsoft Windows 8",      "8"},
>          { 6, 3, "Microsoft Windows 8.1",    "8.1"},
> -        { 0, 0, 0}
> +        { }
>      },{
>          /* Server editions */
>          { 5, 2, "Microsoft Windows Server 2003",        "2003"},
> @@ -2141,28 +2141,27 @@ static const ga_matrix_lookup_t
> WIN_VERSION_MATRIX[2][7] = {
>          { 6, 1, "Microsoft Windows Server 2008 R2",     "2008r2"},
>          { 6, 2, "Microsoft Windows Server 2012",        "2012"},
>          { 6, 3, "Microsoft Windows Server 2012 R2",     "2012r2"},
> -        { 0, 0, 0},
> -        { 0, 0, 0}
> +        { },
>      }
>  };
>
>  typedef struct _ga_win_10_0_t {
>      int first_build;
> -    const char *version;
> -    const char *version_id;
> +    char const *version;
> +    char const *version_id;
>  } ga_win_10_0_t;
>
> -static const ga_win_10_0_t WIN_10_0_SERVER_VERSION_MATRIX[4] = {
> +static const ga_win_10_0_t WIN_10_0_SERVER_VERSION_MATRIX[] = {
>      {14393, "Microsoft Windows Server 2016",    "2016"},
>      {17763, "Microsoft Windows Server 2019",    "2019"},
>      {20344, "Microsoft Windows Server 2022",    "2022"},
> -    {0, 0}
> +    { }
>  };
>
> -static const ga_win_10_0_t WIN_10_0_CLIENT_VERSION_MATRIX[3] = {
> +static const ga_win_10_0_t WIN_10_0_CLIENT_VERSION_MATRIX[] = {
>      {10240, "Microsoft Windows 10",    "10"},
>      {22000, "Microsoft Windows 11",    "11"},
> -    {0, 0}
> +    { }
>  };
>
>  static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp)
> --
> 2.41.0
>
>