From 8a8613bcf4227dfe46a694b761e9575bdf6ca2ce Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 7 Nov 2015 23:52:17 -0800 Subject: [PATCH] Prefer xpalloc to doubling buffers by hand MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * src/lread.c (grow_read_buffer): New function, which uses xpalloc. (read1): Use it for simplicity. * src/macros.c (store_kbd_macro_char): * src/minibuf.c (read_minibuf_noninteractive): * src/term.c (encode_terminal_code): * src/xrdb.c (magic_db): Prefer xpalloc to growing buffers by hand. This doesn’t fix any bugs, but simplifies the code a bit. --- src/lread.c | 24 ++++++++++++------------ src/macros.c | 15 +++++---------- src/minibuf.c | 7 +------ src/term.c | 8 ++++---- src/xrdb.c | 8 ++------ 5 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/lread.c b/src/lread.c index 7c891f9954..c4456f37f6 100644 --- a/src/lread.c +++ b/src/lread.c @@ -2120,6 +2120,15 @@ read0 (Lisp_Object readcharfun) static ptrdiff_t read_buffer_size; static char *read_buffer; +/* Grow the read buffer by at least MAX_MULTIBYTE_LENGTH bytes. */ + +static void +grow_read_buffer (void) +{ + read_buffer = xpalloc (read_buffer, &read_buffer_size, + MAX_MULTIBYTE_LENGTH, -1, 1); +} + /* Read a \-escape sequence, assuming we already read the `\'. If the escape sequence forces unibyte, return eight-bit char. */ @@ -2985,10 +2994,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list) if (end - p < MAX_MULTIBYTE_LENGTH) { ptrdiff_t offset = p - read_buffer; - if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size) - memory_full (SIZE_MAX); - read_buffer = xrealloc (read_buffer, read_buffer_size * 2); - read_buffer_size *= 2; + grow_read_buffer (); p = read_buffer + offset; end = read_buffer + read_buffer_size; } @@ -3119,10 +3125,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list) if (end - p < MAX_MULTIBYTE_LENGTH) { ptrdiff_t offset = p - read_buffer; - if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size) - memory_full (SIZE_MAX); - read_buffer = xrealloc (read_buffer, read_buffer_size * 2); - read_buffer_size *= 2; + grow_read_buffer (); p = read_buffer + offset; end = read_buffer + read_buffer_size; } @@ -3149,10 +3152,7 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list) if (p == end) { ptrdiff_t offset = p - read_buffer; - if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < read_buffer_size) - memory_full (SIZE_MAX); - read_buffer = xrealloc (read_buffer, read_buffer_size * 2); - read_buffer_size *= 2; + grow_read_buffer (); p = read_buffer + offset; end = read_buffer + read_buffer_size; } diff --git a/src/macros.c b/src/macros.c index d963838069..7c6ab2efc3 100644 --- a/src/macros.c +++ b/src/macros.c @@ -184,16 +184,11 @@ store_kbd_macro_char (Lisp_Object c) { if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize) { - ptrdiff_t ptr_offset, end_offset, nbytes; - - ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer; - end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer; - if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *kb->kbd_macro_buffer / 2 - < kb->kbd_macro_bufsize) - memory_full (SIZE_MAX); - nbytes = kb->kbd_macro_bufsize * (2 * sizeof *kb->kbd_macro_buffer); - kb->kbd_macro_buffer = xrealloc (kb->kbd_macro_buffer, nbytes); - kb->kbd_macro_bufsize *= 2; + ptrdiff_t ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer; + ptrdiff_t end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer; + kb->kbd_macro_buffer = xpalloc (kb->kbd_macro_buffer, + &kb->kbd_macro_bufsize, + 1, -1, sizeof *kb->kbd_macro_buffer); kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset; kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset; } diff --git a/src/minibuf.c b/src/minibuf.c index 31b69461bd..727a70b166 100644 --- a/src/minibuf.c +++ b/src/minibuf.c @@ -229,12 +229,7 @@ read_minibuf_noninteractive (Lisp_Object map, Lisp_Object initial, if (hide_char) fprintf (stdout, "%c", hide_char); if (len == size) - { - if (STRING_BYTES_BOUND / 2 < size) - memory_full (SIZE_MAX); - size *= 2; - line = xrealloc (line, size); - } + line = xpalloc (line, &size, 1, -1, sizeof *line); line[len++] = c; } } diff --git a/src/term.c b/src/term.c index 9b1e7cad4b..245712ecfc 100644 --- a/src/term.c +++ b/src/term.c @@ -537,10 +537,10 @@ encode_terminal_code (struct glyph *src, int src_len, required = src_len; required *= MAX_MULTIBYTE_LENGTH; if (encode_terminal_src_size < required) - { - encode_terminal_src = xrealloc (encode_terminal_src, required); - encode_terminal_src_size = required; - } + encode_terminal_src = xpalloc (encode_terminal_src, + &encode_terminal_src_size, + required - encode_terminal_src_size, + -1, sizeof *encode_terminal_src); charset_list = coding_charset_list (coding); diff --git a/src/xrdb.c b/src/xrdb.c index ce6e7d21ed..10bc76986e 100644 --- a/src/xrdb.c +++ b/src/xrdb.c @@ -177,12 +177,8 @@ magic_db (const char *string, ptrdiff_t string_len, const char *class, /* Do we have room for this component followed by a '\0'? */ if (path_size - path_len <= next_len) - { - if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len) - memory_full (SIZE_MAX); - path_size = (path_len + next_len + 1) * 2; - path = xrealloc (path, path_size); - } + path = xpalloc (path, &path_size, path_len - path_size + next_len + 1, + -1, sizeof *path); memcpy (path + path_len, next, next_len); path_len += next_len; -- 2.39.2