diff --git a/malloc/memusage.c b/malloc/memusage.c
index 2a3a508557..6251e039b0 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>
@@ -210,10 +212,12 @@ 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]);
+          if (write (fd, &buffer[idx + 1 - buffer_size], write_size) < write_size)
+              error(EXIT_FAILURE, errno, "cannot write buffer");
+        }
     }
 }
 
@@ -299,8 +303,10 @@ 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));
+              if (write (fd, &first, sizeof (first)) < sizeof (first))
+                error(EXIT_FAILURE, errno, "cannot write entry");
+              if (write (fd, &first, sizeof (first)) < sizeof (first))
+                error(EXIT_FAILURE, errno, "cannot write entry");
 
               /* Determine the buffer size.  We use the default if the
                  environment variable is not present.  */
@@ -850,24 +856,32 @@ 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;
+        }
+
+      if (write(fd, start, write_cnt * sizeof (buffer[0])) < sizeof (buffer[0]))
+        error(EXIT_FAILURE, errno, "cannot write buffer");
 
       /* 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));
+      if (write (fd, &first, sizeof (first)) < sizeof (first))
+        error(EXIT_FAILURE, errno, "cannot write 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));
+      if (write (fd, &first, sizeof (first)) < sizeof (first))
+        error(EXIT_FAILURE, errno, "cannot write first");
 
       /* Close the file.  */
       close (fd);
diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
index 67c5131f79..3853f00b9b 100644
--- a/malloc/memusagestat.c
+++ b/malloc/memusagestat.c
@@ -188,7 +188,8 @@ main (int argc, char *argv[])
   total = st.st_size / sizeof (struct entry) - 2;
 
   /* Read the administrative information.  */
-  read (fd, headent, sizeof (headent));
+  if (read (fd, headent, sizeof (headent)) < sizeof (headent))
+    error(EXIT_FAILURE, errno, "cannot read entry header");
   maxsize_heap = headent[1].heap;
   maxsize_stack = headent[1].stack;
   maxsize_total = headent[0].stack;
@@ -220,7 +221,9 @@ main (int argc, char *argv[])
 
       /* Write the computed values in the file.  */
       lseek (fd, 0, SEEK_SET);
-      write (fd, headent, 2 * sizeof (struct entry));
+      if (write (fd, headent, sizeof (headent)) < sizeof (headent))
+        error(EXIT_FAILURE, errno, "cannot write entries");
+
     }
 
   if (also_total)
@@ -372,7 +375,8 @@ main (int argc, char *argv[])
           size_t new[2];
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          if (read (fd, &entry, sizeof (entry)) < sizeof (entry))
+            error(EXIT_FAILURE, errno, "cannot read entry");
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
 
@@ -455,7 +459,8 @@ main (int argc, char *argv[])
           size_t xpos;
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          if (read (fd, &entry, sizeof (entry)) < sizeof (entry))
+            error(EXIT_FAILURE, errno, "cannot read entry");
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
           xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
