diff options
| author | Paul Eggert | 2014-09-07 00:04:01 -0700 |
|---|---|---|
| committer | Paul Eggert | 2014-09-07 00:04:01 -0700 |
| commit | b3bf18b3b87ac8f00857b8bfc3f2c74cf0e2fb7d (patch) | |
| tree | cf138164e4f8887394f52cb22da594d1713da316 /src/editfns.c | |
| parent | 930fb80f9e2815e599eb1de699668d42e305fa21 (diff) | |
| download | emacs-b3bf18b3b87ac8f00857b8bfc3f2c74cf0e2fb7d.tar.gz emacs-b3bf18b3b87ac8f00857b8bfc3f2c74cf0e2fb7d.zip | |
Use SAFE_ALLOCA etc. to avoid unbounded stack allocation.
This follows up on the recent thread in emacs-devel on alloca; see:
http://lists.gnu.org/archive/html/emacs-devel/2014-09/msg00042.html
This patch also cleans up alloca-related glitches noted while
examining the code looking for unbounded alloca.
* alloc.c (listn):
* callproc.c (init_callproc):
Rewrite to avoid need for alloca.
* buffer.c (mouse_face_overlay_overlaps)
(report_overlay_modification):
* buffer.h (GET_OVERLAYS_AT):
* coding.c (make_subsidiaries):
* doc.c (Fsnarf_documentation):
* editfns.c (Fuser_full_name):
* fileio.c (Ffile_name_directory, Fexpand_file_name)
(search_embedded_absfilename, Fsubstitute_in_file_name):
* fns.c (Fmake_hash_table):
* font.c (font_vconcat_entity_vectors, font_update_drivers):
* fontset.c (fontset_pattern_regexp, Ffontset_info):
* frame.c (Fmake_terminal_frame, x_set_frame_parameters)
(xrdb_get_resource, x_get_resource_string):
* ftfont.c (ftfont_get_charset, ftfont_check_otf, ftfont_drive_otf):
* ftxfont.c (ftxfont_draw):
* image.c (xbm_load, xpm_load, jpeg_load_body):
* keyboard.c (echo_add_key, menu_bar_items, tool_bar_items):
* keymap.c (Fdescribe_buffer_bindings, describe_map):
* lread.c (openp):
* menu.c (digest_single_submenu, find_and_call_menu_selection)
(find_and_return_menu_selection):
* print.c (PRINTFINISH):
* process.c (Fformat_network_address):
* scroll.c (do_scrolling, do_direct_scrolling, scrolling_1):
* search.c (search_buffer, Fmatch_data, Fregexp_quote):
* sound.c (wav_play, au_play):
* syntax.c (skip_chars):
* term.c (tty_menu_activate, tty_menu_show):
* textprop.c (get_char_property_and_overlay):
* window.c (Fset_window_configuration):
* xdisp.c (safe__call, next_overlay_change, vmessage)
(compute_overhangs_and_x, draw_glyphs, note_mouse_highlight):
* xfaces.c (face_at_buffer_position):
* xmenu.c (x_menu_show):
Use SAFE_ALLOCA etc. instead of plain alloca, since the
allocation size isn't bounded.
* callint.c (Fcall_interactively): Redo memory_full check
so that it can be done at compile-time on some platforms.
* coding.c (MAX_LOOKUP_MAX): New constant.
(get_translation_table): Use it.
* callproc.c (call_process): Use SAFE_NALLOCA instead of
SAFE_ALLOCA, to catch integer overflows on size calculation.
(exec_failed) [!DOS_NT]: New function.
(child_setup) [!DOS_NT]: Use it.
* editfns.c (Ftranspose_regions):
Hoist USE_SAFE_ALLOC + SAFE_FREE out of 'if'.
* editfns.c (check_translation):
Allocate larger buffers on the heap.
* eval.c (internal_lisp_condition_case):
Check for MAX_ALLOCA overflow.
* fns.c (sort_vector): Use SAFE_ALLOCA_LISP rather than Fmake_vector.
(Fbase64_encode_region, Fbase64_decode_region):
Avoid unnecessary calls to SAFE_FREE before 'error'.
* buffer.c (mouse_face_overlay_overlaps):
* editfns.c (Fget_pos_property, check_translation):
* eval.c (Ffuncall):
* font.c (font_unparse_xlfd, font_find_for_lface):
* ftfont.c (ftfont_drive_otf):
* keyboard.c (echo_add_key, read_decoded_event_from_main_queue)
(menu_bar_items, tool_bar_items):
* sound.c (Fplay_sound_internal):
* xdisp.c (load_overlay_strings, dump_glyph_row):
Use an ordinary auto buffer rather than alloca, since the
allocation size is fixed and small.
* ftfont.c: Include <c-strcase.h>.
(matching_prefix): New function.
(get_adstyle_property): Use it, to avoid need for alloca.
* keyboard.c (echo_add_key):
* keymap.c (describe_map): Use ptrdiff_t, not int.
* keyboard.c (echo_add_key): Prefer sizeof to strlen.
* keymap.c (Fdescribe_buffer_bindings): Use SBYTES, not SCHARS,
when counting bytes.
* lisp.h (xlispstrdupa): Remove, replacing with ...
(SAFE_ALLOCA_STRING): ... new macro with different API.
This fixes a portability problem, namely, alloca result
passed to another function. All uses changed.
(SAFE_ALLOCA, SAFE_ALLOCA_LISP): Check for MAX_ALLOCA,
not MAX_ALLOCA - 1.
* regex.c (REGEX_USE_SAFE_ALLOCA, REGEX_SAFE_FREE)
(REGEX_ALLOCATE): New macros.
(REGEX_REALLOCATE, REGEX_ALLOCATE_STACK, REGEX_REALLOCATE_STACK)
(REGEX_FREE_STACK, FREE_VARIABLES, re_match_2_internal):
Use them.
* xdisp.c (message3): Use SAFE_ALLOCA_STRING rather than doing it
by hand.
(decode_mode_spec_coding): Store directly into buf rather than
into an alloca temporary and copying the temporary to the buf.
Fixes: debbugs:18410
Diffstat (limited to 'src/editfns.c')
| -rw-r--r-- | src/editfns.c | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/src/editfns.c b/src/editfns.c index a906aead09a..7e9a3bf7d3c 100644 --- a/src/editfns.c +++ b/src/editfns.c | |||
| @@ -376,13 +376,14 @@ at POSITION. */) | |||
| 376 | set_buffer_temp (XBUFFER (object)); | 376 | set_buffer_temp (XBUFFER (object)); |
| 377 | 377 | ||
| 378 | /* First try with room for 40 overlays. */ | 378 | /* First try with room for 40 overlays. */ |
| 379 | noverlays = 40; | 379 | Lisp_Object overlay_vecbuf[40]; |
| 380 | overlay_vec = alloca (noverlays * sizeof *overlay_vec); | 380 | noverlays = ARRAYELTS (overlay_vecbuf); |
| 381 | overlay_vec = overlay_vecbuf; | ||
| 381 | noverlays = overlays_around (posn, overlay_vec, noverlays); | 382 | noverlays = overlays_around (posn, overlay_vec, noverlays); |
| 382 | 383 | ||
| 383 | /* If there are more than 40, | 384 | /* If there are more than 40, |
| 384 | make enough space for all, and try again. */ | 385 | make enough space for all, and try again. */ |
| 385 | if (noverlays > 40) | 386 | if (ARRAYELTS (overlay_vecbuf) < noverlays) |
| 386 | { | 387 | { |
| 387 | SAFE_ALLOCA_LISP (overlay_vec, noverlays); | 388 | SAFE_ALLOCA_LISP (overlay_vec, noverlays); |
| 388 | noverlays = overlays_around (posn, overlay_vec, noverlays); | 389 | noverlays = overlays_around (posn, overlay_vec, noverlays); |
| @@ -1325,17 +1326,16 @@ name, or nil if there is no such user. */) | |||
| 1325 | /* Substitute the login name for the &, upcasing the first character. */ | 1326 | /* Substitute the login name for the &, upcasing the first character. */ |
| 1326 | if (q) | 1327 | if (q) |
| 1327 | { | 1328 | { |
| 1328 | register char *r; | 1329 | Lisp_Object login = Fuser_login_name (make_number (pw->pw_uid)); |
| 1329 | Lisp_Object login; | 1330 | USE_SAFE_ALLOCA; |
| 1330 | 1331 | char *r = SAFE_ALLOCA (strlen (p) + SBYTES (login) + 1); | |
| 1331 | login = Fuser_login_name (make_number (pw->pw_uid)); | ||
| 1332 | r = alloca (strlen (p) + SCHARS (login) + 1); | ||
| 1333 | memcpy (r, p, q - p); | 1332 | memcpy (r, p, q - p); |
| 1334 | r[q - p] = 0; | 1333 | r[q - p] = 0; |
| 1335 | strcat (r, SSDATA (login)); | 1334 | strcat (r, SSDATA (login)); |
| 1336 | r[q - p] = upcase ((unsigned char) r[q - p]); | 1335 | r[q - p] = upcase ((unsigned char) r[q - p]); |
| 1337 | strcat (r, q + 1); | 1336 | strcat (r, q + 1); |
| 1338 | full = build_string (r); | 1337 | full = build_string (r); |
| 1338 | SAFE_FREE (); | ||
| 1339 | } | 1339 | } |
| 1340 | #endif /* AMPERSAND_FULL_NAME */ | 1340 | #endif /* AMPERSAND_FULL_NAME */ |
| 1341 | 1341 | ||
| @@ -3012,8 +3012,12 @@ static Lisp_Object | |||
| 3012 | check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end, | 3012 | check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end, |
| 3013 | Lisp_Object val) | 3013 | Lisp_Object val) |
| 3014 | { | 3014 | { |
| 3015 | int buf_size = 16, buf_used = 0; | 3015 | int initial_buf[16]; |
| 3016 | int *buf = alloca (sizeof (int) * buf_size); | 3016 | int *buf = initial_buf; |
| 3017 | ptrdiff_t buf_size = ARRAYELTS (initial_buf); | ||
| 3018 | int *bufalloc = 0; | ||
| 3019 | ptrdiff_t buf_used = 0; | ||
| 3020 | Lisp_Object result = Qnil; | ||
| 3017 | 3021 | ||
| 3018 | for (; CONSP (val); val = XCDR (val)) | 3022 | for (; CONSP (val); val = XCDR (val)) |
| 3019 | { | 3023 | { |
| @@ -3038,12 +3042,11 @@ check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end, | |||
| 3038 | 3042 | ||
| 3039 | if (buf_used == buf_size) | 3043 | if (buf_used == buf_size) |
| 3040 | { | 3044 | { |
| 3041 | int *newbuf; | 3045 | bufalloc = xpalloc (bufalloc, &buf_size, 1, -1, |
| 3042 | 3046 | sizeof *bufalloc); | |
| 3043 | buf_size += 16; | 3047 | if (buf == initial_buf) |
| 3044 | newbuf = alloca (sizeof (int) * buf_size); | 3048 | memcpy (bufalloc, buf, sizeof initial_buf); |
| 3045 | memcpy (newbuf, buf, sizeof (int) * buf_used); | 3049 | buf = bufalloc; |
| 3046 | buf = newbuf; | ||
| 3047 | } | 3050 | } |
| 3048 | buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1); | 3051 | buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1); |
| 3049 | pos_byte += len1; | 3052 | pos_byte += len1; |
| @@ -3052,10 +3055,15 @@ check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end, | |||
| 3052 | break; | 3055 | break; |
| 3053 | } | 3056 | } |
| 3054 | if (i == len) | 3057 | if (i == len) |
| 3055 | return XCAR (val); | 3058 | { |
| 3059 | result = XCAR (val); | ||
| 3060 | break; | ||
| 3061 | } | ||
| 3056 | } | 3062 | } |
| 3057 | } | 3063 | } |
| 3058 | return Qnil; | 3064 | |
| 3065 | xfree (bufalloc); | ||
| 3066 | return result; | ||
| 3059 | } | 3067 | } |
| 3060 | 3068 | ||
| 3061 | 3069 | ||
| @@ -4617,11 +4625,11 @@ Transposing beyond buffer boundaries is an error. */) | |||
| 4617 | if (tmp_interval3) | 4625 | if (tmp_interval3) |
| 4618 | set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3); | 4626 | set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3); |
| 4619 | 4627 | ||
| 4628 | USE_SAFE_ALLOCA; | ||
| 4629 | |||
| 4620 | /* First region smaller than second. */ | 4630 | /* First region smaller than second. */ |
| 4621 | if (len1_byte < len2_byte) | 4631 | if (len1_byte < len2_byte) |
| 4622 | { | 4632 | { |
| 4623 | USE_SAFE_ALLOCA; | ||
| 4624 | |||
| 4625 | temp = SAFE_ALLOCA (len2_byte); | 4633 | temp = SAFE_ALLOCA (len2_byte); |
| 4626 | 4634 | ||
| 4627 | /* Don't precompute these addresses. We have to compute them | 4635 | /* Don't precompute these addresses. We have to compute them |
| @@ -4633,21 +4641,19 @@ Transposing beyond buffer boundaries is an error. */) | |||
| 4633 | memcpy (temp, start2_addr, len2_byte); | 4641 | memcpy (temp, start2_addr, len2_byte); |
| 4634 | memcpy (start1_addr + len2_byte, start1_addr, len1_byte); | 4642 | memcpy (start1_addr + len2_byte, start1_addr, len1_byte); |
| 4635 | memcpy (start1_addr, temp, len2_byte); | 4643 | memcpy (start1_addr, temp, len2_byte); |
| 4636 | SAFE_FREE (); | ||
| 4637 | } | 4644 | } |
| 4638 | else | 4645 | else |
| 4639 | /* First region not smaller than second. */ | 4646 | /* First region not smaller than second. */ |
| 4640 | { | 4647 | { |
| 4641 | USE_SAFE_ALLOCA; | ||
| 4642 | |||
| 4643 | temp = SAFE_ALLOCA (len1_byte); | 4648 | temp = SAFE_ALLOCA (len1_byte); |
| 4644 | start1_addr = BYTE_POS_ADDR (start1_byte); | 4649 | start1_addr = BYTE_POS_ADDR (start1_byte); |
| 4645 | start2_addr = BYTE_POS_ADDR (start2_byte); | 4650 | start2_addr = BYTE_POS_ADDR (start2_byte); |
| 4646 | memcpy (temp, start1_addr, len1_byte); | 4651 | memcpy (temp, start1_addr, len1_byte); |
| 4647 | memcpy (start1_addr, start2_addr, len2_byte); | 4652 | memcpy (start1_addr, start2_addr, len2_byte); |
| 4648 | memcpy (start1_addr + len2_byte, temp, len1_byte); | 4653 | memcpy (start1_addr + len2_byte, temp, len1_byte); |
| 4649 | SAFE_FREE (); | ||
| 4650 | } | 4654 | } |
| 4655 | |||
| 4656 | SAFE_FREE (); | ||
| 4651 | graft_intervals_into_buffer (tmp_interval1, start1 + len2, | 4657 | graft_intervals_into_buffer (tmp_interval1, start1 + len2, |
| 4652 | len1, current_buffer, 0); | 4658 | len1, current_buffer, 0); |
| 4653 | graft_intervals_into_buffer (tmp_interval2, start1, | 4659 | graft_intervals_into_buffer (tmp_interval2, start1, |