aboutsummaryrefslogtreecommitdiffstats
path: root/src/lisp.h
diff options
context:
space:
mode:
authorPaul Eggert2014-09-07 00:04:01 -0700
committerPaul Eggert2014-09-07 00:04:01 -0700
commitb3bf18b3b87ac8f00857b8bfc3f2c74cf0e2fb7d (patch)
treecf138164e4f8887394f52cb22da594d1713da316 /src/lisp.h
parent930fb80f9e2815e599eb1de699668d42e305fa21 (diff)
downloademacs-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/lisp.h')
-rw-r--r--src/lisp.h20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/lisp.h b/src/lisp.h
index 53bfd7c4daf..15c459c9fdb 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4451,12 +4451,6 @@ egetenv (const char *var)
4451 return egetenv_internal (var, strlen (var)); 4451 return egetenv_internal (var, strlen (var));
4452} 4452}
4453 4453
4454/* Copy Lisp string to temporary (allocated on stack) C string. */
4455
4456#define xlispstrdupa(string) \
4457 memcpy (alloca (SBYTES (string) + 1), \
4458 SSDATA (string), SBYTES (string) + 1)
4459
4460/* Set up the name of the machine we're running on. */ 4454/* Set up the name of the machine we're running on. */
4461extern void init_system_name (void); 4455extern void init_system_name (void);
4462 4456
@@ -4484,7 +4478,7 @@ extern void *record_xmalloc (size_t) ATTRIBUTE_ALLOC_SIZE ((1));
4484 4478
4485/* SAFE_ALLOCA allocates a simple buffer. */ 4479/* SAFE_ALLOCA allocates a simple buffer. */
4486 4480
4487#define SAFE_ALLOCA(size) ((size) < MAX_ALLOCA \ 4481#define SAFE_ALLOCA(size) ((size) <= MAX_ALLOCA \
4488 ? alloca (size) \ 4482 ? alloca (size) \
4489 : (sa_must_free = true, record_xmalloc (size))) 4483 : (sa_must_free = true, record_xmalloc (size)))
4490 4484
@@ -4504,6 +4498,14 @@ extern void *record_xmalloc (size_t) ATTRIBUTE_ALLOC_SIZE ((1));
4504 } \ 4498 } \
4505 } while (false) 4499 } while (false)
4506 4500
4501/* SAFE_ALLOCA_STRING allocates a C copy of a Lisp string. */
4502
4503#define SAFE_ALLOCA_STRING(ptr, string) \
4504 do { \
4505 (ptr) = SAFE_ALLOCA (SBYTES (string) + 1); \
4506 memcpy (ptr, SDATA (string), SBYTES (string) + 1); \
4507 } while (false)
4508
4507/* SAFE_FREE frees xmalloced memory and enables GC as needed. */ 4509/* SAFE_FREE frees xmalloced memory and enables GC as needed. */
4508 4510
4509#define SAFE_FREE() \ 4511#define SAFE_FREE() \
@@ -4519,9 +4521,9 @@ extern void *record_xmalloc (size_t) ATTRIBUTE_ALLOC_SIZE ((1));
4519 4521
4520#define SAFE_ALLOCA_LISP(buf, nelt) \ 4522#define SAFE_ALLOCA_LISP(buf, nelt) \
4521 do { \ 4523 do { \
4522 if ((nelt) < MAX_ALLOCA / word_size) \ 4524 if ((nelt) <= MAX_ALLOCA / word_size) \
4523 (buf) = alloca ((nelt) * word_size); \ 4525 (buf) = alloca ((nelt) * word_size); \
4524 else if ((nelt) < min (PTRDIFF_MAX, SIZE_MAX) / word_size) \ 4526 else if ((nelt) <= min (PTRDIFF_MAX, SIZE_MAX) / word_size) \
4525 { \ 4527 { \
4526 Lisp_Object arg_; \ 4528 Lisp_Object arg_; \
4527 (buf) = xmalloc ((nelt) * word_size); \ 4529 (buf) = xmalloc ((nelt) * word_size); \