58725c
--- binutils.orig/bfd/dwarf2.c	2018-05-15 17:25:27.661131255 +0100
58725c
+++ binutils-2.27/bfd/dwarf2.c	2018-05-15 17:38:16.900598281 +0100
58725c
@@ -606,14 +606,24 @@ read_8_bytes (bfd *abfd, bfd_byte *buf,
58725c
 }
58725c
 
58725c
 static bfd_byte *
58725c
-read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
58725c
-	      bfd_byte *buf,
58725c
-	      bfd_byte *end,
58725c
-	      unsigned int size ATTRIBUTE_UNUSED)
58725c
+read_n_bytes (bfd_byte *           buf,
58725c
+	      bfd_byte *           end,
58725c
+	      struct dwarf_block * block)
58725c
 {
58725c
-  if (buf + size > end)
58725c
-    return NULL;
58725c
-  return buf;
58725c
+  unsigned int  size = block->size;
58725c
+  bfd_byte *    block_end = buf + size;
58725c
+
58725c
+  if (block_end > end || block_end < buf)
58725c
+    {
58725c
+      block->data = NULL;
58725c
+      block->size = 0;
58725c
+      return end;
58725c
+    }
58725c
+  else
58725c
+    {
58725c
+      block->data = buf;
58725c
+      return block_end;
58725c
+    }
58725c
 }
58725c
 
58725c
 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
58725c
@@ -1053,8 +1063,7 @@ read_attribute_value (struct attribute *
58725c
 	return NULL;
58725c
       blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
58725c
       info_ptr += 2;
58725c
-      blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
58725c
-      info_ptr += blk->size;
58725c
+      info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
58725c
       attr->u.blk = blk;
58725c
       break;
58725c
     case DW_FORM_block4:
58725c
@@ -1064,8 +1073,7 @@ read_attribute_value (struct attribute *
58725c
 	return NULL;
58725c
       blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
58725c
       info_ptr += 4;
58725c
-      blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
58725c
-      info_ptr += blk->size;
58725c
+      info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
58725c
       attr->u.blk = blk;
58725c
       break;
58725c
     case DW_FORM_data2:
58725c
@@ -1100,8 +1108,7 @@ read_attribute_value (struct attribute *
58725c
 	return NULL;
58725c
       blk->size = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
58725c
       info_ptr += bytes_read;
58725c
-      blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
58725c
-      info_ptr += blk->size;
58725c
+      info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
58725c
       attr->u.blk = blk;
58725c
       break;
58725c
     case DW_FORM_block1:
58725c
@@ -1111,8 +1118,7 @@ read_attribute_value (struct attribute *
58725c
 	return NULL;
58725c
       blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
58725c
       info_ptr += 1;
58725c
-      blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
58725c
-      info_ptr += blk->size;
58725c
+      info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
58725c
       attr->u.blk = blk;
58725c
       break;
58725c
     case DW_FORM_data1: