]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - binutils/ar.c
* ar.c (open_inarch): Check fwrite return. Use size_t.
[thirdparty/binutils-gdb.git] / binutils / ar.c
index 243144894b4a71b15b944d74c542dc3651678e4d..115224221e6ddea6ad396a5d0aeb04547c4e2c4b 100644 (file)
@@ -430,7 +430,7 @@ main (int argc, char **argv)
        usage (0);
       if (strcmp (argv[1], "-V") == 0
          || strcmp (argv[1], "-v") == 0
-         || strncmp (argv[1], "--v", 3) == 0)
+         || CONST_STRNEQ (argv[1], "--v"))
        print_version ("ranlib");
       arg_index = 1;
       if (strcmp (argv[1], "-t") == 0)
@@ -777,10 +777,10 @@ open_inarch (const char *archive_filename, const char *file)
 static void
 print_contents (bfd *abfd)
 {
-  int ncopied = 0;
+  size_t ncopied = 0;
   char *cbuf = xmalloc (BUFSIZE);
   struct stat buf;
-  long size;
+  size_t size;
   if (bfd_stat_arch_elt (abfd, &buf) != 0)
     /* xgettext:c-format */
     fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
@@ -795,8 +795,8 @@ print_contents (bfd *abfd)
   while (ncopied < size)
     {
 
-      int nread;
-      int tocopy = size - ncopied;
+      size_t nread;
+      size_t tocopy = size - ncopied;
       if (tocopy > BUFSIZE)
        tocopy = BUFSIZE;
 
@@ -805,7 +805,8 @@ print_contents (bfd *abfd)
        /* xgettext:c-format */
        fatal (_("%s is not a valid archive"),
               bfd_get_filename (bfd_my_archive (abfd)));
-      fwrite (cbuf, 1, nread, stdout);
+      if (fwrite (cbuf, 1, nread, stdout) != nread)
+       fatal ("stdout: %s", strerror (errno));
       ncopied += tocopy;
     }
   free (cbuf);
@@ -826,9 +827,9 @@ extract_file (bfd *abfd)
 {
   FILE *ostream;
   char *cbuf = xmalloc (BUFSIZE);
-  int nread, tocopy;
-  long ncopied = 0;
-  long size;
+  size_t nread, tocopy;
+  size_t ncopied = 0;
+  size_t size;
   struct stat buf;
 
   if (bfd_stat_arch_elt (abfd, &buf) != 0)
@@ -836,10 +837,6 @@ extract_file (bfd *abfd)
     fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
   size = buf.st_size;
 
-  if (size < 0)
-    /* xgettext:c-format */
-    fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd));
-
   if (verbose)
     printf ("x - %s\n", bfd_get_filename (abfd));
 
@@ -888,7 +885,8 @@ extract_file (bfd *abfd)
 
            output_file = ostream;
          }
-       fwrite (cbuf, 1, nread, ostream);
+       if (fwrite (cbuf, 1, nread, ostream) != nread)
+         fatal ("%s: %s", output_filename, strerror (errno));
        ncopied += tocopy;
       }
 
@@ -922,6 +920,9 @@ write_archive (bfd *iarch)
   strcpy (old_name, bfd_get_filename (iarch));
   new_name = make_tempname (old_name);
 
+  if (new_name == NULL)
+    bfd_fatal ("could not create temporary file whilst writing archive");
+  
   output_filename = new_name;
 
   obfd = bfd_openw (new_name, bfd_get_target (iarch));