diff src/c/urweb.c @ 317:6a4e365db60c

Fix memory bounds checks; specialization of multi-argument polymorphic function works
author Adam Chlipala <adamc@hcoop.net>
date Thu, 11 Sep 2008 10:34:47 -0400
parents 9ad92047a499
children cd4cabaf3e52
line wrap: on
line diff
--- a/src/c/urweb.c	Thu Sep 11 10:14:59 2008 -0400
+++ b/src/c/urweb.c	Thu Sep 11 10:34:47 2008 -0400
@@ -145,13 +145,16 @@
 
 static void uw_check_heap(uw_context ctx, size_t extra) {
   if (ctx->heap_back - ctx->heap_front < extra) {
-    size_t desired = ctx->heap_back - ctx->heap_front + extra, next;
+    size_t desired = ctx->heap_back - ctx->heap + extra, next;
     char *new_heap;
 
-    for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2);
+    next = ctx->heap_back - ctx->heap;
+    if (next == 0)
+      next = 1;
+    for (; next < desired; next *= 2);
 
     new_heap = realloc(ctx->heap, next);
-    ctx->heap_front = new_heap;
+    ctx->heap_front = new_heap + (ctx->heap_back - ctx->heap_front);
     ctx->heap_back = new_heap + next;
 
     if (new_heap != ctx->heap) {
@@ -192,14 +195,17 @@
 }
 
 static void uw_check(uw_context ctx, size_t extra) {
-  size_t desired = ctx->page_back - ctx->page_front + extra, next;
+  size_t desired = ctx->page_back - ctx->page + extra, next;
   char *new_page;
 
-  for (next = ctx->page_back - ctx->page_front; next < desired; next *= 2);
+  next = ctx->page_back - ctx->page;
+  if (next == 0)
+    next = 1;
+  for (; next < desired; next *= 2);
 
   new_page = realloc(ctx->page, next);
   ctx->page_front = new_page + (ctx->page_front - ctx->page);
-  ctx->page_back = new_page + (ctx->page_back - ctx->page);
+  ctx->page_back = new_page + next;
   ctx->page = new_page;
 }