[2/8] catgets/gencat.c: fix warn unused result

Message ID 20230418121130.844302-3-fberat@redhat.com
State Superseded
Headers
Series Fix warn unused result |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Frederic Berat April 18, 2023, 12:11 p.m. UTC
  Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 catgets/gencat.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
  

Comments

Siddhesh Poyarekar April 18, 2023, 12:36 p.m. UTC | #1
On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   catgets/gencat.c | 34 +++++++++++++++++++++++-----------
>   1 file changed, 23 insertions(+), 11 deletions(-)
> 
> diff --git a/catgets/gencat.c b/catgets/gencat.c
> index 61ac797349..ecfdaa6e6d 100644
> --- a/catgets/gencat.c
> +++ b/catgets/gencat.c
> @@ -852,6 +852,7 @@ write_out (struct catalog *catalog, const char *output_name,
>     const char *strings;
>     size_t strings_size;
>     uint32_t *array1, *array2;
> +  uint32_t array_size;
>     size_t cnt;
>     int fd;
>   
> @@ -927,12 +928,11 @@ write_out (struct catalog *catalog, const char *output_name,
>     obj.plane_size = best_size;
>     obj.plane_depth = best_depth;
>   
> +  array_size = best_size * best_depth * sizeof (uint32_t) * 3;

We use c99 by default, so use declare and define here, no need for 
declarations at the top.

>     /* Allocate room for all needed arrays.  */
> -  array1 =
> -    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> -  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
> -  array2
> -    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> +  array1 = (uint32_t *) alloca (array_size);
> +  memset (array1, '\0', array_size);
> +  array2 = (uint32_t *) alloca (array_size);

That's a good cleanup.

>     obstack_init (&string_pool);
>   
>     set_run = catalog->all_sets;
> @@ -985,22 +985,34 @@ write_out (struct catalog *catalog, const char *output_name,
>       }
>   
>     /* Write out header.  */
> -  write (fd, &obj, sizeof (obj));
> +  if (write (fd, &obj, sizeof (obj)) < sizeof (obj))
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);

I wonder if this should use xwrite-like functionality, i.e. write 
iteratively until there's an error.

>   
>     /* We always write out the little endian version of the index
>        arrays.  */
>   #if __BYTE_ORDER == __LITTLE_ENDIAN
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> +  if (write (fd, array1, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
> +  if (write (fd, array2, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
>   #elif __BYTE_ORDER == __BIG_ENDIAN
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> +  if (write (fd, array2, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
> +  if (write (fd, array1, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
>   #else
>   # error Cannot handle __BYTE_ORDER byte order
>   #endif
>   
>     /* Finally write the strings.  */
> -  write (fd, strings, strings_size);
> +  if (write (fd, strings, strings_size) < strings_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
>   
>     if (fd != STDOUT_FILENO)
>       close (fd);

Likewise for all of these calls.  It probably makes sense to implement a 
static int write_all() in this file that is similar to support/xwrite.c 
and then use it in place of all of these write calls.

If this pattern is more common across glibc, perhaps putting it into 
include/unistd.h may make sense too.

Thanks,
Sid
  

Patch

diff --git a/catgets/gencat.c b/catgets/gencat.c
index 61ac797349..ecfdaa6e6d 100644
--- a/catgets/gencat.c
+++ b/catgets/gencat.c
@@ -852,6 +852,7 @@  write_out (struct catalog *catalog, const char *output_name,
   const char *strings;
   size_t strings_size;
   uint32_t *array1, *array2;
+  uint32_t array_size;
   size_t cnt;
   int fd;
 
@@ -927,12 +928,11 @@  write_out (struct catalog *catalog, const char *output_name,
   obj.plane_size = best_size;
   obj.plane_depth = best_depth;
 
+  array_size = best_size * best_depth * sizeof (uint32_t) * 3;
   /* Allocate room for all needed arrays.  */
-  array1 =
-    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
-  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
-  array2
-    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
+  array1 = (uint32_t *) alloca (array_size);
+  memset (array1, '\0', array_size);
+  array2 = (uint32_t *) alloca (array_size);
   obstack_init (&string_pool);
 
   set_run = catalog->all_sets;
@@ -985,22 +985,34 @@  write_out (struct catalog *catalog, const char *output_name,
     }
 
   /* Write out header.  */
-  write (fd, &obj, sizeof (obj));
+  if (write (fd, &obj, sizeof (obj)) < sizeof (obj))
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 
   /* We always write out the little endian version of the index
      arrays.  */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
+  if (write (fd, array1, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
+  if (write (fd, array2, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 #elif __BYTE_ORDER == __BIG_ENDIAN
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
+  if (write (fd, array2, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
+  if (write (fd, array1, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 #else
 # error Cannot handle __BYTE_ORDER byte order
 #endif
 
   /* Finally write the strings.  */
-  write (fd, strings, strings_size);
+  if (write (fd, strings, strings_size) < strings_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 
   if (fd != STDOUT_FILENO)
     close (fd);