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/keymap.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/keymap.c')
| -rw-r--r-- | src/keymap.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/keymap.c b/src/keymap.c index f4dd644aebd..f7256bfde2a 100644 --- a/src/keymap.c +++ b/src/keymap.c | |||
| @@ -2883,13 +2883,14 @@ You type Translation\n\ | |||
| 2883 | if (!SYMBOLP (modes[i])) | 2883 | if (!SYMBOLP (modes[i])) |
| 2884 | emacs_abort (); | 2884 | emacs_abort (); |
| 2885 | 2885 | ||
| 2886 | p = title = alloca (42 + SCHARS (SYMBOL_NAME (modes[i]))); | 2886 | USE_SAFE_ALLOCA; |
| 2887 | p = title = SAFE_ALLOCA (42 + SBYTES (SYMBOL_NAME (modes[i]))); | ||
| 2887 | *p++ = '\f'; | 2888 | *p++ = '\f'; |
| 2888 | *p++ = '\n'; | 2889 | *p++ = '\n'; |
| 2889 | *p++ = '`'; | 2890 | *p++ = '`'; |
| 2890 | memcpy (p, SDATA (SYMBOL_NAME (modes[i])), | 2891 | memcpy (p, SDATA (SYMBOL_NAME (modes[i])), |
| 2891 | SCHARS (SYMBOL_NAME (modes[i]))); | 2892 | SBYTES (SYMBOL_NAME (modes[i]))); |
| 2892 | p += SCHARS (SYMBOL_NAME (modes[i])); | 2893 | p += SBYTES (SYMBOL_NAME (modes[i])); |
| 2893 | *p++ = '\''; | 2894 | *p++ = '\''; |
| 2894 | memcpy (p, " Minor Mode Bindings", strlen (" Minor Mode Bindings")); | 2895 | memcpy (p, " Minor Mode Bindings", strlen (" Minor Mode Bindings")); |
| 2895 | p += strlen (" Minor Mode Bindings"); | 2896 | p += strlen (" Minor Mode Bindings"); |
| @@ -2898,6 +2899,7 @@ You type Translation\n\ | |||
| 2898 | describe_map_tree (maps[i], 1, shadow, prefix, | 2899 | describe_map_tree (maps[i], 1, shadow, prefix, |
| 2899 | title, nomenu, 0, 0, 0); | 2900 | title, nomenu, 0, 0, 0); |
| 2900 | shadow = Fcons (maps[i], shadow); | 2901 | shadow = Fcons (maps[i], shadow); |
| 2902 | SAFE_FREE (); | ||
| 2901 | } | 2903 | } |
| 2902 | 2904 | ||
| 2903 | start1 = get_local_map (BUF_PT (XBUFFER (buffer)), | 2905 | start1 = get_local_map (BUF_PT (XBUFFER (buffer)), |
| @@ -3184,10 +3186,10 @@ describe_map (Lisp_Object map, Lisp_Object prefix, | |||
| 3184 | 3186 | ||
| 3185 | /* These accumulate the values from sparse keymap bindings, | 3187 | /* These accumulate the values from sparse keymap bindings, |
| 3186 | so we can sort them and handle them in order. */ | 3188 | so we can sort them and handle them in order. */ |
| 3187 | int length_needed = 0; | 3189 | ptrdiff_t length_needed = 0; |
| 3188 | struct describe_map_elt *vect; | 3190 | struct describe_map_elt *vect; |
| 3189 | int slots_used = 0; | 3191 | ptrdiff_t slots_used = 0; |
| 3190 | int i; | 3192 | ptrdiff_t i; |
| 3191 | 3193 | ||
| 3192 | suppress = Qnil; | 3194 | suppress = Qnil; |
| 3193 | 3195 | ||
| @@ -3207,7 +3209,8 @@ describe_map (Lisp_Object map, Lisp_Object prefix, | |||
| 3207 | for (tail = map; CONSP (tail); tail = XCDR (tail)) | 3209 | for (tail = map; CONSP (tail); tail = XCDR (tail)) |
| 3208 | length_needed++; | 3210 | length_needed++; |
| 3209 | 3211 | ||
| 3210 | vect = alloca (length_needed * sizeof *vect); | 3212 | USE_SAFE_ALLOCA; |
| 3213 | SAFE_NALLOCA (vect, 1, length_needed); | ||
| 3211 | 3214 | ||
| 3212 | for (tail = map; CONSP (tail); tail = XCDR (tail)) | 3215 | for (tail = map; CONSP (tail); tail = XCDR (tail)) |
| 3213 | { | 3216 | { |
| @@ -3350,6 +3353,7 @@ describe_map (Lisp_Object map, Lisp_Object prefix, | |||
| 3350 | } | 3353 | } |
| 3351 | } | 3354 | } |
| 3352 | 3355 | ||
| 3356 | SAFE_FREE (); | ||
| 3353 | UNGCPRO; | 3357 | UNGCPRO; |
| 3354 | } | 3358 | } |
| 3355 | 3359 | ||