aboutsummaryrefslogtreecommitdiffstats
path: root/src/sound.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/sound.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/sound.c')
-rw-r--r--src/sound.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/sound.c b/src/sound.c
index 7046f4e8e32..9351097138d 100644
--- a/src/sound.c
+++ b/src/sound.c
@@ -564,12 +564,11 @@ wav_play (struct sound *s, struct sound_device *sd)
564 SBYTES (s->data) - sizeof *header); 564 SBYTES (s->data) - sizeof *header);
565 else 565 else
566 { 566 {
567 char *buffer;
568 ptrdiff_t nbytes = 0; 567 ptrdiff_t nbytes = 0;
569 ptrdiff_t blksize = sd->period_size ? sd->period_size (sd) : 2048; 568 ptrdiff_t blksize = sd->period_size ? sd->period_size (sd) : 2048;
570 ptrdiff_t data_left = header->data_length; 569 ptrdiff_t data_left = header->data_length;
571 570 USE_SAFE_ALLOCA;
572 buffer = alloca (blksize); 571 char *buffer = SAFE_ALLOCA (blksize);
573 lseek (s->fd, sizeof *header, SEEK_SET); 572 lseek (s->fd, sizeof *header, SEEK_SET);
574 while (data_left > 0 573 while (data_left > 0
575 && (nbytes = emacs_read (s->fd, buffer, blksize)) > 0) 574 && (nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
@@ -582,6 +581,7 @@ wav_play (struct sound *s, struct sound_device *sd)
582 581
583 if (nbytes < 0) 582 if (nbytes < 0)
584 sound_perror ("Error reading sound file"); 583 sound_perror ("Error reading sound file");
584 SAFE_FREE ();
585 } 585 }
586} 586}
587 587
@@ -656,19 +656,20 @@ au_play (struct sound *s, struct sound_device *sd)
656 else 656 else
657 { 657 {
658 ptrdiff_t blksize = sd->period_size ? sd->period_size (sd) : 2048; 658 ptrdiff_t blksize = sd->period_size ? sd->period_size (sd) : 2048;
659 char *buffer;
660 ptrdiff_t nbytes; 659 ptrdiff_t nbytes;
661 660
662 /* Seek */ 661 /* Seek */
663 lseek (s->fd, header->data_offset, SEEK_SET); 662 lseek (s->fd, header->data_offset, SEEK_SET);
664 663
665 /* Copy sound data to the device. */ 664 /* Copy sound data to the device. */
666 buffer = alloca (blksize); 665 USE_SAFE_ALLOCA;
666 char *buffer = SAFE_ALLOCA (blksize);
667 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0) 667 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
668 sd->write (sd, buffer, nbytes); 668 sd->write (sd, buffer, nbytes);
669 669
670 if (nbytes < 0) 670 if (nbytes < 0)
671 sound_perror ("Error reading sound file"); 671 sound_perror ("Error reading sound file");
672 SAFE_FREE ();
672 } 673 }
673} 674}
674 675
@@ -1309,7 +1310,6 @@ Internal use only, use `play-sound' instead. */)
1309 struct gcpro gcpro1, gcpro2; 1310 struct gcpro gcpro1, gcpro2;
1310 Lisp_Object args[2]; 1311 Lisp_Object args[2];
1311#else /* WINDOWSNT */ 1312#else /* WINDOWSNT */
1312 int len = 0;
1313 Lisp_Object lo_file = {0}; 1313 Lisp_Object lo_file = {0};
1314 char * psz_file = NULL; 1314 char * psz_file = NULL;
1315 unsigned long ui_volume_tmp = UINT_MAX; 1315 unsigned long ui_volume_tmp = UINT_MAX;
@@ -1326,7 +1326,8 @@ Internal use only, use `play-sound' instead. */)
1326 current_sound_device = xzalloc (sizeof *current_sound_device); 1326 current_sound_device = xzalloc (sizeof *current_sound_device);
1327 current_sound = xzalloc (sizeof *current_sound); 1327 current_sound = xzalloc (sizeof *current_sound);
1328 record_unwind_protect_void (sound_cleanup); 1328 record_unwind_protect_void (sound_cleanup);
1329 current_sound->header = alloca (MAX_SOUND_HEADER_BYTES); 1329 char headerbuf[MAX_SOUND_HEADER_BYTES];
1330 current_sound->header = headerbuf;
1330 1331
1331 if (STRINGP (attrs[SOUND_FILE])) 1332 if (STRINGP (attrs[SOUND_FILE]))
1332 { 1333 {