Skip to contentSkip to conversation

Error check find_ram_offset

PATCH Applied Alex Williamson 3 participants 3 messages Oct 31, 2011 – Nov 1, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Error check find_ram_offset

Spotted via code review, we initialize offset to 0 to avoid a compiler warning, but in the unlikely case that offset is never set to something else, we should abort instead of return a value that will almost certainly cause problems.

---

 exec.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
exec.c +9 -2
index 9dc4edb..70f6fb8 100644
@@ -2874,7 +2874,7 @@ static void *file_ram_alloc(RAMBlock *block,
 static ram_addr_t find_ram_offset(ram_addr_t size)
 {
     RAMBlock *block, *next_block;
-    ram_addr_t offset = 0, mingap = RAM_ADDR_MAX;
+    ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
 
     if (QLIST_EMPTY(&ram_list.blocks))
         return 0;
@@ -2890,10 +2890,17 @@ static ram_addr_t find_ram_offset(ram_addr_t size)
             }
         }
         if (next - end >= size && next - end < mingap) {
-            offset =  end;
+            offset = end;
             mingap = next - end;
         }
     }
+
+    if (offset == RAM_ADDR_MAX) {
+        fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
+                (uint64_t)size);
+        abort();
+    }
+
     return offset;
 }
 
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Alex Williamson

Spotted via code review, we initialize offset to 0 to avoid a compiler warning, but in the unlikely case that offset is never set to something else, we should abort instead of return a value that will almost certainly cause problems.

Compiler warning pointed to the problem until commit 09d7ae90 "Fix warning about uninitialized variable" papered over it.

Quoted 27 lines

---

 exec.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
exec.c +2 -2
index 9dc4edb..70f6fb8 100644
@@ -2874,7 +2874,7 @@ static void *file_ram_alloc(RAMBlock *block,
 static ram_addr_t find_ram_offset(ram_addr_t size)
 {
     RAMBlock *block, *next_block;
-    ram_addr_t offset = 0, mingap = RAM_ADDR_MAX;
+    ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
 
     if (QLIST_EMPTY(&ram_list.blocks))
         return 0;
@@ -2890,10 +2890,17 @@ static ram_addr_t find_ram_offset(ram_addr_t size)
             }
         }
         if (next - end >= size && next - end < mingap) {
-            offset =  end;
+            offset = end;
             mingap = next - end;
         }

exec.c static ram_addr_t find_ram_offset(ram_addr_t size) · line 2905

Quoted 10 lines
exec.c +7 -0
     }
+
+    if (offset == RAM_ADDR_MAX) {
+        fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
+                (uint64_t)size);
+        abort();
+    }
+
     return offset;
 }

The loop can't yield offset RAM_ADDR_MAX, because size needs to be zero for next - end >= size to succeed, and that's not possible.

#2 lore raw
In reply to Alex Williamson
Quoted 6 lines · Alex Williamson

Spotted via code review, we initialize offset to 0 to avoid a compiler warning, but in the unlikely case that offset is never set to something else, we should abort instead of return a value that will almost certainly cause problems.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 40 lines

---

  exec.c |   11 +++++++++--
  1 files changed, 9 insertions(+), 2 deletions(-)
exec.c +9 -2
index 9dc4edb..70f6fb8 100644
@@ -2874,7 +2874,7 @@ static void *file_ram_alloc(RAMBlock *block,
  static ram_addr_t find_ram_offset(ram_addr_t size)
  {
      RAMBlock *block, *next_block;
-    ram_addr_t offset = 0, mingap = RAM_ADDR_MAX;
+    ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
 
      if (QLIST_EMPTY(&ram_list.blocks))
          return 0;
@@ -2890,10 +2890,17 @@ static ram_addr_t find_ram_offset(ram_addr_t size)
              }
          }
          if (next - end>= size&&  next - end<  mingap) {
-            offset =  end;
+            offset = end;
              mingap = next - end;
          }
      }
+
+    if (offset == RAM_ADDR_MAX) {
+        fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
+                (uint64_t)size);
+        abort();
+    }
+
      return offset;
  }