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/callint.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/callint.c')
| -rw-r--r-- | src/callint.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/callint.c b/src/callint.c index 817f84d897b..9a4573c77be 100644 --- a/src/callint.c +++ b/src/callint.c | |||
| @@ -297,6 +297,7 @@ invoke it. If KEYS is omitted or nil, the return value of | |||
| 297 | Lisp_Object teml; | 297 | Lisp_Object teml; |
| 298 | Lisp_Object up_event; | 298 | Lisp_Object up_event; |
| 299 | Lisp_Object enable; | 299 | Lisp_Object enable; |
| 300 | USE_SAFE_ALLOCA; | ||
| 300 | ptrdiff_t speccount = SPECPDL_INDEX (); | 301 | ptrdiff_t speccount = SPECPDL_INDEX (); |
| 301 | 302 | ||
| 302 | /* The index of the next element of this_command_keys to examine for | 303 | /* The index of the next element of this_command_keys to examine for |
| @@ -366,12 +367,8 @@ invoke it. If KEYS is omitted or nil, the return value of | |||
| 366 | wrong_type_argument (Qcommandp, function); | 367 | wrong_type_argument (Qcommandp, function); |
| 367 | } | 368 | } |
| 368 | 369 | ||
| 369 | /* If SPECS is set to a string, use it as an interactive prompt. */ | 370 | /* If SPECS is not a string, invent one. */ |
| 370 | if (STRINGP (specs)) | 371 | if (! STRINGP (specs)) |
| 371 | /* Make a copy of string so that if a GC relocates specs, | ||
| 372 | `string' will still be valid. */ | ||
| 373 | string = xlispstrdupa (specs); | ||
| 374 | else | ||
| 375 | { | 372 | { |
| 376 | Lisp_Object input; | 373 | Lisp_Object input; |
| 377 | Lisp_Object funval = Findirect_function (function, Qt); | 374 | Lisp_Object funval = Findirect_function (function, Qt); |
| @@ -416,10 +413,16 @@ invoke it. If KEYS is omitted or nil, the return value of | |||
| 416 | args[0] = Qfuncall_interactively; | 413 | args[0] = Qfuncall_interactively; |
| 417 | args[1] = function; | 414 | args[1] = function; |
| 418 | args[2] = specs; | 415 | args[2] = specs; |
| 419 | return unbind_to (speccount, Fapply (3, args)); | 416 | Lisp_Object result = unbind_to (speccount, Fapply (3, args)); |
| 417 | SAFE_FREE (); | ||
| 418 | return result; | ||
| 420 | } | 419 | } |
| 421 | } | 420 | } |
| 422 | 421 | ||
| 422 | /* SPECS is set to a string; use it as an interactive prompt. | ||
| 423 | Copy it so that STRING will be valid even if a GC relocates SPECS. */ | ||
| 424 | SAFE_ALLOCA_STRING (string, specs); | ||
| 425 | |||
| 423 | /* Here if function specifies a string to control parsing the defaults. */ | 426 | /* Here if function specifies a string to control parsing the defaults. */ |
| 424 | 427 | ||
| 425 | /* Set next_event to point to the first event with parameters. */ | 428 | /* Set next_event to point to the first event with parameters. */ |
| @@ -507,14 +510,15 @@ invoke it. If KEYS is omitted or nil, the return value of | |||
| 507 | break; | 510 | break; |
| 508 | } | 511 | } |
| 509 | 512 | ||
| 510 | if (min (MOST_POSITIVE_FIXNUM, | 513 | if (MOST_POSITIVE_FIXNUM < min (PTRDIFF_MAX, SIZE_MAX) / word_size |
| 511 | min (PTRDIFF_MAX, SIZE_MAX) / word_size) | 514 | && MOST_POSITIVE_FIXNUM < nargs) |
| 512 | < nargs) | ||
| 513 | memory_full (SIZE_MAX); | 515 | memory_full (SIZE_MAX); |
| 514 | 516 | ||
| 515 | args = alloca (nargs * sizeof *args); | 517 | /* Allocate them all at one go. This wastes a bit of memory, but |
| 516 | visargs = alloca (nargs * sizeof *visargs); | 518 | it's OK to trade space for speed. */ |
| 517 | varies = alloca (nargs * sizeof *varies); | 519 | SAFE_NALLOCA (args, 3, nargs); |
| 520 | visargs = args + nargs; | ||
| 521 | varies = (signed char *) (visargs + nargs); | ||
| 518 | 522 | ||
| 519 | for (i = 0; i < nargs; i++) | 523 | for (i = 0; i < nargs; i++) |
| 520 | { | 524 | { |
| @@ -871,7 +875,9 @@ invoke it. If KEYS is omitted or nil, the return value of | |||
| 871 | { | 875 | { |
| 872 | Lisp_Object val = Ffuncall (nargs, args); | 876 | Lisp_Object val = Ffuncall (nargs, args); |
| 873 | UNGCPRO; | 877 | UNGCPRO; |
| 874 | return unbind_to (speccount, val); | 878 | val = unbind_to (speccount, val); |
| 879 | SAFE_FREE (); | ||
| 880 | return val; | ||
| 875 | } | 881 | } |
| 876 | } | 882 | } |
| 877 | 883 | ||