comparison src/c/urweb.c @ 321:cd4cabaf3e52

Fix memory management bug
author Adam Chlipala <adamc@hcoop.net>
date Thu, 11 Sep 2008 12:12:22 -0400
parents 6a4e365db60c
children 5030e909fbf3
comparison
equal deleted inserted replaced
320:132416711463 321:cd4cabaf3e52
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 + extra, next; 148 size_t desired = ctx->heap_front - ctx->heap + extra, next;
149 char *new_heap; 149 char *new_heap;
150 150
151 next = ctx->heap_back - ctx->heap; 151 next = ctx->heap_back - ctx->heap;
152 if (next == 0) 152 if (next == 0)
153 next = 1; 153 next = 1;
154 for (; next < desired; next *= 2); 154 for (; next < desired; next *= 2);
155 155
156 new_heap = realloc(ctx->heap, next); 156 new_heap = realloc(ctx->heap, next);
157 ctx->heap_front = new_heap + (ctx->heap_back - ctx->heap_front); 157 ctx->heap_front = new_heap + (ctx->heap_front - ctx->heap);
158 ctx->heap_back = new_heap + next; 158 ctx->heap_back = new_heap + next;
159 159
160 if (new_heap != ctx->heap) { 160 if (new_heap != ctx->heap) {
161 ctx->heap = new_heap; 161 ctx->heap = new_heap;
162 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");
193 int uw_send(uw_context ctx, int sock) { 193 int uw_send(uw_context ctx, int sock) {
194 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);
195 } 195 }
196 196
197 static void uw_check(uw_context ctx, size_t extra) { 197 static void uw_check(uw_context ctx, size_t extra) {
198 size_t desired = ctx->page_back - ctx->page + extra, next; 198 size_t desired = ctx->page_front - ctx->page + extra, next;
199 char *new_page; 199 char *new_page;
200 200
201 next = ctx->page_back - ctx->page; 201 next = ctx->page_back - ctx->page;
202 if (next == 0) 202 if (next == 0)
203 next = 1; 203 next = 1;
576 ctx->heap_front = s2; 576 ctx->heap_front = s2;
577 return r; 577 return r;
578 } 578 }
579 579
580 void uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) { 580 void uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
581 uw_check(ctx, strlen(s) * 5); 581 uw_check(ctx, strlen(s) * 6);
582 582
583 for (; *s; s++) { 583 for (; *s; s++) {
584 char c = *s; 584 char c = *s;
585 585
586 switch (c) { 586 switch (c) {