This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gold patch] Fix gold to detect truncated binaries (PR gold/13288)


This patch fixes gold so that it detects truncated binaries. With
--no-map-whole-files, the existing checks worked fine, but with
--map-whole-files, we don't check the limits and (almost) always
return the whole file view. I moved the check from make_view() into
find_or_make_view(), and added a couple of asserts in find_view and
make_view.

Tested on x86_64, and with the test case in PR 13288.

-cary


2011-10-12  Cary Coutant  <ccoutant@google.com>

	PR gold/13288
	* gold/fileread.cc (File_read::find_view): Add assert.
	(File_read::make_view): Move bounds check (replace with assert)...
	(File_read::find_or_make_view): ... to here.


commit bde394dd32e66a0aab61e4ab38caff28098c686c
Author: Cary Coutant <ccoutant@google.com>
Date:   Wed Oct 12 16:16:02 2011 -0700

    Move check for view within the file (PR 13288).

diff --git a/gold/fileread.cc b/gold/fileread.cc
index 80ddfbc..c5dc320 100644
--- a/gold/fileread.cc
+++ b/gold/fileread.cc
@@ -329,6 +329,10 @@ inline File_read::View*
 File_read::find_view(off_t start, section_size_type size,
 		     unsigned int byteshift, File_read::View** vshifted) const
 {
+  gold_assert(start <= this->size_
+	      && (static_cast<unsigned long long>(size)
+		  <= static_cast<unsigned long long>(this->size_ - start)));
+
   if (vshifted != NULL)
     *vshifted = NULL;

@@ -456,16 +460,9 @@ File_read::make_view(off_t start, section_size_type size,
 		     unsigned int byteshift, bool cache)
 {
   gold_assert(size > 0);
-
-  // Check that start and end of the view are within the file.
-  if (start > this->size_
-      || (static_cast<unsigned long long>(size)
-          > static_cast<unsigned long long>(this->size_ - start)))
-    gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
-                 "size of file; the file may be corrupt"),
-		   this->filename().c_str(),
-		   static_cast<long long>(size),
-		   static_cast<long long>(start));
+  gold_assert(start <= this->size_
+	      && (static_cast<unsigned long long>(size)
+		  <= static_cast<unsigned long long>(this->size_ - start)));

   off_t poff = File_read::page_offset(start);

@@ -523,6 +520,16 @@ File_read::View*
 File_read::find_or_make_view(off_t offset, off_t start,
 			     section_size_type size, bool aligned, bool cache)
 {
+  // Check that start and end of the view are within the file.
+  if (start > this->size_
+      || (static_cast<unsigned long long>(size)
+          > static_cast<unsigned long long>(this->size_ - start)))
+    gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
+                 "size of file; the file may be corrupt"),
+		   this->filename().c_str(),
+		   static_cast<long long>(size),
+		   static_cast<long long>(start));
+
   unsigned int byteshift;
   if (offset == 0)
     byteshift = 0;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]