aboutsummaryrefslogtreecommitdiffstats
path: root/src/fns.c
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/fns.c
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/fns.c')
-rw-r--r--src/fns.c24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/fns.c b/src/fns.c
index 6cc5cef95df..260bc4e3a12 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1992,17 +1992,14 @@ sort_vector (Lisp_Object vector, Lisp_Object predicate)
1992 return; 1992 return;
1993 ptrdiff_t halflen = len >> 1; 1993 ptrdiff_t halflen = len >> 1;
1994 Lisp_Object *tmp; 1994 Lisp_Object *tmp;
1995 Lisp_Object tmpvec = Qnil; 1995 struct gcpro gcpro1, gcpro2;
1996 struct gcpro gcpro1, gcpro2, gcpro3; 1996 GCPRO2 (vector, predicate);
1997 GCPRO3 (vector, predicate, tmpvec); 1997 USE_SAFE_ALLOCA;
1998 if (halflen < MAX_ALLOCA / word_size) 1998 SAFE_ALLOCA_LISP (tmp, halflen);
1999 tmp = alloca (halflen * word_size); 1999 for (ptrdiff_t i = 0; i < halflen; i++)
2000 else 2000 tmp[i] = make_number (0);
2001 {
2002 tmpvec = Fmake_vector (make_number (halflen), make_number (0));
2003 tmp = XVECTOR (tmpvec)->contents;
2004 }
2005 sort_vector_inplace (predicate, len, XVECTOR (vector)->contents, tmp); 2001 sort_vector_inplace (predicate, len, XVECTOR (vector)->contents, tmp);
2002 SAFE_FREE ();
2006 UNGCPRO; 2003 UNGCPRO;
2007} 2004}
2008 2005
@@ -3289,7 +3286,6 @@ into shorter lines. */)
3289 if (encoded_length < 0) 3286 if (encoded_length < 0)
3290 { 3287 {
3291 /* The encoding wasn't possible. */ 3288 /* The encoding wasn't possible. */
3292 SAFE_FREE ();
3293 error ("Multibyte character in data for base64 encoding"); 3289 error ("Multibyte character in data for base64 encoding");
3294 } 3290 }
3295 3291
@@ -3434,7 +3430,6 @@ If the region can't be decoded, signal an error and don't modify the buffer. */
3434 if (decoded_length < 0) 3430 if (decoded_length < 0)
3435 { 3431 {
3436 /* The decoding wasn't possible. */ 3432 /* The decoding wasn't possible. */
3437 SAFE_FREE ();
3438 error ("Invalid base64 data"); 3433 error ("Invalid base64 data");
3439 } 3434 }
3440 3435
@@ -4581,12 +4576,12 @@ usage: (make-hash-table &rest KEYWORD-ARGS) */)
4581{ 4576{
4582 Lisp_Object test, size, rehash_size, rehash_threshold, weak; 4577 Lisp_Object test, size, rehash_size, rehash_threshold, weak;
4583 struct hash_table_test testdesc; 4578 struct hash_table_test testdesc;
4584 char *used;
4585 ptrdiff_t i; 4579 ptrdiff_t i;
4580 USE_SAFE_ALLOCA;
4586 4581
4587 /* The vector `used' is used to keep track of arguments that 4582 /* The vector `used' is used to keep track of arguments that
4588 have been consumed. */ 4583 have been consumed. */
4589 used = alloca (nargs * sizeof *used); 4584 char *used = SAFE_ALLOCA (nargs * sizeof *used);
4590 memset (used, 0, nargs * sizeof *used); 4585 memset (used, 0, nargs * sizeof *used);
4591 4586
4592 /* See if there's a `:test TEST' among the arguments. */ 4587 /* See if there's a `:test TEST' among the arguments. */
@@ -4653,6 +4648,7 @@ usage: (make-hash-table &rest KEYWORD-ARGS) */)
4653 if (!used[i]) 4648 if (!used[i])
4654 signal_error ("Invalid argument list", args[i]); 4649 signal_error ("Invalid argument list", args[i]);
4655 4650
4651 SAFE_FREE ();
4656 return make_hash_table (testdesc, size, rehash_size, rehash_threshold, weak); 4652 return make_hash_table (testdesc, size, rehash_size, rehash_threshold, weak);
4657} 4653}
4658 4654