import gdb-2000-02-02 snapshot
[external/binutils.git] / bfd / libbfd.c
index d2baa5b..fb833f6 100644 (file)
@@ -274,7 +274,10 @@ bfd_read (ptr, size, nitems, abfd)
       get = size * nitems;
       if (abfd->where + get > bim->size)
        {
-         get = bim->size - abfd->where;
+         if (bim->size < abfd->where)
+           get = 0;
+         else
+           get = bim->size - abfd->where;
          bfd_set_error (bfd_error_file_truncated);
        }
       memcpy (ptr, bim->buffer + abfd->where, get);
@@ -293,7 +296,7 @@ bfd_read (ptr, size, nitems, abfd)
 
      A BFD backend may wish to override bfd_error_file_truncated to
      provide something more useful (eg. no_symbols or wrong_format).  */
-  if (nread < (int)(size * nitems))
+  if (nread != (int) (size * nitems))
     {
       if (ferror (bfd_cache_lookup (abfd)))
        bfd_set_error (bfd_error_system_call);
@@ -677,10 +680,22 @@ bfd_seek (abfd, position, direction)
 
   if ((abfd->flags & BFD_IN_MEMORY) != 0)
     {
+      struct bfd_in_memory *bim;
+
+      bim = (struct bfd_in_memory *) abfd->iostream;
+      
       if (direction == SEEK_SET)
        abfd->where = position;
       else
        abfd->where += position;
+      
+      if (abfd->where > bim->size)
+       {
+         abfd->where = bim->size;
+         bfd_set_error (bfd_error_file_truncated);
+         return -1;
+       }
+      
       return 0;
     }
 
@@ -1166,13 +1181,20 @@ _bfd_generic_get_section_contents (abfd, section, location, offset, count)
      file_ptr offset;
      bfd_size_type count;
 {
-    if (count == 0)
-        return true;
-    if ((bfd_size_type)(offset+count) > section->_raw_size
-        || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
-        || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
-        return (false); /* on error */
-    return (true);
+  if (count == 0)
+    return true;
+
+  if ((bfd_size_type) (offset + count) > section->_raw_size)
+    {
+      bfd_set_error (bfd_error_invalid_operation);
+      return false;
+    }
+
+  if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
+      || bfd_read (location, (bfd_size_type) 1, count, abfd) != count)
+    return false;
+
+  return true;
 }
 
 boolean
@@ -1260,7 +1282,7 @@ bfd_log2 (x)
 {
   unsigned int result = 0;
 
-  while ((((bfd_vma) 1) << result) < x)
+  while ((x = (x >> 1)) != 0)
     ++result;
   return result;
 }