[v4,03/15] malloc/{memusage.c, memusagestat.c}: fix warn unused result

Message ID 20230428122142.928135-4-fberat@redhat.com
State Superseded
Delegated to: Siddhesh Poyarekar
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 28, 2023, 12:21 p.m. UTC
  Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 malloc/memusage.c     | 53 +++++++++++++++++++++++++++++++++----------
 malloc/memusagestat.c | 48 +++++++++++++++++++++++++++++++++++----
 2 files changed, 85 insertions(+), 16 deletions(-)
  

Comments

Siddhesh Poyarekar May 25, 2023, 12:50 a.m. UTC | #1
On 2023-04-28 08:21, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   malloc/memusage.c     | 53 +++++++++++++++++++++++++++++++++----------
>   malloc/memusagestat.c | 48 +++++++++++++++++++++++++++++++++++----
>   2 files changed, 85 insertions(+), 16 deletions(-)
> 
> diff --git a/malloc/memusage.c b/malloc/memusage.c
> index 2a3a508557..0c414b0e85 100644
> --- a/malloc/memusage.c
> +++ b/malloc/memusage.c
> @@ -18,6 +18,8 @@
>   
>   #include <assert.h>
>   #include <dlfcn.h>
> +#include <errno.h>
> +#include <error.h>
>   #include <fcntl.h>
>   #include <stdatomic.h>
>   #include <stdbool.h>
> @@ -142,6 +144,27 @@ peak_atomic_max (_Atomic size_t *peak, size_t val)
>     while (! atomic_compare_exchange_weak (peak, &v, val));
>   }
>   
> +static void
> +write_all (int fd, const void *buffer, size_t length)
> +{
> +  const char *p = buffer;
> +  const char *end = p + length;
> +  while (p < end)
> +    {
> +      ssize_t ret = write (fd, p, end - p);
> +      if (ret < 0)
> +	error (EXIT_FAILURE, errno,
> +	       "write of %zu bytes failed after %td: %m",
> +	       length, p - (const char *) buffer);
> +
> +      if (ret == 0)
> +	error (EXIT_FAILURE, 0,
> +	       "write return 0 after writing %td bytes of %zu",
> +	       p - (const char *) buffer, length);

Same as 1/15, this needs to be a translatable string.  Also "write 
returned 0 after...".

Also since this is repeated thrice, I wonder if it should get pulled out 
into its own header and included in memusage.c, memusagestat.c and gencat.c.

> +      p += ret;
> +    }
> +}
> +
>   /* Update the global data after a successful function call.  */
>   static void
>   update_data (struct header *result, size_t len, size_t old_len)
> @@ -210,10 +233,11 @@ update_data (struct header *result, size_t len, size_t old_len)
>         gettime (&buffer[idx]);
>   
>         /* Write out buffer if it is full.  */
> -      if (idx + 1 == buffer_size)
> -        write (fd, buffer, buffer_size * sizeof (struct entry));
> -      else if (idx + 1 == 2 * buffer_size)
> -        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
> +      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
> +        {
> +	  uint32_t write_size = buffer_size * sizeof (buffer[0]);
> +	  write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
> +        }
>       }
>   }
>   
> @@ -299,8 +323,8 @@ me (void)
>                 first.stack = 0;
>                 gettime (&first);
>                 /* Write it two times since we need the starting and end time. */
> -              write (fd, &first, sizeof (first));
> -              write (fd, &first, sizeof (first));
> +	      write_all (fd, &first, sizeof (first));
> +	      write_all (fd, &first, sizeof (first));
>   
>                 /* Determine the buffer size.  We use the default if the
>                    environment variable is not present.  */
> @@ -850,24 +874,29 @@ dest (void)
>     if (fd != -1)
>       {
>         /* Write the partially filled buffer.  */
> +      struct entry *start = buffer;
> +      uint32_t write_cnt = buffer_cnt;
> +
>         if (buffer_cnt > buffer_size)
> -        write (fd, buffer + buffer_size,
> -               (buffer_cnt - buffer_size) * sizeof (struct entry));
> -      else
> -        write (fd, buffer, buffer_cnt * sizeof (struct entry));
> +        {
> +          start = buffer + buffer_size;
> +          write_cnt = buffer_cnt - buffer_size;
> +        }
> +
> +      write_all (fd, start, write_cnt * sizeof (buffer[0]));
>   
>         /* Go back to the beginning of the file.  We allocated two records
>            here when we opened the file.  */
>         lseek (fd, 0, SEEK_SET);
>         /* Write out a record containing the total size.  */
>         first.stack = peak_total;
> -      write (fd, &first, sizeof (struct entry));
> +      write_all (fd, &first, sizeof (first));
>         /* Write out another record containing the maximum for heap and
>            stack.  */
>         first.heap = peak_heap;
>         first.stack = peak_stack;
>         gettime (&first);
> -      write (fd, &first, sizeof (struct entry));
> +      write_all (fd, &first, sizeof (first));
>   
>         /* Close the file.  */
>         close (fd);
> diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
> index 67c5131f79..9fe6dba2d8 100644
> --- a/malloc/memusagestat.c
> +++ b/malloc/memusagestat.c
> @@ -114,6 +114,45 @@ static int time_based;
>   static int also_total = 0;
>   
>   
> +static void
> +read_all (int fd, void *buffer, size_t length)
> +{
> +  char *p = buffer;
> +  char *end = p + length;
> +  while (p < end)
> +    {
> +      ssize_t ret = read (fd, p, end - p);
> +      if (ret < 0)
> +	error (EXIT_FAILURE, errno,
> +	       "read of %zu bytes failed after %td: %m",
> +	       length, p - (char *) buffer);
> +
> +      p += ret;
> +    }
> +}
> +
> +static void
> +write_all (int fd, const void *buffer, size_t length)
> +{
> +  const char *p = buffer;
> +  const char *end = p + length;
> +  while (p < end)
> +    {
> +      ssize_t ret = write (fd, p, end - p);
> +      if (ret < 0)
> +	error (EXIT_FAILURE, errno,
> +	       "write of %zu bytes failed after %td: %m",
> +	       length, p - (const char *) buffer);
> +
> +      if (ret == 0)
> +	error (EXIT_FAILURE, 0,
> +	       "write return 0 after writing %td bytes of %zu",
> +	       p - (const char *) buffer, length);
> +      p += ret;
> +    }
> +}
> +
> +
>   int
>   main (int argc, char *argv[])
>   {
> @@ -188,7 +227,7 @@ main (int argc, char *argv[])
>     total = st.st_size / sizeof (struct entry) - 2;
>   
>     /* Read the administrative information.  */
> -  read (fd, headent, sizeof (headent));
> +  read_all (fd, headent, sizeof (headent));
>     maxsize_heap = headent[1].heap;
>     maxsize_stack = headent[1].stack;
>     maxsize_total = headent[0].stack;
> @@ -220,7 +259,8 @@ main (int argc, char *argv[])
>   
>         /* Write the computed values in the file.  */
>         lseek (fd, 0, SEEK_SET);
> -      write (fd, headent, 2 * sizeof (struct entry));
> +      write_all (fd, headent, sizeof (headent));
> +
>       }
>   
>     if (also_total)
> @@ -372,7 +412,7 @@ main (int argc, char *argv[])
>             size_t new[2];
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          read_all (fd, &entry, sizeof (entry));
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>   
> @@ -455,7 +495,7 @@ main (int argc, char *argv[])
>             size_t xpos;
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          read_all (fd, &entry, sizeof (entry));
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>             xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
  

Patch

diff --git a/malloc/memusage.c b/malloc/memusage.c
index 2a3a508557..0c414b0e85 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -18,6 +18,8 @@ 
 
 #include <assert.h>
 #include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
 #include <fcntl.h>
 #include <stdatomic.h>
 #include <stdbool.h>
@@ -142,6 +144,27 @@  peak_atomic_max (_Atomic size_t *peak, size_t val)
   while (! atomic_compare_exchange_weak (peak, &v, val));
 }
 
+static void
+write_all (int fd, const void *buffer, size_t length)
+{
+  const char *p = buffer;
+  const char *end = p + length;
+  while (p < end)
+    {
+      ssize_t ret = write (fd, p, end - p);
+      if (ret < 0)
+	error (EXIT_FAILURE, errno,
+	       "write of %zu bytes failed after %td: %m",
+	       length, p - (const char *) buffer);
+
+      if (ret == 0)
+	error (EXIT_FAILURE, 0,
+	       "write return 0 after writing %td bytes of %zu",
+	       p - (const char *) buffer, length);
+      p += ret;
+    }
+}
+
 /* Update the global data after a successful function call.  */
 static void
 update_data (struct header *result, size_t len, size_t old_len)
@@ -210,10 +233,11 @@  update_data (struct header *result, size_t len, size_t old_len)
       gettime (&buffer[idx]);
 
       /* Write out buffer if it is full.  */
-      if (idx + 1 == buffer_size)
-        write (fd, buffer, buffer_size * sizeof (struct entry));
-      else if (idx + 1 == 2 * buffer_size)
-        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
+      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
+        {
+	  uint32_t write_size = buffer_size * sizeof (buffer[0]);
+	  write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
+        }
     }
 }
 
@@ -299,8 +323,8 @@  me (void)
               first.stack = 0;
               gettime (&first);
               /* Write it two times since we need the starting and end time. */
-              write (fd, &first, sizeof (first));
-              write (fd, &first, sizeof (first));
+	      write_all (fd, &first, sizeof (first));
+	      write_all (fd, &first, sizeof (first));
 
               /* Determine the buffer size.  We use the default if the
                  environment variable is not present.  */
@@ -850,24 +874,29 @@  dest (void)
   if (fd != -1)
     {
       /* Write the partially filled buffer.  */
+      struct entry *start = buffer;
+      uint32_t write_cnt = buffer_cnt;
+
       if (buffer_cnt > buffer_size)
-        write (fd, buffer + buffer_size,
-               (buffer_cnt - buffer_size) * sizeof (struct entry));
-      else
-        write (fd, buffer, buffer_cnt * sizeof (struct entry));
+        {
+          start = buffer + buffer_size;
+          write_cnt = buffer_cnt - buffer_size;
+        }
+
+      write_all (fd, start, write_cnt * sizeof (buffer[0]));
 
       /* Go back to the beginning of the file.  We allocated two records
          here when we opened the file.  */
       lseek (fd, 0, SEEK_SET);
       /* Write out a record containing the total size.  */
       first.stack = peak_total;
-      write (fd, &first, sizeof (struct entry));
+      write_all (fd, &first, sizeof (first));
       /* Write out another record containing the maximum for heap and
          stack.  */
       first.heap = peak_heap;
       first.stack = peak_stack;
       gettime (&first);
-      write (fd, &first, sizeof (struct entry));
+      write_all (fd, &first, sizeof (first));
 
       /* Close the file.  */
       close (fd);
diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
index 67c5131f79..9fe6dba2d8 100644
--- a/malloc/memusagestat.c
+++ b/malloc/memusagestat.c
@@ -114,6 +114,45 @@  static int time_based;
 static int also_total = 0;
 
 
+static void
+read_all (int fd, void *buffer, size_t length)
+{
+  char *p = buffer;
+  char *end = p + length;
+  while (p < end)
+    {
+      ssize_t ret = read (fd, p, end - p);
+      if (ret < 0)
+	error (EXIT_FAILURE, errno,
+	       "read of %zu bytes failed after %td: %m",
+	       length, p - (char *) buffer);
+
+      p += ret;
+    }
+}
+
+static void
+write_all (int fd, const void *buffer, size_t length)
+{
+  const char *p = buffer;
+  const char *end = p + length;
+  while (p < end)
+    {
+      ssize_t ret = write (fd, p, end - p);
+      if (ret < 0)
+	error (EXIT_FAILURE, errno,
+	       "write of %zu bytes failed after %td: %m",
+	       length, p - (const char *) buffer);
+
+      if (ret == 0)
+	error (EXIT_FAILURE, 0,
+	       "write return 0 after writing %td bytes of %zu",
+	       p - (const char *) buffer, length);
+      p += ret;
+    }
+}
+
+
 int
 main (int argc, char *argv[])
 {
@@ -188,7 +227,7 @@  main (int argc, char *argv[])
   total = st.st_size / sizeof (struct entry) - 2;
 
   /* Read the administrative information.  */
-  read (fd, headent, sizeof (headent));
+  read_all (fd, headent, sizeof (headent));
   maxsize_heap = headent[1].heap;
   maxsize_stack = headent[1].stack;
   maxsize_total = headent[0].stack;
@@ -220,7 +259,8 @@  main (int argc, char *argv[])
 
       /* Write the computed values in the file.  */
       lseek (fd, 0, SEEK_SET);
-      write (fd, headent, 2 * sizeof (struct entry));
+      write_all (fd, headent, sizeof (headent));
+
     }
 
   if (also_total)
@@ -372,7 +412,7 @@  main (int argc, char *argv[])
           size_t new[2];
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          read_all (fd, &entry, sizeof (entry));
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
 
@@ -455,7 +495,7 @@  main (int argc, char *argv[])
           size_t xpos;
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          read_all (fd, &entry, sizeof (entry));
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
           xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;