diff --git a/buffer.c b/buffer.c index f2bbecb..650e2aa 100644 --- a/buffer.c +++ b/buffer.c @@ -65,7 +65,7 @@ static void update_page(Page *p) /* if the page is read only, copy it */ if (p->flags & PG_READ_ONLY) { - buf = qe_malloc_dup(p->data, p->size); + buf = qe_malloc_dup_bytes(p->data, p->size); /* XXX: should return an error */ if (!buf) return; @@ -180,7 +180,8 @@ static void eb_insert1(EditBuffer *b, int page_index, const u8 *buf, int size) if (len > 0) { update_page(p); /* CG: probably faster with qe_malloc + qe_free */ - qe_realloc(&p->data, p->size + len); + // XXX: test for failure + qe_realloc_bytes(&p->data, p->size + len); memmove(p->data + len, p->data, p->size); memcpy(p->data, buf + size - len, len); size -= len; @@ -192,7 +193,8 @@ static void eb_insert1(EditBuffer *b, int page_index, const u8 *buf, int size) n = (size + MAX_PAGE_SIZE - 1) / MAX_PAGE_SIZE; if (n > 0) { b->nb_pages += n; - qe_realloc(&b->page_table, b->nb_pages * sizeof(Page)); + // XXX: test for failure + qe_realloc_array(&b->page_table, b->nb_pages); p = &b->page_table[page_index]; blockmove(p + n, p, b->nb_pages - n - page_index); while (size > 0) { @@ -200,7 +202,7 @@ static void eb_insert1(EditBuffer *b, int page_index, const u8 *buf, int size) if (len > MAX_PAGE_SIZE) len = MAX_PAGE_SIZE; p->size = len; - p->data = qe_malloc_dup(buf, len); + p->data = qe_malloc_dup_bytes(buf, len); p->flags = 0; buf += len; size -= len; @@ -240,7 +242,8 @@ static void eb_insert_lowlevel(EditBuffer *b, int offset, update_page(p - 1); update_page(p); chunk = min_offset(MAX_PAGE_SIZE - p[-1].size, offset); - qe_realloc(&p[-1].data, p[-1].size + chunk); + // XXX: test for failure + qe_realloc_bytes(&p[-1].data, p[-1].size + chunk); memcpy(p[-1].data + p[-1].size, p->data, chunk); p[-1].size += chunk; p->size -= chunk; @@ -249,13 +252,15 @@ static void eb_insert_lowlevel(EditBuffer *b, int offset, b->nb_pages -= 1; qe_free(&p->data); blockmove(p, p + 1, b->nb_pages - page_index); - qe_realloc(&b->page_table, b->nb_pages * sizeof(Page)); + // XXX: test for failure + qe_realloc_array(&b->page_table, b->nb_pages); p = b->page_table + page_index - 1; offset = p->size; goto retry; } memmove(p->data, p->data + chunk, p->size); - qe_realloc(&p->data, p->size); + // XXX: test for failure + qe_realloc_bytes(&p->data, p->size); offset -= chunk; if (offset == 0 && p[-1].size < MAX_PAGE_SIZE) { /* restart from previous page */ @@ -276,7 +281,8 @@ static void eb_insert_lowlevel(EditBuffer *b, int offset, p = b->page_table + page_index; update_page(p); p->size += len - len_out; - qe_realloc(&p->data, p->size); + // XXX: test for failure + qe_realloc_bytes(&p->data, p->size); memmove(p->data + offset + len, p->data + offset, p->size - (offset + len)); memcpy(p->data + offset, buf, len); @@ -382,7 +388,8 @@ int eb_insert_buffer(EditBuffer *dest, int dest_offset, realloced */ q = dest->page_table + page_index - 1; update_page(q); - qe_realloc(&q->data, dest_offset); + // XXX: test for failure + qe_realloc_bytes(&q->data, dest_offset); q->size = dest_offset; } } else { @@ -403,7 +410,8 @@ int eb_insert_buffer(EditBuffer *dest, int dest_offset, if (n > 0) { /* add the pages */ dest->nb_pages += n; - qe_realloc(&dest->page_table, dest->nb_pages * sizeof(Page)); + // XXX: test for failure + qe_realloc_array(&dest->page_table, dest->nb_pages); q = dest->page_table + page_index; blockmove(q + n, q, dest->nb_pages - n - page_index); p = p_start; @@ -417,7 +425,7 @@ int eb_insert_buffer(EditBuffer *dest, int dest_offset, } else { /* allocate a new page */ q->flags = 0; - q->data = qe_malloc_dup(p->data, len); + q->data = qe_malloc_dup_bytes(p->data, len); } n--; p++; @@ -506,7 +514,8 @@ int eb_delete(EditBuffer *b, int offset, int size) memmove(p->data + offset, p->data + offset + len, p->size - offset - len); p->size -= len; - qe_realloc(&p->data, p->size); + // XXX: test for failure + qe_realloc_bytes(&p->data, p->size); offset += len; /* XXX: should merge with adjacent pages if size becomes small? */ if (offset >= p->size) { @@ -522,7 +531,8 @@ int eb_delete(EditBuffer *b, int offset, int size) b->nb_pages -= n; blockmove(del_start, del_start + n, b->page_table + b->nb_pages - del_start); - qe_realloc(&b->page_table, b->nb_pages * sizeof(Page)); + // XXX: test for failure + qe_realloc_array(&b->page_table, b->nb_pages); } /* the page cache is no longer valid */ @@ -616,7 +626,7 @@ static int eb_cache_insert(EditBuffer *b) if (len >= qs->buffer_cache_size) { int size = max_int(32, len + (len >> 1) + (len >> 3)); - cache = qe_realloc(&qs->buffer_cache, size * sizeof(*cache)); + cache = qe_realloc_array(&qs->buffer_cache, size); if (!cache) return -1; qs->buffer_cache_size = size; diff --git a/color.c b/color.c index d87ca68..4d2f599 100644 --- a/color.c +++ b/color.c @@ -641,15 +641,16 @@ int css_define_color(const char *name, const char *value) /* First color definition: allocate modifiable array */ if (qe_colors == default_colors) { - qe_colors = qe_malloc_dup(default_colors, sizeof(default_colors)); + def = qe_malloc_dup_array(default_colors, nb_default_colors); + if (!def) + return -1; + qe_colors = def; } /* Make room: reallocate table in chunks of 8 entries */ if (((nb_qe_colors - nb_default_colors) & 7) == 0) { - if (!qe_realloc(&qe_colors, - (nb_qe_colors + 8) * sizeof(ColorDef))) { + if (!qe_realloc_array(&qe_colors, nb_qe_colors + 8)) return -1; - } } /* Check for redefinition */ index = css_lookup_color(qe_colors, nb_qe_colors, name); diff --git a/html2png.c b/html2png.c index 8571ed9..4facb89 100644 --- a/html2png.c +++ b/html2png.c @@ -141,7 +141,8 @@ static int ppm_resize(QEditScreen *s, int w, int h) CFBContext *cfb = s->priv_data; /* alloc bitmap */ - if (!qe_realloc(&cfb->base, w * h * sizeof(int))) { + // XXX: Achtung: cfb->base is an `unsigned char *` + if (!qe_realloc_bytes(&cfb->base, w * h * sizeof(int))) { return -1; } cfb->wrap = w * sizeof(int); diff --git a/libqhtml/css.c b/libqhtml/css.c index 6831db9..a031f70 100644 --- a/libqhtml/css.c +++ b/libqhtml/css.c @@ -339,7 +339,7 @@ CSSIdent css_new_ident(const char *str) if (table_ident_nb == table_ident_allocated) { /* realloc ident table if needed */ n = table_ident_allocated + CSS_IDENT_INCR; - if (!qe_realloc(&table_ident, n * sizeof(CSSIdentEntry *))) { + if (!qe_realloc_array(&table_ident, n)) { return CSS_ID_NIL; } table_ident_allocated = n; @@ -888,7 +888,7 @@ static CSSState *allocate_props(CSSContext *s, CSSState *props) pp = &p->hash_next; } /* add new props */ - p = qe_malloc_dup(props, sizeof(CSSState)); + p = qe_malloc_dup_array(props, 1); if (!p) return NULL; s->nb_props++; @@ -1956,7 +1956,7 @@ static void css_flush_line(InlineLayout *s, if (level_max > 0) { /* needed to do bidir reordering */ - box_table = qe_malloc_dup(line_boxes, sizeof(InlineBox) * nb_boxes); + box_table = qe_malloc_dup_array(line_boxes, nb_boxes); if (box_table) { /* record the logical order of the boxes */ /* rearrange them to match visual order */ @@ -2543,7 +2543,7 @@ static void allocate_column(TableLayout *s) s->nb_cols++; if (s->nb_cols > s->nb_cols_allocated) { s->nb_cols_allocated = s->nb_cols_allocated + COL_INCR; - qe_realloc(&s->cols, s->nb_cols_allocated * sizeof(ColStruct)); + qe_realloc_array(&s->cols, s->nb_cols_allocated); memset(s->cols + s->nb_cols_allocated - COL_INCR, 0, COL_INCR * sizeof(ColStruct)); } diff --git a/libqhtml/cssparse.c b/libqhtml/cssparse.c index 312d79c..2e8e0b0 100644 --- a/libqhtml/cssparse.c +++ b/libqhtml/cssparse.c @@ -1077,7 +1077,7 @@ void css_parse_style_sheet(CSSStyleSheet *s, CSSParseState *b) if (qe_isalpha(ch)) { tree_op = CSS_TREE_OP_DESCENDANT; add_tree: - ss1 = qe_malloc_dup(ss, sizeof(CSSSimpleSelector)); + ss1 = qe_malloc_dup_array(ss, 1); if (ss1) { last_ss = ss1; } diff --git a/libqhtml/xmlparse.c b/libqhtml/xmlparse.c index 8997b43..bc3533f 100644 --- a/libqhtml/xmlparse.c +++ b/libqhtml/xmlparse.c @@ -191,7 +191,7 @@ static void strbuf_addch1(StringBuffer *b, char32_t ch) ptr = b->buf; if (b->buf == b->buf1) ptr = NULL; - if (qe_realloc(&ptr, size1)) { + if (qe_realloc_bytes(&ptr, size1)) { if (b->buf == b->buf1) memcpy(ptr, b->buf1, STRING_BUF_SIZE); b->buf = ptr; @@ -251,7 +251,7 @@ static void offsetbuf_add(OffsetBuffer *b, unsigned int offset) if (!n) n = 1; n = n * 2; - if (!qe_realloc(&b->offsets, n * sizeof(unsigned int))) + if (!qe_realloc_array(&b->offsets, n)) return; b->nb_allocated_offsets = n; } diff --git a/qe-manual.md b/qe-manual.md index 1219f82..8a06110 100644 --- a/qe-manual.md +++ b/qe-manual.md @@ -752,7 +752,21 @@ bytes. Return a pointer to allocated memory, aligned on the maximum alignment size. -### `void *qe_malloc_dup(const void *src, size_t size);` +### `T *qe_malloc_dup_array(const T *p, size_t n);` + +Allocate memory for an array of objects of type `T`. Initialize the elements +from the array pointed to by `p`. + +* argument `T` the type of the object to allocate. + +* argument `p` a pointer to the array used for initialization. + +* argument `n` the number of elements to duplicate. + +Note: this function is implemented as a macro. +The uninitialized elements are set to all bits zero. + +### `void *qe_malloc_dup_bytes(const void *src, size_t size);` Allocate a block of memory of a given size in bytes initialized as a copy of an existing object. @@ -815,7 +829,20 @@ to all bits zero. Return a pointer to allocated memory, aligned on the maximum alignment size. -### `void *qe_realloc(void *pp, size_t size);` +### `T *qe_realloc_array(T **pp, size_t new_len);` + +Reallocate a block of memory to a different size. + +* argument `pp` the address of a pointer to the array to reallocate + +* argument `new_len` the new number of elements for the array. + +Return a pointer to allocated memory, aligned on the maximum +alignment size. + +Note: this function is implemented as a macro. + +### `void *qe_realloc_bytes(void *pp, size_t size);` reallocate a block of memory to a different size. @@ -845,6 +872,17 @@ Allocate a copy of a string. Return a pointer to allocated memory, aligned on the maximum alignment size. +### `char *qe_strndup(const char *str, size_t n);` + +Allocate a copy of a portion of a string. + +* argument `src` a valid pointer to a string to duplicate. + +* argument `n` the number of characters to duplicate. + +Return a pointer to allocated memory, aligned on the maximum +alignment size. + ### `int eb_search(EditBuffer *b, int dir, int flags, int start_offset, int end_offset, const char32_t *buf, int len, CSSAbortFunc *abort_func, void *abort_opaque, int *found_offset, int *found_end);` Search a buffer for contents. Return true if contents was found. diff --git a/qe.c b/qe.c index 897cab7..754d16a 100644 --- a/qe.c +++ b/qe.c @@ -199,8 +199,8 @@ void qe_register_mode(ModeDef *m, int flags) mode_name, 0, mode_name); def = qe_mallocz(CmdDef); /* allocate space for name and spec with embedded null bytes */ - def->name = qe_malloc_dup(name, name_len + 2); - def->spec = qe_malloc_dup(spec, spec_len + 1); + def->name = qe_malloc_dup_bytes(name, name_len + 2); + def->spec = qe_malloc_dup_bytes(spec, spec_len + 1); def->sig = CMD_ESs; def->val = 0; def->action.ESs = do_set_mode; @@ -423,7 +423,7 @@ int qe_register_commands(ModeDef *m, const CmdDef *cmds, int len) if (i >= qs->cmd_array_count) { if (i >= qs->cmd_array_size) { int n = max_int(i + 16, 32); - if (!qe_realloc(&qs->cmd_array, n * sizeof(*qs->cmd_array))) { + if (!qe_realloc_array(&qs->cmd_array, n)) { put_status(NULL, "Out of memory"); return -1; } @@ -3690,7 +3690,7 @@ static void flush_line(DisplayState *ds, if (ds->line_num >= e->shadow_nb_lines) { /* reallocate shadow */ int n = ds->line_num + LINE_SHADOW_INCR; - if (qe_realloc(&e->line_shadow, n * sizeof(QELineShadow))) { + if (qe_realloc_array(&e->line_shadow, n)) { /* put an impossible value so that we redraw */ memset(&e->line_shadow[e->shadow_nb_lines], 0xff, (n - e->shadow_nb_lines) * sizeof(QELineShadow)); @@ -4365,7 +4365,7 @@ void cp_colorize_line(QEColorizeContext *cp, if (syn && syn->colorize_func) { if (buf[n] != '\0') { /* ensure buf is null terminated, clone if needed */ - char32_t *buf1 = qe_malloc_dup(buf + i, (n - i + 1) * sizeof(*buf)); + char32_t *buf1 = qe_malloc_dup_array(buf + i, n - i + 1); if (buf1) { buf1[n - i] = '\0'; syn->colorize_func(cp, buf1, n - i, sbuf + i, syn); @@ -4414,10 +4414,8 @@ static int syntax_get_colorized_line(EditState *s, n = max_int(s->colorize_nb_lines, COLORIZED_LINE_PREALLOC_SIZE); while (n < (line_num + 2)) n += (n >> 1) + (n >> 3); - if (!qe_realloc(&s->colorize_states, - n * sizeof(*s->colorize_states))) { + if (!qe_realloc_array(&s->colorize_states, n)) return 0; - } s->colorize_nb_lines = n; } @@ -5993,7 +5991,7 @@ static void macro_add_key(int key) if (qs->nb_macro_keys >= qs->macro_keys_size) { new_size = qs->macro_keys_size + MACRO_KEY_INCR; - if (!qe_realloc(&qs->macro_keys, new_size * sizeof(unsigned short))) + if (!qe_realloc_array(&qs->macro_keys, new_size)) return; qs->macro_keys_size = new_size; } diff --git a/search.c b/search.c index b606f68..e0ba68f 100644 --- a/search.c +++ b/search.c @@ -34,7 +34,7 @@ BOOL lre_check_stack_overflow(void *opaque, size_t alloca_size) { } void *lre_realloc(void *opaque, void *ptr, size_t size) { - return qe_realloc(&ptr, size); + return qe_realloc_bytes(&ptr, size); } #endif diff --git a/tty.c b/tty.c index a519be8..90960f7 100644 --- a/tty.c +++ b/tty.c @@ -518,8 +518,9 @@ static void tty_dpy_invalidate(QEditScreen *s) count = s->width * s->height; size = count * sizeof(TTYChar); /* screen buffer + shadow buffer + extra slot for loop guard */ - qe_realloc(&ts->screen, size * 2 + sizeof(TTYChar)); - qe_realloc(&ts->line_updated, s->height); + // XXX: test for failure + qe_realloc_array(&ts->screen, count * 2 + 1); + qe_realloc_array(&ts->line_updated, s->height); ts->screen_size = count; /* Erase shadow buffer to impossible value */ diff --git a/util.c b/util.c index 0ebec73..e7a9e6d 100644 --- a/util.c +++ b/util.c @@ -1740,7 +1740,7 @@ StringItem *add_string(StringArray *cs, const char *str, int group) { return NULL; if (cs->nb_items >= cs->nb_allocated) { int n = cs->nb_allocated + 32; - if (!qe_realloc(&cs->items, n * sizeof(StringItem *))) + if (!qe_realloc_array(&cs->items, n)) return NULL; cs->nb_allocated = n; } @@ -2015,7 +2015,7 @@ void *qe_mallocz_bytes(size_t size) { return p; } -void *qe_malloc_dup(const void *src, size_t size) { +void *qe_malloc_dup_bytes(const void *src, size_t size) { /*@API memory Allocate a block of memory of a given size in bytes initialized as a copy of an existing object. @@ -2044,7 +2044,24 @@ char *qe_strdup(const char *str) { return p; } -void *qe_realloc(void *pp, size_t size) { +char *qe_strndup(const char *str, size_t n) { + /*@API memory + Allocate a copy of a portion of a string. + @argument `src` a valid pointer to a string to duplicate. + @argument `n` the number of characters to duplicate. + @return a pointer to allocated memory, aligned on the maximum + alignment size. + */ + size_t len = strnlen(str, n); + char *p = (malloc)(len + 1); + if (p) { + memcpy(p, str, len); + p[len] = '\0'; + } + return p; +} + +void *qe_realloc_bytes(void *pp, size_t size) { /*@API memory reallocate a block of memory to a different size. @argument `pp` the address of a possibly null pointer to a diff --git a/util.h b/util.h index d5c203d..bc93fba 100644 --- a/util.h +++ b/util.h @@ -448,9 +448,10 @@ static inline int check_fcall(const char32_t *str, int i) { void *qe_malloc_bytes(size_t size); void *qe_mallocz_bytes(size_t size); -void *qe_malloc_dup(const void *src, size_t size); +void *qe_malloc_dup_bytes(const void *src, size_t size); char *qe_strdup(const char *str); -void *qe_realloc(void *pp, size_t size); +char *qe_strndup(const char *str, size_t len); +void *qe_realloc_bytes(void *pp, size_t new_size); #if 0 /* Documentation prototypes */ @@ -503,6 +504,27 @@ T *qe_mallocz_array(type T, size_t n); @note this function is implemented as a macro. */ +T *qe_malloc_dup_array(const T *p, size_t n); +/*@API memory + Allocate memory for an array of objects of type `T`. Initialize the elements + from the array pointed to by `p`. + @argument `T` the type of the object to allocate. + @argument `p` a pointer to the array used for initialization. + @argument `n` the number of elements to duplicate. + @note this function is implemented as a macro. + The uninitialized elements are set to all bits zero. + */ + +T *qe_realloc_array(T **pp, size_t new_len); +/*@API memory + Reallocate a block of memory to a different size. + @argument `pp` the address of a pointer to the array to reallocate + @argument `new_len` the new number of elements for the array. + @return a pointer to allocated memory, aligned on the maximum + alignment size. + @note this function is implemented as a macro. + */ + void qe_free(T **pp); /*@API memory Free the allocated memory pointed to by a pointer whose address is passed. @@ -520,6 +542,9 @@ void qe_free(T **pp); #define qe_mallocz_array(t, n) ((t *)qe_mallocz_bytes((n) * sizeof(t))) #define qe_malloc_hack(t, n) ((t *)qe_malloc_bytes(sizeof(t) + (n))) #define qe_mallocz_hack(t, n) ((t *)qe_mallocz_bytes(sizeof(t) + (n))) +// XXX: Should use (typeof(**(p)) *) if available +#define qe_malloc_dup_array(p, n) (qe_malloc_dup_bytes(p, (n) * sizeof(*(p)))) +#define qe_realloc_array(pp, n) (qe_realloc_bytes(pp, (n) * sizeof(**(pp)))) #if 1 // to test clang -Weverything #define qe_free(pp) do { void *_1 = (pp), **_2 = _1; (free)(*_2); *_2 = NULL; } while (0)