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/scroll.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/scroll.c')
| -rw-r--r-- | src/scroll.c | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/scroll.c b/src/scroll.c index 6c559663f80..7cb683c4577 100644 --- a/src/scroll.c +++ b/src/scroll.c | |||
| @@ -245,18 +245,20 @@ do_scrolling (struct frame *frame, struct glyph_matrix *current_matrix, | |||
| 245 | { | 245 | { |
| 246 | struct matrix_elt *p; | 246 | struct matrix_elt *p; |
| 247 | int i, j, k; | 247 | int i, j, k; |
| 248 | USE_SAFE_ALLOCA; | ||
| 248 | 249 | ||
| 249 | /* True if we have set a terminal window with set_terminal_window. */ | 250 | /* True if we have set a terminal window with set_terminal_window. */ |
| 250 | bool terminal_window_p = 0; | 251 | bool terminal_window_p = 0; |
| 251 | 252 | ||
| 252 | /* A queue for line insertions to be done. */ | 253 | /* A queue for line insertions to be done. */ |
| 253 | struct queue { int count, pos; }; | 254 | struct queue { int count, pos; }; |
| 254 | struct queue *queue_start | 255 | struct queue *queue_start; |
| 255 | = alloca (current_matrix->nrows * sizeof *queue_start); | 256 | SAFE_NALLOCA (queue_start, 1, current_matrix->nrows); |
| 256 | struct queue *queue = queue_start; | 257 | struct queue *queue = queue_start; |
| 257 | 258 | ||
| 258 | char *retained_p = alloca (window_size * sizeof *retained_p); | 259 | char *retained_p = SAFE_ALLOCA (window_size); |
| 259 | int *copy_from = alloca (window_size * sizeof *copy_from); | 260 | int *copy_from; |
| 261 | SAFE_NALLOCA (copy_from, 1, window_size); | ||
| 260 | 262 | ||
| 261 | /* Zero means line is empty. */ | 263 | /* Zero means line is empty. */ |
| 262 | memset (retained_p, 0, window_size * sizeof (char)); | 264 | memset (retained_p, 0, window_size * sizeof (char)); |
| @@ -378,6 +380,7 @@ do_scrolling (struct frame *frame, struct glyph_matrix *current_matrix, | |||
| 378 | 380 | ||
| 379 | if (terminal_window_p) | 381 | if (terminal_window_p) |
| 380 | set_terminal_window (frame, 0); | 382 | set_terminal_window (frame, 0); |
| 383 | SAFE_FREE (); | ||
| 381 | } | 384 | } |
| 382 | 385 | ||
| 383 | 386 | ||
| @@ -649,10 +652,12 @@ do_direct_scrolling (struct frame *frame, struct glyph_matrix *current_matrix, | |||
| 649 | { | 652 | { |
| 650 | struct matrix_elt *p; | 653 | struct matrix_elt *p; |
| 651 | int i, j; | 654 | int i, j; |
| 655 | USE_SAFE_ALLOCA; | ||
| 652 | 656 | ||
| 653 | /* A queue of deletions and insertions to be performed. */ | 657 | /* A queue of deletions and insertions to be performed. */ |
| 654 | struct alt_queue { int count, pos, window; }; | 658 | struct alt_queue { int count, pos, window; }; |
| 655 | struct alt_queue *queue_start = alloca (window_size * sizeof *queue_start); | 659 | struct alt_queue *queue_start; |
| 660 | SAFE_NALLOCA (queue_start, 1, window_size); | ||
| 656 | struct alt_queue *queue = queue_start; | 661 | struct alt_queue *queue = queue_start; |
| 657 | 662 | ||
| 658 | /* True if a terminal window has been set with set_terminal_window. */ | 663 | /* True if a terminal window has been set with set_terminal_window. */ |
| @@ -667,11 +672,12 @@ do_direct_scrolling (struct frame *frame, struct glyph_matrix *current_matrix, | |||
| 667 | bool write_follows_p = 1; | 672 | bool write_follows_p = 1; |
| 668 | 673 | ||
| 669 | /* For each row in the new matrix what row of the old matrix it is. */ | 674 | /* For each row in the new matrix what row of the old matrix it is. */ |
| 670 | int *copy_from = alloca (window_size * sizeof *copy_from); | 675 | int *copy_from; |
| 676 | SAFE_NALLOCA (copy_from, 1, window_size); | ||
| 671 | 677 | ||
| 672 | /* Non-zero for each row in the new matrix that is retained from the | 678 | /* Non-zero for each row in the new matrix that is retained from the |
| 673 | old matrix. Lines not retained are empty. */ | 679 | old matrix. Lines not retained are empty. */ |
| 674 | char *retained_p = alloca (window_size * sizeof *retained_p); | 680 | char *retained_p = SAFE_ALLOCA (window_size); |
| 675 | 681 | ||
| 676 | memset (retained_p, 0, window_size * sizeof (char)); | 682 | memset (retained_p, 0, window_size * sizeof (char)); |
| 677 | 683 | ||
| @@ -787,6 +793,7 @@ do_direct_scrolling (struct frame *frame, struct glyph_matrix *current_matrix, | |||
| 787 | 793 | ||
| 788 | if (terminal_window_p) | 794 | if (terminal_window_p) |
| 789 | set_terminal_window (frame, 0); | 795 | set_terminal_window (frame, 0); |
| 796 | SAFE_FREE (); | ||
| 790 | } | 797 | } |
| 791 | 798 | ||
| 792 | 799 | ||
| @@ -796,8 +803,9 @@ scrolling_1 (struct frame *frame, int window_size, int unchanged_at_top, | |||
| 796 | int unchanged_at_bottom, int *draw_cost, int *old_draw_cost, | 803 | int unchanged_at_bottom, int *draw_cost, int *old_draw_cost, |
| 797 | unsigned *old_hash, unsigned *new_hash, int free_at_end) | 804 | unsigned *old_hash, unsigned *new_hash, int free_at_end) |
| 798 | { | 805 | { |
| 799 | struct matrix_elt *matrix | 806 | USE_SAFE_ALLOCA; |
| 800 | = alloca ((window_size + 1) * (window_size + 1) * sizeof *matrix); | 807 | struct matrix_elt *matrix; |
| 808 | SAFE_NALLOCA (matrix, window_size + 1, window_size + 1); | ||
| 801 | 809 | ||
| 802 | if (FRAME_SCROLL_REGION_OK (frame)) | 810 | if (FRAME_SCROLL_REGION_OK (frame)) |
| 803 | { | 811 | { |
| @@ -817,6 +825,8 @@ scrolling_1 (struct frame *frame, int window_size, int unchanged_at_top, | |||
| 817 | frame->current_matrix, matrix, window_size, | 825 | frame->current_matrix, matrix, window_size, |
| 818 | unchanged_at_top); | 826 | unchanged_at_top); |
| 819 | } | 827 | } |
| 828 | |||
| 829 | SAFE_FREE (); | ||
| 820 | } | 830 | } |
| 821 | 831 | ||
| 822 | 832 | ||