links

lynx-like text mode web browser
git clone anongit@rnpnr.xyz:links.git
Log | Files | Refs | Feed | README | LICENSE

Commit: b9b16fd31babca2297405acd8703da0cab5127a6
Parent: 212de293f2481e07780dc3730abc6ebb442d2ecf
Author: opask
Date:   Tue, 24 Jul 2018 19:09:14 -0600

add xrealloc replaces mem_realloc, minor style fixes

Diffstat:
Mbfu.c | 23+++++++++++++++--------
Mcache.c | 13+++++++++----
Mcharsets.c | 18+++++++++++-------
Mcompress.c | 4++--
Mconnect.c | 5+++--
Mdip.c | 2+-
Mdither.c | 6+++---
Merror.c | 20--------------------
Mfile.c | 5+++--
Mhtml.c | 38++++++++++++++++++++++++--------------
Mhtml_gr.c | 18+++++++++++++-----
Mhtml_r.c | 36+++++++++++++++++++-----------------
Mhtml_tbl.c | 44++++++++++++++++++++++++++++----------------
Mjpeg.c | 9++++-----
Mkbd.c | 5+++--
Mlinks.h | 9+--------
Mmain.c | 9+++++++++
Mos_dep.c | 30+++++++++++++++++-------------
Mselect.c | 5+++--
Mstring.c | 12+++++++-----
Mterminal.c | 27+++++++++++++++++----------
Mview.c | 51+++++++++++++++++++++++++++++++++------------------
Mview_gr.c | 12++++++++----
Mx.c | 11+++++++----
24 files changed, 240 insertions(+), 172 deletions(-)

diff --git a/bfu.c b/bfu.c @@ -46,8 +46,10 @@ void add_to_ml(struct memory_list **ml, ...) if (n == MAXINT) overalloc(); n++; } - if ((unsigned)n + (unsigned)((*ml)->n) > (MAXINT - sizeof(struct memory_list)) / sizeof(void *)) overalloc(); - nml = mem_realloc(*ml, sizeof(struct memory_list) + (n + (*ml)->n) * sizeof(void *)); + if ((unsigned)n + (unsigned)((*ml)->n) > (MAXINT - sizeof(struct memory_list)) / sizeof(void *)) + overalloc(); + nml = xrealloc(*ml, sizeof(struct memory_list) + + (n + (*ml)->n) * sizeof(void *)); va_end(ap); va_start(ap, ml); while ((q = va_arg(ap, void *))) nml->p[nml->n++] = q; @@ -811,8 +813,9 @@ void add_to_menu(struct menu_item **mi, unsigned char *text, unsigned char *rtex } else { for (n = 0; (*mi)[n].text; n++) if (n == MAXINT) overalloc(); } - if (((unsigned)n + 2) > MAXINT / sizeof(struct menu_item)) overalloc(); - mii = mem_realloc(*mi, (n + 2) * sizeof(struct menu_item)); + if (((unsigned)n + 2) > MAXINT / sizeof(struct menu_item)) + overalloc(); + mii = xrealloc(*mi, (n + 2) * sizeof(struct menu_item)); *mi = mii; memcpy(mii + n + 1, mii + n, sizeof(struct menu_item)); mii[n].text = text; @@ -1204,8 +1207,11 @@ static void do_tab_compl(struct terminal *term, struct list_head *history, struc unsigned char *s = dlg_get_history_string(term, hi, MAXINT); if (!strncmp(cast_const_char cdata, cast_const_char s, l)) { if (!(n & (ALLOC_GR - 1))) { - if ((unsigned)n > MAXINT / sizeof(struct menu_item) - ALLOC_GR - 1) overalloc(); - items = mem_realloc(items, (n + ALLOC_GR + 1) * sizeof(struct menu_item)); + if ((unsigned)n > MAXINT / sizeof(struct menu_item) - ALLOC_GR - 1) + overalloc(); + items = xrealloc(items, + (n + ALLOC_GR + 1) + * sizeof(struct menu_item)); } items[n].text = s; items[n].rtext = cast_uchar ""; @@ -2233,8 +2239,9 @@ void msg_box(struct terminal *term, struct memory_list *ml, unsigned char *title do { text = va_arg(ap, unsigned char *); udatan++; - if ((unsigned)udatan > MAXINT / sizeof(unsigned char *)) overalloc(); - udata = mem_realloc(udata, udatan * sizeof(unsigned char *)); + if ((unsigned)udatan > MAXINT / sizeof(unsigned char *)) + overalloc(); + udata = xrealloc(udata, udatan * sizeof(unsigned char *)); udata[udatan - 1] = text; } while (text); udata2 = va_arg(ap, void *); diff --git a/cache.c b/cache.c @@ -262,7 +262,9 @@ have_f: while (f->list_entry.next != &e->frag && f->offset + f->length > list_struct(f->list_entry.next, struct fragment)->offset) { struct fragment *next = list_struct(f->list_entry.next, struct fragment); if (f->offset + f->length < next->offset + next->length) { - f = mem_realloc(f, sizeof(struct fragment) + (size_t)(next->offset - f->offset + next->length)); + f = xrealloc(f, sizeof(struct fragment) + + (size_t)(next->offset - f->offset + + next->length)); fix_list_after_realloc(f); if (memcmp(f->data + next->offset - f->offset, next->data, (size_t)(f->offset + f->length - next->offset))) trunc = 1; memcpy(f->data + f->length, next->data + f->offset + f->length - next->offset, (size_t)(next->offset + next->length - f->offset - f->length)); @@ -304,7 +306,8 @@ int defrag_entry(struct cache_entry *e) } if (g == f->list_entry.next) { if (f->length != f->real_length) { - f = mem_realloc_mayfail(f, sizeof(struct fragment) + (size_t)f->length); + f = xrealloc(f, sizeof(struct fragment) + + (size_t)f->length); if (f) { f->real_length = f->length; fix_list_after_realloc(f); @@ -354,7 +357,8 @@ void truncate_entry(struct cache_entry *e, off_t off, int final) sf(-(f->offset + f->length - off)); f->length = off - f->offset; if (final) { - g = mem_realloc_mayfail(f, sizeof(struct fragment) + (size_t)f->length); + g = xrealloc(f, sizeof(struct fragment) + + (size_t)f->length); if (g) { f = g; fix_list_after_realloc(f); @@ -404,7 +408,8 @@ void trim_cache_entry(struct cache_entry *e) struct list_head *lf; foreach(struct fragment, f, lf, e->frag) { if (f->length != f->real_length) { - nf = mem_realloc_mayfail(f, sizeof(struct fragment) + (size_t)f->length); + nf = xrealloc(f, sizeof(struct fragment) + + (size_t)f->length); if (nf) { f = nf; fix_list_after_realloc(f); diff --git a/charsets.c b/charsets.c @@ -389,8 +389,9 @@ unsigned char *convert_string(struct conv_table *ct, unsigned char *c, int l, st put_c: buffer[bp++] = c[pp++]; if (!(bp & (ALLOC_GR - 1))) { - if ((unsigned)bp > MAXINT - ALLOC_GR) overalloc(); - buffer = mem_realloc(buffer, bp + ALLOC_GR); + if ((unsigned)bp > MAXINT - ALLOC_GR) + overalloc(); + buffer = xrealloc(buffer, bp + ALLOC_GR); } continue; } @@ -416,20 +417,23 @@ unsigned char *convert_string(struct conv_table *ct, unsigned char *c, int l, st if (!(e = get_entity_string(&c[pp + 1], i - pp - 1, dopt->cp))) goto put_c; pp = i + (i < l && c[i] == ';'); } - if (!e[0]) continue; + if (!e[0]) + continue; if (!e[1]) { buffer[bp++] = e[0]; if (!(bp & (ALLOC_GR - 1))) { - if ((unsigned)bp > MAXINT - ALLOC_GR) overalloc(); - buffer = mem_realloc(buffer, bp + ALLOC_GR); + if ((unsigned)bp > MAXINT - ALLOC_GR) + overalloc(); + buffer = xrealloc(buffer, bp + ALLOC_GR); } continue; } while (*e) { buffer[bp++] = *(e++); if (!(bp & (ALLOC_GR - 1))) { - if ((unsigned)bp > MAXINT - ALLOC_GR) overalloc(); - buffer = mem_realloc(buffer, bp + ALLOC_GR); + if ((unsigned)bp > MAXINT - ALLOC_GR) + overalloc(); + buffer = xrealloc(buffer, bp + ALLOC_GR); } } } diff --git a/compress.c b/compress.c @@ -31,7 +31,7 @@ static int decoder_memory_expand(unsigned char **p, size_t size, size_t *addsize if (add > 1) add >>= 1; else overalloc(); } - pp = mem_realloc_mayfail(*p, size + add); + pp = xrealloc(*p, size + add); if (!pp) { *addsize = 0; return -1; @@ -248,7 +248,7 @@ static int decode_gzip(struct terminal *term, struct cache_entry *ce, int defl, ce->decompressed = p; ce->decompressed_len = (unsigned char *)z.next_out - (unsigned char *)p; decompressed_cache_size += ce->decompressed_len; - ce->decompressed = mem_realloc(ce->decompressed, ce->decompressed_len); + ce->decompressed = xrealloc(ce->decompressed, ce->decompressed_len); return 0; } diff --git a/connect.c b/connect.c @@ -946,8 +946,9 @@ static void read_select(void *c_) set_handlers(rb->sock, NULL, NULL, NULL); read_more: - if ((unsigned)rb->len > MAXINT - sizeof(struct read_buffer) - READ_SIZE) overalloc(); - rb = mem_realloc(rb, sizeof(struct read_buffer) + rb->len + READ_SIZE); + if ((unsigned)rb->len > MAXINT - sizeof(struct read_buffer) - READ_SIZE) + overalloc(); + rb = xrealloc(rb, sizeof(struct read_buffer) + rb->len + READ_SIZE); c->buffer = rb; if (c->ssl) { diff --git a/dip.c b/dip.c @@ -885,7 +885,7 @@ static void decimate_3(unsigned short **data0, int x, int y) } } } - *data0=mem_realloc(*data0,futuresize); + *data0 = xrealloc(*data0, futuresize); } /* Scales color 48-bits-per-pixel bitmap. Both enlarges and diminishes. Uses diff --git a/dither.c b/dither.c @@ -761,19 +761,19 @@ static void make_16_table(int *table, int bits, int pos, float_double gamma, int static void make_red_table(int bits, int pos, int dump_t2c, int be) { - red_table = mem_realloc(red_table, 65536 * sizeof(*red_table)); + red_table = xrealloc(red_table, 65536 * sizeof(*red_table)); make_16_table(red_table,bits,pos,(float_double)display_red_gamma,dump_t2c, be); } static void make_green_table(int bits, int pos, int dump_t2c, int be) { - green_table = mem_realloc(green_table, 65536 * sizeof(*green_table)); + green_table = xrealloc(green_table, 65536 * sizeof(*green_table)); make_16_table(green_table,bits,pos,(float_double)display_green_gamma,dump_t2c, be); } static void make_blue_table(int bits, int pos,int dump_t2c, int be) { - blue_table = mem_realloc(blue_table, 65536 * sizeof(*blue_table)); + blue_table = xrealloc(blue_table, 65536 * sizeof(*blue_table)); make_16_table(blue_table,bits,pos,(float_double)display_blue_gamma, dump_t2c, be); } diff --git a/error.c b/error.c @@ -23,8 +23,6 @@ void *do_not_optimize_here(void *p) return p; } -#define heap_malloc malloc -#define heap_realloc realloc #define heap_calloc(x) calloc(1, (x)) void init_heap(void) { @@ -120,24 +118,6 @@ void *mem_calloc_(size_t size, int mayfail) return p; } -void *mem_realloc_(void *p, size_t size, int mayfail) -{ - void *np; - if (!p) - return xmalloc(size); - debug_test_free(NULL, 0); - if (!size) { - free(p); - return NULL; - } - retry: - if (!(np = heap_realloc(p, size))) { - if (out_of_memory_fl(0, !mayfail ? cast_uchar "realloc" : NULL, size, NULL, 0)) goto retry; - return NULL; - } - return np; -} - unsigned char *memacpy(const unsigned char *src, size_t len) { unsigned char *m; diff --git a/file.c b/file.c @@ -326,8 +326,9 @@ void file_func(struct connection *c) if (strspn(cast_const_char n, dir_sep('\\') ? "/\\" : "/") == strlen(cast_const_char n)) continue; } - if ((unsigned)dirl > MAXINT / sizeof(struct dirs) - 1) overalloc(); - dir = mem_realloc(dir, (dirl + 1) * sizeof(struct dirs)); + if ((unsigned)dirl > MAXINT / sizeof(struct dirs) - 1) + overalloc(); + dir = xrealloc(dir, (dirl + 1) * sizeof(struct dirs)); dir[dirl].f = stracpy(cast_uchar de->d_name); *(p = &dir[dirl++].s) = init_str(); l = 0; diff --git a/html.c b/html.c @@ -106,7 +106,7 @@ int parse_element(unsigned char *e, unsigned char *eof, unsigned char **name, in do { \ if (!((l) & (32 - 1))) { \ if ((unsigned)(l) > MAXINT - 32) overalloc(); \ - (s) = mem_realloc((s), (l) + 32); \ + (s) = xrealloc((s), (l) + 32); \ } \ (s)[(l)++] = (c); \ } while (0) @@ -2032,8 +2032,10 @@ static void new_menu_item(unsigned char *name, long data, int fullname) if (menu_stack_size && name) { top = item = menu_stack[menu_stack_size - 1]; while (item->text) item++; - if ((size_t)((unsigned char *)(item + 2) - (unsigned char *)top) > MAXINT) overalloc(); - top = mem_realloc(top, (unsigned char *)(item + 2) - (unsigned char *)top); + if ((size_t)((unsigned char *)(item + 2) - (unsigned char *)top) > MAXINT) + overalloc(); + top = xrealloc(top, (unsigned char *)(item + 2) + - (unsigned char *)top); item = item - menu_stack[menu_stack_size - 1] + top; menu_stack[menu_stack_size - 1] = top; if (menu_stack_size >= 2) { @@ -2054,8 +2056,9 @@ static void new_menu_item(unsigned char *name, long data, int fullname) } else free(name); if (name && data == -1) { - if ((unsigned)menu_stack_size > MAXINT / sizeof(struct menu_item *) - 1) overalloc(); - menu_stack = mem_realloc(menu_stack, (menu_stack_size + 1) * sizeof(struct menu_item *)); + if ((unsigned)menu_stack_size > MAXINT / sizeof(struct menu_item *) - 1) + overalloc(); + menu_stack = xrealloc(menu_stack, (menu_stack_size + 1) * sizeof(struct menu_item *)); menu_stack[menu_stack_size++] = nmenu; } if (!name) menu_stack_size--; @@ -2238,8 +2241,10 @@ static int do_html_select(unsigned char *attr, unsigned char *html, unsigned cha if (preselect == -1 && has_attr(t_attr, cast_uchar "selected")) preselect = order; v = get_exact_attr_val(t_attr, cast_uchar "value"); if (!(order & (ALLOC_GR - 1))) { - if ((unsigned)order > MAXINT / sizeof(unsigned char *) - ALLOC_GR) overalloc(); - val = mem_realloc(val, (order + ALLOC_GR) * sizeof(unsigned char *)); + if ((unsigned)order > MAXINT / sizeof(unsigned char *) - ALLOC_GR) + overalloc(); + val = xrealloc(val, (order + ALLOC_GR) + * sizeof(unsigned char *)); } val[order++] = v; if ((vx = get_attr_val(t_attr, cast_uchar "label"))) { @@ -2279,7 +2284,8 @@ static int do_html_select(unsigned char *attr, unsigned char *html, unsigned cha *end = en; if (!order) goto abort; fc = mem_calloc(sizeof(struct form_control)); - if ((unsigned)order > (unsigned)MAXINT / sizeof(unsigned char *)) overalloc(); + if ((unsigned)order > (unsigned)MAXINT / sizeof(unsigned char *)) + overalloc(); lbls = mem_calloc(order * sizeof(unsigned char *)); fc->form_num = last_form_tag ? (int)(last_form_tag - startf) : 0; fc->ctrl_num = last_form_tag ? (int)(attr - last_form_tag) : (int)(attr - startf); @@ -2476,8 +2482,9 @@ static void parse_frame_widths(unsigned char *a, int ww, int www, int **op, int if (*a == '%') q = q * ww / 100; else if (*a != '*') q = (q + (www - 1) / 2) / (www ? www : 1); else if (!(q = -q)) q = -1; - if ((unsigned)ol > MAXINT / sizeof(int) - 1) overalloc(); - o = mem_realloc(o, (ol + 1) * sizeof(int)); + if ((unsigned)ol > MAXINT / sizeof(int) - 1) + overalloc(); + o = xrealloc(o, (ol + 1) * sizeof(int)); o[ol++] = q; if ((aa = cast_uchar strchr(cast_const_char a, ','))) { a = aa + 1; @@ -2517,7 +2524,8 @@ static void parse_frame_widths(unsigned char *a, int ww, int www, int **op, int int nn = 0; for (i = 0; i < ol; i++) if (o[i] < 0) nn = 1; if (!nn) goto distribute; - if ((unsigned)ol > MAXINT / sizeof(int)) overalloc(); + if ((unsigned)ol > MAXINT / sizeof(int)) + overalloc(); oo = xmalloc(ol * sizeof(int)); memcpy(oo, o, ol * sizeof(int)); for (i = 0; i < ol; i++) if (o[i] < 1) o[i] = 1; @@ -2605,8 +2613,10 @@ static void html_frameset(unsigned char *a) continue; } if ((w = parse_width(d, 1)) != -1) { - if ((unsigned)fp->n > (MAXINT - sizeof(struct frameset_param)) / sizeof(int) - 1) overalloc(); - fp = mem_realloc(fp, sizeof(struct frameset_param) + (fp->n + 1) * sizeof(int)); + if ((unsigned)fp->n > (MAXINT - sizeof(struct frameset_param)) / sizeof(int) - 1) + overalloc(); + fp = xrealloc(fp, sizeof(struct frameset_param) + + (fp->n + 1) * sizeof(int)); fp->width[fp->n++] = w; } if (!(d = cast_uchar strchr(cast_const_char d, ','))) break; @@ -3354,7 +3364,7 @@ int get_image_map(unsigned char *head, unsigned char *s, unsigned char *eof, uns } if ((unsigned)nmenu > MAXINT / sizeof(struct menu_item) - 2) overalloc(); - nm = mem_realloc(*menu, (nmenu + 2) * sizeof(struct menu_item)); + nm = xrealloc(*menu, (nmenu + 2) * sizeof(struct menu_item)); *menu = nm; memset(&nm[nmenu], 0, 2 * sizeof(struct menu_item)); nm[nmenu].text = label; diff --git a/html_gr.c b/html_gr.c @@ -190,7 +190,9 @@ void flush_pending_line_to_obj(struct g_part *p, int minheight) if (!(a->n_lines & (a->n_lines + 1))) { if ((unsigned)a->n_lines > ((MAXINT - sizeof(struct g_object_area)) / sizeof(struct g_object_text *) - 1) / 2) overalloc(); - a = mem_realloc(a, sizeof(struct g_object_area) + sizeof(struct g_object_text *) * (a->n_lines * 2 + 1)); + a = xrealloc(a, sizeof(struct g_object_area) + + sizeof(struct g_object_text *) + * (a->n_lines * 2 + 1)); p->root = a; } a->lines[a->n_lines++] = l; @@ -229,7 +231,9 @@ void add_object_to_line(struct g_part *pp, struct g_object_line **lp, struct g_o (*lp)->n_entries++; if ((unsigned)(*lp)->n_entries > (MAXINT - sizeof(struct g_object_line)) / sizeof(struct g_object *)) overalloc(); - l = mem_realloc(*lp, sizeof(struct g_object_line) + sizeof(struct g_object *) * (*lp)->n_entries); + l = xrealloc(*lp, sizeof(struct g_object_line) + + sizeof(struct g_object *) + * (*lp)->n_entries); *lp = l; } l->entries[l->n_entries - 1] = go; @@ -549,7 +553,9 @@ static void do_image(struct g_part *p, struct image_description *im) continue; if ((unsigned)map->n_areas > (MAXINT - sizeof(struct image_map)) / sizeof(struct map_area) - 1) overalloc(); - map = mem_realloc(map, sizeof(struct image_map) + (map->n_areas + 1) * sizeof(struct map_area)); + map = xrealloc(map, sizeof(struct image_map) + + (map->n_areas + 1) + * sizeof(struct map_area)); a = &map->area[map->n_areas++]; a->shape = shape; a->coords = NULL; @@ -576,7 +582,8 @@ static void do_image(struct g_part *p, struct image_description *im) num = num * d_opt->image_scale / 100; if ((unsigned)a->ncoords > MAXINT / sizeof(int) - 1) overalloc(); - a->coords = mem_realloc(a->coords, (a->ncoords + 1) * sizeof(int)); + a->coords = xrealloc(a->coords, + (a->ncoords + 1) * sizeof(int)); a->coords[a->ncoords++] = num; goto next_coord; } @@ -842,7 +849,8 @@ static void g_put_chars(void *p_, unsigned char *s, int l) if ((unsigned)l > MAXINT || (unsigned)ptl + (unsigned)l > MAXINT - ALLOC_GR) overalloc(); - t = mem_realloc(p->text, sizeof(struct g_object_text) + ((ptl + l + ALLOC_GR - 1) & ~(ALLOC_GR - 1))); + t = xrealloc(p->text, sizeof(struct g_object_text) + + ((ptl + l + ALLOC_GR - 1) & ~(ALLOC_GR - 1))); if (p->w.last_wrap >= p->text->text && p->w.last_wrap < p->text->text + p->pending_text_len) p->w.last_wrap = p->w.last_wrap + ((unsigned char *)t - (unsigned char *)p->text); if (p->w.last_wrap_obj == &p->text->goti.go) diff --git a/html_r.c b/html_r.c @@ -244,16 +244,12 @@ static void xpand_lines(struct part *p, int y) if ((y ^ p->data->y) > p->data->y) { unsigned s; for (s = 1; s < (unsigned)y; s = s * 2 + 1) { - if (s > MAXINT / sizeof(struct line)) overalloc(); + if (s > MAXINT / sizeof(struct line)) + overalloc(); } - p->data->data = mem_realloc(p->data->data, s * sizeof(struct line)); + p->data->data = xrealloc(p->data->data, + s * sizeof(struct line)); } - /* -#define YALIGN(y) (((y)+0x3ff)&~0x3ff) - if (YALIGN(y + 1) > YALIGN(p->data->y)) { - if (YALIGN((unsigned)y + 1) > MAXINT / sizeof(struct line)) overalloc(); - p->data->data = mem_realloc(p->data->data, YALIGN(y+1)*sizeof(struct line)); - }*/ for (i = p->data->y; i < y; i++) { p->data->data[i].l = 0; p->data->data[i].allocated = 0; @@ -281,8 +277,9 @@ static void xpand_line(struct part *p, int y, int x) if (x >= ln->allocated) { if (x >= 0x4000) ln->allocated = safe_add(x, x); else ln->allocated = safe_add(x, 0x10) & ~0xf; - if ((unsigned)ln->allocated > MAXINT / sizeof(chr)) overalloc(); - ln->d = mem_realloc(ln->d, ln->allocated*sizeof(chr)); + if ((unsigned)ln->allocated > MAXINT / sizeof(chr)) + overalloc(); + ln->d = xrealloc(ln->d, ln->allocated*sizeof(chr)); } for (i = ln->l; i <= x; i++) { ln->d[i].at = p->attribute; @@ -295,8 +292,9 @@ static void xpand_line(struct part *p, int y, int x) static void r_xpand_spaces(struct part *p, int l) { unsigned char *c; - if ((unsigned)l >= MAXINT) overalloc(); - c = mem_realloc(p->spaces, l + 1); + if ((unsigned)l >= MAXINT) + overalloc(); + c = xrealloc(p->spaces, l + 1); memset(c + p->spl, 0, l - p->spl + 1); p->spl = l + 1; p->spaces = c; @@ -531,10 +529,13 @@ static void align_line(struct part *p, int y) struct link *new_link(struct f_data *f) { - if (!f) return NULL; + if (!f) + return NULL; if (!(f->nlinks & (ALLOC_GR - 1))) { - if ((unsigned)f->nlinks > MAXINT / sizeof(struct link) - ALLOC_GR) overalloc(); - f->links = mem_realloc(f->links, (f->nlinks + ALLOC_GR) * sizeof(struct link)); + if ((unsigned)f->nlinks > MAXINT / sizeof(struct link) - ALLOC_GR) + overalloc(); + f->links = xrealloc(f->links, + (f->nlinks + ALLOC_GR) * sizeof(struct link)); } memset(&f->links[f->nlinks], 0, sizeof(struct link)); #ifdef G @@ -774,8 +775,9 @@ static void put_chars(void *p_, unsigned char *c, int l) link->sel_color = get_attribute(fg, bg); link->n = 0; set_link: - if ((unsigned)link->n + (unsigned)ll > MAXINT / sizeof(struct point)) overalloc(); - pt = mem_realloc(link->pos, (link->n + ll) * sizeof(struct point)); + if ((unsigned)link->n + (unsigned)ll > MAXINT / sizeof(struct point)) + overalloc(); + pt = xrealloc(link->pos, (link->n + ll) * sizeof(struct point)); link->pos = pt; for (i = 0; i < ll; i++) pt[link->n + i].x = X(p->cx) + i, pt[link->n + i].y = Y(p->cy); diff --git a/html_tbl.c b/html_tbl.c @@ -332,8 +332,9 @@ static void new_columns(struct table *t, int span, int width, int align, int val if ((unsigned)n > MAXINT / 2) overalloc(); n *= 2; } - if ((unsigned)n > MAXINT / sizeof(struct table_column)) overalloc(); - nc = mem_realloc(t->cols, n * sizeof(struct table_column)); + if ((unsigned)n > MAXINT / sizeof(struct table_column)) + overalloc(); + nc = xrealloc(t->cols, n * sizeof(struct table_column)); t->rc = n; t->cols = nc; } @@ -356,9 +357,11 @@ static void set_td_width(struct table *t, int x, int width, int f) if ((unsigned)n > MAXINT / 2) overalloc(); n *= 2; } - if ((unsigned)n > MAXINT / sizeof(int)) overalloc(); - nc = mem_realloc(t->xcols, n * sizeof(int)); - for (i = t->xc; i < n; i++) nc[i] = W_AUTO; + if ((unsigned)n > MAXINT / sizeof(int)) + overalloc(); + nc = xrealloc(t->xcols, n * sizeof(int)); + for (i = t->xc; i < n; i++) + nc[i] = W_AUTO; t->xc = n; t->xcols = nc; } @@ -432,8 +435,10 @@ static struct table *parse_table(unsigned char *html, unsigned char *eof, unsign html = en; if (bad_html && !p && !lbhp) { if (!(*bhp & (ALLOC_GR-1))) { - if ((unsigned)*bhp > MAXINT / sizeof(struct s_e) - ALLOC_GR) overalloc(); - *bad_html = mem_realloc(*bad_html, (*bhp + ALLOC_GR) * sizeof(struct s_e)); + if ((unsigned)*bhp > MAXINT / sizeof(struct s_e) - ALLOC_GR) + overalloc(); + *bad_html = xrealloc(*bad_html, + (*bhp + ALLOC_GR) * sizeof(struct s_e)); } lbhp = (*bad_html)[(*bhp)++].s = html; } @@ -1589,9 +1594,12 @@ static void add_to_rect_sets(struct rect_set ***s, int *n, struct rect *r) for (i = r->y1 >> RECT_BOUND_BITS; i <= (r->y2 - 1) >> RECT_BOUND_BITS; i++) { if (i >= *n) { struct rect_set **ns; - if ((unsigned)i > MAXINT / sizeof(struct rect_set *) - 1) overalloc(); - ns = mem_realloc(*s, (i + 1) * sizeof(struct rect_set *)); - for (j = *n; j < i + 1; j++) ns[j] = init_rect_set(); + if ((unsigned)i > MAXINT / sizeof(struct rect_set *) - 1) + overalloc(); + ns = xrealloc(*s, + (i + 1) * sizeof(struct rect_set *)); + for (j = *n; j < i + 1; j++) + ns[j] = init_rect_set(); *s = ns; *n = i + 1; } @@ -1608,10 +1616,12 @@ static void add_to_cell_sets(struct table_cell ****s, int **nn, int *n, struct r if (i >= *n) { struct table_cell ***ns; int *nnn; - if ((unsigned)i > MAXINT / sizeof(struct table_cell ***) - 1) overalloc(); - if ((unsigned)i > MAXINT / sizeof(int *) - 1) overalloc(); - ns = mem_realloc(*s, (i + 1) * sizeof(struct table_cell **)); - nnn = mem_realloc(*nn, (i + 1) * sizeof(int)); + if ((unsigned)i > MAXINT / sizeof(struct table_cell ***) - 1 + || (unsigned)i > MAXINT / sizeof(int *) - 1) + overalloc(); + ns = xrealloc(*s, + (i + 1) * sizeof(struct table_cell **)); + nnn = xrealloc(*nn, (i + 1) * sizeof(int)); for (j = *n; j < i + 1; j++) { ns[j] = NULL; nnn[j] = 0; @@ -1622,8 +1632,10 @@ static void add_to_cell_sets(struct table_cell ****s, int **nn, int *n, struct r } { struct table_cell **nc; - if ((unsigned)(*nn)[i] > MAXINT / sizeof(struct table_cell *) - 1) overalloc(); - nc = mem_realloc((*s)[i], ((*nn)[i] + 1) * sizeof(struct table_cell *)); + if ((unsigned)(*nn)[i] > MAXINT / sizeof(struct table_cell *) - 1) + overalloc(); + nc = xrealloc((*s)[i], + ((*nn)[i] + 1) * sizeof(struct table_cell *)); nc[(*nn)[i]] = c; (*s)[i] = nc; (*nn)[i]++; diff --git a/jpeg.c b/jpeg.c @@ -218,13 +218,12 @@ void jpeg_restart(struct cached_image *cimg, unsigned char *data, int length) * allocate more space */ memmove(deco->jdata,global_cinfo->src->next_input_byte, global_cinfo->src->bytes_in_buffer); - deco->jdata=mem_realloc( - deco->jdata, global_cinfo->src->bytes_in_buffer+length); - }else{ + deco->jdata = xrealloc(deco->jdata, + global_cinfo->src->bytes_in_buffer+length); + } else /* If there is no decoder buffer we'll have to allocate * space for a new buffer */ - deco->jdata = xmalloc(global_cinfo->src->bytes_in_buffer+length); - } + deco->jdata = xmalloc(global_cinfo->src->bytes_in_buffer + length); /* Copy the data iself into the decoder buffer */ memcpy(deco->jdata+global_cinfo->src->bytes_in_buffer diff --git a/kbd.c b/kbd.c @@ -105,8 +105,9 @@ retry: } } if (w < len) { - if ((unsigned)itrm->eqlen + (unsigned)(len - w) > MAXINT) overalloc(); - itrm->ev_queue = mem_realloc(itrm->ev_queue, itrm->eqlen + len - w); + if ((unsigned)itrm->eqlen + (unsigned)(len - w) > MAXINT) + overalloc(); + itrm->ev_queue = xrealloc(itrm->ev_queue, itrm->eqlen + len - w); memcpy(itrm->ev_queue + itrm->eqlen, data + w, len - w); itrm->eqlen += len - w; set_handlers(itrm->sock_out, get_handler(itrm->sock_out, H_READ), write_ev_queue, itrm); diff --git a/links.h b/links.h @@ -207,6 +207,7 @@ extern int F; void die(const char *, ...); void *xmalloc(size_t); +void *xrealloc(void *, size_t); /* error.c */ @@ -273,17 +274,9 @@ static inline int safe_add_function(int x, int y, unsigned char *file, int line) #define safe_add(x, y) safe_add_function(x, y, (unsigned char *)__FILE__, __LINE__) void *mem_calloc_(size_t size, int mayfail); -void *mem_realloc_(void *p, size_t size, int mayfail); - #define mem_calloc(x) mem_calloc_(x, 0) -#define mem_realloc(x, y) mem_realloc_(x, y, 0) - #define mem_calloc_mayfail(x) mem_calloc_(x, 1) -#define mem_realloc_mayfail(x, y) mem_realloc_(x, y, 1) - - static inline void *debug_mem_calloc(unsigned char *f, int l, size_t s, int mayfail) { return mem_calloc_(s, mayfail); } -static inline void *debug_mem_realloc(unsigned char *f, int l, void *p, size_t s, int mayfail) { return mem_realloc_(p, s, mayfail); } static inline unsigned char *get_mem_comment(void *p){return (unsigned char *)"";} unsigned char *memacpy(const unsigned char *src, size_t len); diff --git a/main.c b/main.c @@ -39,6 +39,15 @@ xmalloc(size_t len) return p; } +void * +xrealloc(void *p, size_t len) +{ + if (!(p = realloc(p, len))) + die("realloc: %s\n", strerror(errno)); + + return p; +} + static void sig_intr(void *t_) { struct terminal *t = (struct terminal *)t_; diff --git a/os_dep.c b/os_dep.c @@ -826,19 +826,23 @@ struct open_in_new *get_open_in_new(int environment) int i; struct open_in_new *oin = NULL; int noin = 0; - if (anonymous) return NULL; - if (environment & ENV_G) environment = ENV_G; - for (i = 0; i < (int)array_elements(oinw); i++) if ((environment & oinw[i].env) == oinw[i].env) { - if ((unsigned)noin > MAXINT / sizeof(struct open_in_new) - 2) overalloc(); - oin = mem_realloc(oin, (noin + 2) * sizeof(struct open_in_new)); - oin[noin].text = oinw[i].text; - oin[noin].hk = oinw[i].hk; - oin[noin].open_window_fn = &oinw[i].open_window_fn; - noin++; - oin[noin].text = NULL; - oin[noin].hk = NULL; - oin[noin].open_window_fn = NULL; - } + if (anonymous) + return NULL; + if (environment & ENV_G) + environment = ENV_G; + for (i = 0; i < (int)array_elements(oinw); i++) + if ((environment & oinw[i].env) == oinw[i].env) { + if ((unsigned)noin > MAXINT / sizeof(struct open_in_new) - 2) + overalloc(); + oin = xrealloc(oin, (noin + 2) * sizeof(struct open_in_new)); + oin[noin].text = oinw[i].text; + oin[noin].hk = oinw[i].hk; + oin[noin].open_window_fn = &oinw[i].open_window_fn; + noin++; + oin[noin].text = NULL; + oin[noin].hk = NULL; + oin[noin].open_window_fn = NULL; + } return oin; } diff --git a/select.c b/select.c @@ -553,8 +553,9 @@ void set_handlers_file_line(int fd, void (*read_func)(void *), void (*write_func return; } if (fd >= n_threads) { - if ((unsigned)fd > (unsigned)MAXINT / sizeof(struct thread) - 1) overalloc(); - threads = mem_realloc(threads, (fd + 1) * sizeof(struct thread)); + if ((unsigned)fd > (unsigned)MAXINT / sizeof(struct thread) - 1) + overalloc(); + threads = xrealloc(threads, (fd + 1) * sizeof(struct thread)); memset(threads + n_threads, 0, (fd + 1 - n_threads) * sizeof(struct thread)); n_threads = fd + 1; } diff --git a/string.c b/string.c @@ -42,8 +42,9 @@ void add_to_strn(unsigned char **s, unsigned char *a) { unsigned char *p; size_t l1 = strlen(cast_const_char *s), l2 = strlen(cast_const_char a); - if (((l1 | l2) | (l1 + l2 + 1)) > MAXINT) overalloc(); - p = (unsigned char *)mem_realloc(*s, l1 + l2 + 1); + if (((l1 | l2) | (l1 + l2 + 1)) > MAXINT) + overalloc(); + p = xrealloc(*s, l1 + l2 + 1); strcat(cast_char p, cast_const_char a); *s = p; } @@ -51,8 +52,9 @@ void add_to_strn(unsigned char **s, unsigned char *a) void extend_str(unsigned char **s, int n) { size_t l = strlen(cast_const_char *s); - if (((l | n) | (l + n + 1)) > MAXINT) overalloc(); - *s = (unsigned char *)mem_realloc(*s, l + n + 1); + if (((l | n) | (l + n + 1)) > MAXINT) + overalloc(); + *s = xrealloc(*s, l + n + 1); } void add_bytes_to_str(unsigned char **s, int *l, unsigned char *a, size_t ll) @@ -81,7 +83,7 @@ void add_bytes_to_str(unsigned char **s, int *l, unsigned char *a, size_t ll) new_length |= new_length >> 16; new_length++; } - p = (unsigned char *)mem_realloc(p, new_length); + p = xrealloc(p, new_length); *s = p; } p[*l] = 0; diff --git a/terminal.c b/terminal.c @@ -96,12 +96,16 @@ static void set_margin(struct terminal *term) static void alloc_term_screen(struct terminal *term) { chr *s, *t; - if (term->x < 0) term->x = 1; - if (term->y < 0) term->y = 1; - if (term->x && (unsigned)term->x * (unsigned)term->y / (unsigned)term->x != (unsigned)term->y) overalloc(); - if ((unsigned)term->x * (unsigned)term->y > MAXINT / sizeof(*term->screen)) overalloc(); - s = mem_realloc(term->screen, term->x * term->y * sizeof(*term->screen)); - t = mem_realloc(term->last_screen, term->x * term->y * sizeof(*term->screen)); + if (term->x < 0) + term->x = 1; + if (term->y < 0) + term->y = 1; + if ((term->x && (unsigned)term->x * (unsigned)term->y / (unsigned)term->x != (unsigned)term->y) + || (unsigned)term->x * (unsigned)term->y > MAXINT / sizeof(*term->screen)) + overalloc(); + s = xrealloc(term->screen, term->x * term->y * sizeof(*term->screen)); + t = xrealloc(term->last_screen, + term->x * term->y * sizeof(*term->screen)); memset(t, -1, term->x * term->y * sizeof(*term->screen)); term->last_screen = t; memset(s, 0, term->x * term->y * sizeof(*term->screen)); @@ -250,8 +254,10 @@ void add_to_rect_set(struct rect_set **s, struct rect *r) if (i >= ss->m) ss->m = i + 1; return; } - if ((unsigned)ss->rl > (MAXINT - sizeof(struct rect_set)) / sizeof(struct rect) - R_GR) overalloc(); - ss = mem_realloc(ss, sizeof(struct rect_set) + sizeof(struct rect) * (ss->rl + R_GR)); + if ((unsigned)ss->rl > (MAXINT - sizeof(struct rect_set)) / sizeof(struct rect) - R_GR) + overalloc(); + ss = xrealloc(ss, + sizeof(struct rect_set) + sizeof(struct rect) * (ss->rl + R_GR)); memset(&(*s = ss)->r[i = (ss->rl += R_GR) - R_GR], 0, sizeof(struct rect) * R_GR); goto x; } @@ -786,8 +792,9 @@ static void in_term(void *term_) struct links_event *ev; int r; unsigned char *iq; - if ((unsigned)term->qlen + ALLOC_GR > MAXINT) overalloc(); - iq = mem_realloc(term->input_queue, term->qlen + ALLOC_GR); + if ((unsigned)term->qlen + ALLOC_GR > MAXINT) + overalloc(); + iq = xrealloc(term->input_queue, term->qlen + ALLOC_GR); term->input_queue = iq; EINTRLOOP(r, (int)read(term->fdin, iq + term->qlen, ALLOC_GR)); if (r <= 0) { diff --git a/view.c b/view.c @@ -201,8 +201,9 @@ static struct line_info *format_text_uncached(unsigned char *text, int width, in sk = 1; put: if (!(lnn & (ALLOC_GR-1))) { - if ((unsigned)lnn > MAXINT / sizeof(struct line_info) - ALLOC_GR) overalloc(); - ln = mem_realloc(ln, (lnn + ALLOC_GR) * sizeof(struct line_info)); + if ((unsigned)lnn > MAXINT / sizeof(struct line_info) - ALLOC_GR) + overalloc(); + ln = xrealloc(ln, (lnn + ALLOC_GR) * sizeof(struct line_info)); } ln[lnn].st = b; ln[lnn++].en = text; @@ -552,8 +553,10 @@ static int get_searched(struct f_data_c *scr, struct point **pt, int *pl) set_color(t, x, y, co);*/ if (!(len & (ALLOC_GR - 1))) { struct point *points2; - if ((unsigned)len > MAXINT / sizeof(struct point) - ALLOC_GR) goto ret; - points2 = mem_realloc_mayfail(points, sizeof(struct point) * (len + ALLOC_GR)); + if ((unsigned)len > MAXINT / sizeof(struct point) - ALLOC_GR) + goto ret; + points2 = xrealloc(points, + sizeof(struct point) * (len + ALLOC_GR)); if (!points2) goto ret; points = points2; } @@ -673,10 +676,12 @@ struct form_state *find_form_state(struct f_data_c *f, struct form_control *form struct view_state *vs = f->vs; struct form_state *fs; int n = form->g_ctrl_num; - if (n < vs->form_info_len) fs = &vs->form_info[n]; + if (n < vs->form_info_len) + fs = &vs->form_info[n]; else { - if ((unsigned)n > MAXINT / sizeof(struct form_state) - 1) overalloc(); - fs = mem_realloc(vs->form_info, (n + 1) * sizeof(struct form_state)); + if ((unsigned)n > MAXINT / sizeof(struct form_state) - 1) + overalloc(); + fs = xrealloc(vs->form_info, (n + 1) * sizeof(struct form_state)); vs->form_info = fs; memset(fs + vs->form_info_len, 0, (n + 1 - vs->form_info_len) * sizeof(struct form_state)); vs->form_info_len = n + 1; @@ -1880,8 +1885,10 @@ static void encode_multipart(struct session *ses, struct list_head *l, unsigned bnd: add_to_str(data, len, cast_uchar "--"); if (!(nbound_ptrs & (ALLOC_GR-1))) { - if ((unsigned)nbound_ptrs > MAXINT / sizeof(int) - ALLOC_GR) overalloc(); - bound_ptrs = mem_realloc(bound_ptrs, (nbound_ptrs + ALLOC_GR) * sizeof(int)); + if ((unsigned)nbound_ptrs > MAXINT / sizeof(int) - ALLOC_GR) + overalloc(); + bound_ptrs = xrealloc(bound_ptrs, + (nbound_ptrs + ALLOC_GR) * sizeof(int)); } bound_ptrs[nbound_ptrs++] = *len; add_bytes_to_str(data, len, bound, BL); @@ -2103,8 +2110,9 @@ static struct menu_item *clone_select_menu(struct menu_item *m) struct menu_item *n = NULL; int i = 0; do { - if ((unsigned)i > MAXINT / sizeof(struct menu_item) - 1) overalloc(); - n = mem_realloc(n, (i + 1) * sizeof(struct menu_item)); + if ((unsigned)i > MAXINT / sizeof(struct menu_item) - 1) + overalloc(); + n = xrealloc(n, (i + 1) * sizeof(struct menu_item)); n[i].text = stracpy(m->text); n[i].rtext = stracpy(m->rtext); n[i].hotkey = stracpy(m->hotkey); @@ -2404,7 +2412,8 @@ int field_op(struct session *ses, struct f_data_c *f, struct link *l, struct lin unsigned char a_[2]; unsigned char *nw; int ll; - v = fs->value = mem_realloc(fs->value, strlen(cast_const_char fs->value) + 12); + v = fs->value = xrealloc(fs->value, + strlen(cast_const_char fs->value) + 12); if (f->f_data->opt.cp != utf8_table) { nw = a_; a_[0] = (unsigned char)ev->x; @@ -2419,10 +2428,15 @@ int field_op(struct session *ses, struct f_data_c *f, struct link *l, struct lin fs->state += ll; } goto done; - } else if (!(ev->y & (KBD_SHIFT | KBD_CTRL | KBD_ALT)) && ev->x == KBD_ENTER && form->type == FC_TEXTAREA && (!ses->term->spec->braille || f->vs->brl_in_field)) { - if (!form->ro && strlen(cast_const_char fs->value) < (size_t)form->maxlength) { + } else if (!(ev->y & (KBD_SHIFT | KBD_CTRL | KBD_ALT)) + && ev->x == KBD_ENTER + && form->type == FC_TEXTAREA + && (!ses->term->spec->braille || f->vs->brl_in_field)) { + if (!form->ro + && strlen(cast_const_char fs->value) < (size_t)form->maxlength) { unsigned char *v; - v = mem_realloc(fs->value, strlen(cast_const_char fs->value) + 2); + v = xrealloc(fs->value, + strlen(cast_const_char fs->value) + 2); fs->value = v; memmove(v + fs->state + 1, v + fs->state, strlen(cast_const_char(v + fs->state)) + 1); v[fs->state++] = '\n'; @@ -2533,16 +2547,17 @@ int field_op(struct session *ses, struct f_data_c *f, struct link *l, struct lin } if (!form->ro && cp_len(term_charset(ses->term), fs->value) + cp_len(term_charset(ses->term), clipboard) <= form->maxlength) { unsigned char *v; - v = mem_realloc(fs->value, strlen(cast_const_char fs->value) + strlen(cast_const_char clipboard) +1); + v = xrealloc(fs->value, + strlen(cast_const_char fs->value) + strlen(cast_const_char clipboard) +1); fs->value = v; memmove(v + fs->state + strlen(cast_const_char clipboard), v + fs->state, strlen(cast_const_char v) - fs->state + 1); memcpy(v + fs->state, clipboard, strlen(cast_const_char clipboard)); fs->state += (int)strlen(cast_const_char clipboard); } free(clipboard); - } else if (ev->x == KBD_ENTER) { + } else if (ev->x == KBD_ENTER) x = 0; - } else if (ev->x == KBD_BS) { + else if (ev->x == KBD_BS) { set_br_pos(f, l); if (!form->ro && fs->state) { int ll = 1; diff --git a/view_gr.c b/view_gr.c @@ -521,11 +521,15 @@ static void g_get_search(struct f_data *f, unsigned char *s) } } len = compare_case_utf8(f->srch_string + i, s); - if (!len) continue; + if (!len) + continue; if (!(f->n_search_positions & (ALLOC_GR - 1))) { - if ((unsigned)f->n_search_positions > MAXINT / sizeof(int) - ALLOC_GR) overalloc(); - f->search_positions = mem_realloc(f->search_positions, (f->n_search_positions + ALLOC_GR) * sizeof(int)); - f->search_lengths = mem_realloc(f->search_lengths, (f->n_search_positions + ALLOC_GR) * sizeof(int)); + if ((unsigned)f->n_search_positions > MAXINT / sizeof(int) - ALLOC_GR) + overalloc(); + f->search_positions = xrealloc(f->search_positions, + (f->n_search_positions + ALLOC_GR) * sizeof(int)); + f->search_lengths = xrealloc(f->search_lengths, + (f->n_search_positions + ALLOC_GR) * sizeof(int)); } f->search_positions[f->n_search_positions] = i; f->search_lengths[f->n_search_positions] = len; diff --git a/x.c b/x.c @@ -574,11 +574,13 @@ static void x_add_to_table(struct graphics_device* gd) if (!c) x_hash_table[a].pointer = xmalloc(sizeof(struct graphics_device *)); else { - if ((unsigned)c > MAXINT / sizeof(struct graphics_device *) - 1) overalloc(); - x_hash_table[a].pointer=mem_realloc(x_hash_table[a].pointer,(c+1)*sizeof(struct graphics_device *)); + if ((unsigned)c > MAXINT / sizeof(struct graphics_device *) - 1) + overalloc(); + x_hash_table[a].pointer = xrealloc(x_hash_table[a].pointer, + (c + 1) * sizeof(struct graphics_device *)); } - x_hash_table[a].pointer[c]=gd; + x_hash_table[a].pointer[c] = gd; x_hash_table[a].count++; } @@ -594,7 +596,8 @@ static void x_remove_from_table(Window *win) { memmove(x_hash_table[a].pointer+b,x_hash_table[a].pointer+b+1,(x_hash_table[a].count-b-1)*sizeof(struct graphics_device *)); x_hash_table[a].count--; - x_hash_table[a].pointer=mem_realloc(x_hash_table[a].pointer,x_hash_table[a].count*sizeof(struct graphics_device*)); + x_hash_table[a].pointer = xrealloc(x_hash_table[a].pointer, + x_hash_table[a].count * sizeof(struct graphics_device *)); } }