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/regex.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/regex.c')
| -rw-r--r-- | src/regex.c | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/src/regex.c b/src/regex.c index 1c1164da57d..9e9018bff88 100644 --- a/src/regex.c +++ b/src/regex.c | |||
| @@ -457,11 +457,17 @@ init_syntax_once (void) | |||
| 457 | 457 | ||
| 458 | # endif /* not alloca */ | 458 | # endif /* not alloca */ |
| 459 | 459 | ||
| 460 | # define REGEX_ALLOCATE alloca | 460 | # ifdef emacs |
| 461 | # define REGEX_USE_SAFE_ALLOCA USE_SAFE_ALLOCA | ||
| 462 | # define REGEX_SAFE_FREE() SAFE_FREE () | ||
| 463 | # define REGEX_ALLOCATE SAFE_ALLOCA | ||
| 464 | # else | ||
| 465 | # define REGEX_ALLOCATE alloca | ||
| 466 | # endif | ||
| 461 | 467 | ||
| 462 | /* Assumes a `char *destination' variable. */ | 468 | /* Assumes a `char *destination' variable. */ |
| 463 | # define REGEX_REALLOCATE(source, osize, nsize) \ | 469 | # define REGEX_REALLOCATE(source, osize, nsize) \ |
| 464 | (destination = alloca (nsize), \ | 470 | (destination = REGEX_ALLOCATE (nsize), \ |
| 465 | memcpy (destination, source, osize)) | 471 | memcpy (destination, source, osize)) |
| 466 | 472 | ||
| 467 | /* No need to do anything to free, after alloca. */ | 473 | /* No need to do anything to free, after alloca. */ |
| @@ -469,6 +475,11 @@ init_syntax_once (void) | |||
| 469 | 475 | ||
| 470 | #endif /* not REGEX_MALLOC */ | 476 | #endif /* not REGEX_MALLOC */ |
| 471 | 477 | ||
| 478 | #ifndef REGEX_USE_SAFE_ALLOCA | ||
| 479 | # define REGEX_USE_SAFE_ALLOCA ((void) 0) | ||
| 480 | # define REGEX_SAFE_FREE() ((void) 0) | ||
| 481 | #endif | ||
| 482 | |||
| 472 | /* Define how to allocate the failure stack. */ | 483 | /* Define how to allocate the failure stack. */ |
| 473 | 484 | ||
| 474 | #if defined REL_ALLOC && defined REGEX_MALLOC | 485 | #if defined REL_ALLOC && defined REGEX_MALLOC |
| @@ -482,22 +493,10 @@ init_syntax_once (void) | |||
| 482 | 493 | ||
| 483 | #else /* not using relocating allocator */ | 494 | #else /* not using relocating allocator */ |
| 484 | 495 | ||
| 485 | # ifdef REGEX_MALLOC | 496 | # define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size) |
| 497 | # define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n) | ||
| 498 | # define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr) | ||
| 486 | 499 | ||
| 487 | # define REGEX_ALLOCATE_STACK malloc | ||
| 488 | # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize) | ||
| 489 | # define REGEX_FREE_STACK free | ||
| 490 | |||
| 491 | # else /* not REGEX_MALLOC */ | ||
| 492 | |||
| 493 | # define REGEX_ALLOCATE_STACK alloca | ||
| 494 | |||
| 495 | # define REGEX_REALLOCATE_STACK(source, osize, nsize) \ | ||
| 496 | REGEX_REALLOCATE (source, osize, nsize) | ||
| 497 | /* No need to explicitly free anything. */ | ||
| 498 | # define REGEX_FREE_STACK(arg) ((void)0) | ||
| 499 | |||
| 500 | # endif /* not REGEX_MALLOC */ | ||
| 501 | #endif /* not using relocating allocator */ | 500 | #endif /* not using relocating allocator */ |
| 502 | 501 | ||
| 503 | 502 | ||
| @@ -4579,6 +4578,7 @@ static int bcmp_translate (re_char *s1, re_char *s2, | |||
| 4579 | FREE_VAR (regend); \ | 4578 | FREE_VAR (regend); \ |
| 4580 | FREE_VAR (best_regstart); \ | 4579 | FREE_VAR (best_regstart); \ |
| 4581 | FREE_VAR (best_regend); \ | 4580 | FREE_VAR (best_regend); \ |
| 4581 | REGEX_SAFE_FREE (); \ | ||
| 4582 | } while (0) | 4582 | } while (0) |
| 4583 | #else | 4583 | #else |
| 4584 | # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */ | 4584 | # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */ |
| @@ -5018,6 +5018,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1, | |||
| 5018 | 5018 | ||
| 5019 | DEBUG_PRINT ("\n\nEntering re_match_2.\n"); | 5019 | DEBUG_PRINT ("\n\nEntering re_match_2.\n"); |
| 5020 | 5020 | ||
| 5021 | REGEX_USE_SAFE_ALLOCA; | ||
| 5022 | |||
| 5021 | INIT_FAIL_STACK (); | 5023 | INIT_FAIL_STACK (); |
| 5022 | 5024 | ||
| 5023 | #ifdef MATCH_MAY_ALLOCATE | 5025 | #ifdef MATCH_MAY_ALLOCATE |