comparison 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
comparison
equal deleted inserted replaced
316:04ebfe929a98 317:6a4e365db60c
143 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]); 143 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
144 } 144 }
145 145
146 static void uw_check_heap(uw_context ctx, size_t extra) { 146 static void uw_check_heap(uw_context ctx, size_t extra) {
147 if (ctx->heap_back - ctx->heap_front < extra) { 147 if (ctx->heap_back - ctx->heap_front < extra) {
148 size_t desired = ctx->heap_back - ctx->heap_front + extra, next; 148 size_t desired = ctx->heap_back - ctx->heap + extra, next;
149 char *new_heap; 149 char *new_heap;
150 150
151 for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2); 151 next = ctx->heap_back - ctx->heap;
152 if (next == 0)
153 next = 1;
154 for (; next < desired; next *= 2);
152 155
153 new_heap = realloc(ctx->heap, next); 156 new_heap = realloc(ctx->heap, next);
154 ctx->heap_front = new_heap; 157 ctx->heap_front = new_heap + (ctx->heap_back - ctx->heap_front);
155 ctx->heap_back = new_heap + next; 158 ctx->heap_back = new_heap + next;
156 159
157 if (new_heap != ctx->heap) { 160 if (new_heap != ctx->heap) {
158 ctx->heap = new_heap; 161 ctx->heap = new_heap;
159 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously"); 162 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously");
190 int uw_send(uw_context ctx, int sock) { 193 int uw_send(uw_context ctx, int sock) {
191 return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page); 194 return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page);
192 } 195 }
193 196
194 static void uw_check(uw_context ctx, size_t extra) { 197 static void uw_check(uw_context ctx, size_t extra) {
195 size_t desired = ctx->page_back - ctx->page_front + extra, next; 198 size_t desired = ctx->page_back - ctx->page + extra, next;
196 char *new_page; 199 char *new_page;
197 200
198 for (next = ctx->page_back - ctx->page_front; next < desired; next *= 2); 201 next = ctx->page_back - ctx->page;
202 if (next == 0)
203 next = 1;
204 for (; next < desired; next *= 2);
199 205
200 new_page = realloc(ctx->page, next); 206 new_page = realloc(ctx->page, next);
201 ctx->page_front = new_page + (ctx->page_front - ctx->page); 207 ctx->page_front = new_page + (ctx->page_front - ctx->page);
202 ctx->page_back = new_page + (ctx->page_back - ctx->page); 208 ctx->page_back = new_page + next;
203 ctx->page = new_page; 209 ctx->page = new_page;
204 } 210 }
205 211
206 static void uw_writec_unsafe(uw_context ctx, char c) { 212 static void uw_writec_unsafe(uw_context ctx, char c) {
207 *(ctx->page_front)++ = c; 213 *(ctx->page_front)++ = c;