Merge from trunk.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 6 Jun 2011 17:58:07 +0000 (10:58 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 6 Jun 2011 17:58:07 +0000 (10:58 -0700)
1  2 
src/ChangeLog
src/alloc.c
src/lisp.h

diff --combined src/ChangeLog
 +2011-06-06  Paul Eggert  <eggert@cs.ucla.edu>
 +
++      Integer overflow fixes.
++
 +      Check for overflow when converting integer to cons and back.
 +      * charset.c (Fdefine_charset_internal, Fdecode_char):
 +      Use cons_to_unsigned to catch overflow.
 +      (Fencode_char): Use INTEGER_TO_CONS.
 +      * composite.h (LGLYPH_CODE): Use cons_to_unsigned.
 +      (LGLYPH_SET_CODE): Use INTEGER_TO_CONS.
 +      * data.c (long_to_cons, cons_to_long): Remove.
 +      (cons_to_unsigned, cons_to_signed): New functions.
 +      These signal an error for invalid or out-of-range values.
 +      * dired.c (Ffile_attributes): Use INTEGER_TO_CONS.
 +      * fileio.c (Fset_visited_file_modtime): Use CONS_TO_INTEGER.
 +      * font.c (Ffont_variation_glyphs):
 +      * fontset.c (Finternal_char_font): Use INTEGER_TO_CONS.
 +      * lisp.h: Include <intprops.h>.
 +      (INTEGER_TO_CONS, CONS_TO_INTEGER): New macros.
 +      (cons_to_signed, cons_to_unsigned): New decls.
 +      (long_to_cons, cons_to_long): Remove decls.
 +      * undo.c (record_first_change): Use INTEGER_TO_CONS.
 +      (Fprimitive_undo): Use CONS_TO_INTEGER.
 +      * xfns.c (Fx_window_property): Likewise.
 +      * xselect.c: Include <limits.h>.
 +      (x_own_selection, selection_data_to_lisp_data):
 +      Use INTEGER_TO_CONS.
 +      (x_handle_selection_request, x_handle_selection_clear)
 +      (x_get_foreign_selection, Fx_disown_selection_internal)
 +      (Fx_get_atom_name, x_send_client_event): Use CONS_TO_INTEGER.
 +      (lisp_data_to_selection_data): Use cons_to_unsigned.
 +      (x_fill_property_data): Use cons_to_signed.
 +      Report values out of range.
 +
 +      Check for buffer and string overflow more precisely.
 +      * buffer.h (BUF_BYTES_MAX): New macro.
 +      * lisp.h (STRING_BYTES_MAX): New macro.
 +      * alloc.c (Fmake_string):
 +      * character.c (string_escape_byte8):
 +      * coding.c (coding_alloc_by_realloc):
 +      * doprnt.c (doprnt):
 +      * editfns.c (Fformat):
 +      * eval.c (verror):
 +      Use STRING_BYTES_MAX, not MOST_POSITIVE_FIXNUM,
 +      since they may not be the same number.
 +      * editfns.c (Finsert_char):
 +      * fileio.c (Finsert_file_contents):
 +      Likewise for BUF_BYTES_MAX.
 +
 +      * image.c: Use ptrdiff_t, not int, for sizes.
 +      (slurp_file): Switch from int to ptrdiff_t.
 +      All uses changed.
 +      (slurp_file): Check that file size fits in both size_t (for
 +      malloc) and ptrdiff_t (for sanity and safety).
 +
 +      * fileio.c (Fverify_visited_file_modtime): Avoid time overflow
 +      if b->modtime has its maximal value.
 +
 +      * dired.c (Ffile_attributes): Don't assume EMACS_INT has >32 bits.
 +
 +      Don't assume time_t can fit into int.
 +      * buffer.h (struct buffer.modtime): Now time_t, not int.
 +      * fileio.c (Fvisited_file_modtime): No need for time_t cast now.
 +      * undo.c (Fprimitive_undo): Use time_t, not int, for time_t value.
 +
 +      Minor fixes for signed vs unsigned integers.
 +      * character.h (MAYBE_UNIFY_CHAR):
 +      * charset.c (maybe_unify_char):
 +      * keyboard.c (read_char, reorder_modifiers):
 +      XINT -> XFASTINT, since the integer must be nonnegative.
 +      * ftfont.c (ftfont_spec_pattern):
 +      * keymap.c (access_keymap, silly_event_symbol_error):
 +      XUINT -> XFASTINT, since the integer must be nonnegative.
 +      (Fsingle_key_description, preferred_sequence_p): XUINT -> XINT,
 +      since it makes no difference and we prefer signed.
 +      * keyboard.c (record_char): Use XUINT when all the neighbors do.
 +      (access_keymap): NATNUMP -> INTEGERP, since the integer must be
 +      nonnegative.
 +
+ 2011-06-06  Stefan Monnier  <monnier@iro.umontreal.ca>
+       * window.h (Fwindow_frame): Declare.
+ 2011-06-06  Paul Eggert  <eggert@cs.ucla.edu>
+       * alloc.c: Simplify handling of large-request failures (Bug#8800).
+       (SPARE_MEMORY): Always define.
+       (LARGE_REQUEST): Remove.
+       (memory_full): Use SPARE_MEMORY rather than LARGE_REQUEST.
+ 2011-06-06  Martin Rudalics  <rudalics@gmx.at>
+       * lisp.h: Move EXFUNS for Fframe_root_window,
+       Fframe_first_window and Fset_frame_selected_window to window.h.
+       * window.h: Move EXFUNS for Fframe_root_window,
+       Fframe_first_window and Fset_frame_selected_window here from
+       lisp.h.
+       * frame.c (Fwindow_frame, Fframe_first_window)
+       (Fframe_root_window, Fframe_selected_window)
+       (Fset_frame_selected_window): Move to window.c.
+       (Factive_minibuffer_window): Move to minibuf.c.
+       (Fother_visible_frames_p): New function.
+       * minibuf.c (Factive_minibuffer_window): Move here from frame.c.
+       * window.c (decode_window, decode_any_window): Move up in code.
+       (Fwindowp, Fwindow_live_p): Rewrite doc-strings.
+       (inhibit_frame_unsplittable): Remove unused variable.
+       (Fwindow_buffer): Move up and rewrite doc-string.
+       (Fwindow_parent, Fwindow_vchild, Fwindow_hchild, Fwindow_next)
+       (Fwindow_prev): New functions.
+       (Fwindow_frame): Move here from frame.c.  Accept any window as
+       argument.
+       (Fframe_root_window, Fframe_first_window)
+       (Fframe_selected_window): Move here from frame.c.  Accept frame
+       or arbitrary window as argument.  Update doc-strings.
+       (Fminibuffer_window): Move up in code.
+       (Fwindow_minibuffer_p): Move up in code and simplify.
+       (Fset_frame_selected_window): Move here from frame.c.
+       Marginal rewrite.
+       (Fselected_window, select_window, Fselect_window): Move up in
+       code.  Minor doc-string fixes.
  2011-06-06  Paul Eggert  <eggert@cs.ucla.edu>
  
        * alloc.c (memory_full) [SYSTEM_MALLOC]: Port to MacOS (Bug#8800).
  
        * xselect.c (x_clipboard_manager_save): Remove redundant arg.
        (x_clipboard_manager_save): Add return value.
-       (x_clipboard_manager_error_1, x_clipboard_manager_error_2): New
-       error handlers.
+       (x_clipboard_manager_error_1, x_clipboard_manager_error_2):
+       New error handlers.
        (x_clipboard_manager_save_frame, x_clipboard_manager_save_all):
        Obey Vx_select_enable_clipboard_manager.  Catch errors in
        x_clipboard_manager_save (Bug#8779).
        (bidi_fetch_char, bidi_fetch_char_advance): New functions.
        (bidi_cache_search, bidi_cache_iterator_state)
        (bidi_paragraph_init, bidi_resolve_explicit, bidi_resolve_weak)
-       (bidi_level_of_next_char, bidi_move_to_visually_next): Support
-       character positions inside a run of characters covered by a
+       (bidi_level_of_next_char, bidi_move_to_visually_next):
+       Support character positions inside a run of characters covered by a
        display string.
        (bidi_paragraph_init, bidi_resolve_explicit_1)
        (bidi_level_of_next_char): Call bidi_fetch_char and
        definitions.
        (bidi_explicit_dir_char): Lookup character type in bidi_type_table,
        instead of using explicit *_CHAR codes.
-       (bidi_resolve_explicit, bidi_resolve_weak): Use
-       FETCH_MULTIBYTE_CHAR instead of FETCH_CHAR, as reordering of
+       (bidi_resolve_explicit, bidi_resolve_weak):
+       Use FETCH_MULTIBYTE_CHAR instead of FETCH_CHAR, as reordering of
        bidirectional text is supported only in multibyte buffers.
        (bidi_init_it): Accept additional argument FRAME_WINDOW_P and use
        it to initialize the frame_window_p member of struct bidi_it.
        (single_display_spec_intangible_p): Function deleted.
        (display_prop_intangible_p): Reimplement to call
        handle_display_spec instead of single_display_spec_intangible_p.
-       Accept 3 additional arguments needed by handle_display_spec.  This
-       fixes incorrect cursor motion across display property with complex
+       Accept 3 additional arguments needed by handle_display_spec.
+       This fixes incorrect cursor motion across display property with complex
        values: lists, `(when COND...)' forms, etc.
        (single_display_spec_string_p): Support property values that are
        lists with the argument STRING its top-level element.
        the display property will replace the characters it covers.
        (Fcurrent_bidi_paragraph_direction): Initialize the nchars and
        frame_window_p members of struct bidi_it.
-       (compute_display_string_pos, compute_display_string_end): New
-       functions.
+       (compute_display_string_pos, compute_display_string_end):
+       New functions.
        (push_it): Accept second argument POSITION, where pop_it should
        jump to continue iteration.
        (reseat_1): Initialize bidi_it.disp_pos.
        * dispextern.h (struct bidi_it): New member frame_window_p.
        (bidi_init_it): Update prototypes.
        (display_prop_intangible_p): Update prototype.
-       (compute_display_string_pos, compute_display_string_end): Declare
-       prototypes.
+       (compute_display_string_pos, compute_display_string_end):
+       Declare prototypes.
        (struct bidi_it): New members nchars and disp_pos.  ch_len is now
        EMACS_INT.
  
diff --combined src/alloc.c
@@@ -190,17 -190,10 +190,10 @@@ static int total_free_floats, total_flo
  
  static char *spare_memory[7];
  
- #ifndef SYSTEM_MALLOC
/* Amount of spare memory to keep in large reserve block.  */
+ /* Amount of spare memory to keep in large reserve block, or to see
   whether this much is available when malloc fails on a larger request.  */
  
  #define SPARE_MEMORY (1 << 14)
- #endif
- #ifdef SYSTEM_MALLOC
- # define LARGE_REQUEST (1 << 14)
- #else
- # define LARGE_REQUEST SPARE_MEMORY
- #endif
  
  /* Number of extra blocks malloc should get when it needs more core.  */
  
@@@ -2211,7 -2204,7 +2204,7 @@@ INIT must be an integer that represent
        int len = CHAR_STRING (c, str);
        EMACS_INT string_len = XINT (length);
  
 -      if (string_len > MOST_POSITIVE_FIXNUM / len)
 +      if (string_len > STRING_BYTES_MAX / len)
        string_overflow ();
        nbytes = len * string_len;
        val = make_uninit_multibyte_string (string_len, nbytes);
@@@ -3289,9 -3282,9 +3282,9 @@@ memory_full (size_t nbytes
  {
    /* Do not go into hysterics merely because a large request failed.  */
    int enough_free_memory = 0;
-   if (LARGE_REQUEST < nbytes)
+   if (SPARE_MEMORY < nbytes)
      {
-       void *p = malloc (LARGE_REQUEST);
+       void *p = malloc (SPARE_MEMORY);
        if (p)
        {
          free (p);
diff --combined src/lisp.h
@@@ -24,8 -24,6 +24,8 @@@ along with GNU Emacs.  If not, see <htt
  #include <stddef.h>
  #include <inttypes.h>
  
 +#include <intprops.h>
 +
  /* Use the configure flag --enable-checking[=LIST] to enable various
     types of run time checks for Lisp objects.  */
  
@@@ -765,12 -763,6 +765,12 @@@ extern EMACS_INT string_bytes (struct L
  
  #endif /* not GC_CHECK_STRING_BYTES */
  
 +/* A string cannot contain more bytes than a fixnum can represent,
 +   nor can it be so long that C pointer arithmetic stops working on
 +   the string plus a terminating null.  */
 +#define STRING_BYTES_MAX  \
 +  min (MOST_POSITIVE_FIXNUM, min (SIZE_MAX, PTRDIFF_MAX) - 1)
 +
  /* Mark STR as a unibyte string.  */
  #define STRING_SET_UNIBYTE(STR)  \
    do { if (EQ (STR, empty_multibyte_string))  \
@@@ -2410,35 -2402,9 +2410,35 @@@ EXFUN (Fadd1, 1)
  EXFUN (Fsub1, 1);
  EXFUN (Fmake_variable_buffer_local, 1);
  
 +/* Convert the integer I to an Emacs representation, either the integer
 +   itself, or a cons of two or three integers, or if all else fails a float.
 +   I should not have side effects.  */
 +#define INTEGER_TO_CONS(i)                                        \
 +  (! FIXNUM_OVERFLOW_P (i)                                        \
 +   ? make_number (i)                                              \
 +   : ! ((FIXNUM_OVERFLOW_P (INTMAX_MIN >> 16)                     \
 +       || FIXNUM_OVERFLOW_P (UINTMAX_MAX >> 16))                  \
 +      && FIXNUM_OVERFLOW_P ((i) >> 16))                           \
 +   ? Fcons (make_number ((i) >> 16), make_number ((i) & 0xffff))    \
 +   : ! ((FIXNUM_OVERFLOW_P (INTMAX_MIN >> 16 >> 24)               \
 +       || FIXNUM_OVERFLOW_P (UINTMAX_MAX >> 16 >> 24))            \
 +      && FIXNUM_OVERFLOW_P ((i) >> 16 >> 24))                     \
 +   ? Fcons (make_number ((i) >> 16 >> 24),                        \
 +          Fcons (make_number ((i) >> 16 & 0xffffff),              \
 +                 make_number ((i) & 0xffff)))                     \
 +   : make_float (i))
 +
 +/* Convert the Emacs representation CONS back to an integer of type
 +   TYPE, storing the result the variable VAR.  Signal an error if CONS
 +   is not a valid representation or is out of range for TYPE.  */
 +#define CONS_TO_INTEGER(cons, type, var)                              \
 + (TYPE_SIGNED (type)                                                  \
 +  ? ((var) = cons_to_signed (cons, TYPE_MINIMUM (type), TYPE_MAXIMUM (type))) \
 +  : ((var) = cons_to_unsigned (cons, TYPE_MAXIMUM (type))))
 +extern intmax_t cons_to_signed (Lisp_Object, intmax_t, intmax_t);
 +extern uintmax_t cons_to_unsigned (Lisp_Object, uintmax_t);
 +
  extern struct Lisp_Symbol *indirect_variable (struct Lisp_Symbol *);
 -extern Lisp_Object long_to_cons (unsigned long);
 -extern unsigned long cons_to_long (Lisp_Object);
  extern void args_out_of_range (Lisp_Object, Lisp_Object) NO_RETURN;
  extern void args_out_of_range_3 (Lisp_Object, Lisp_Object,
                                   Lisp_Object) NO_RETURN;
@@@ -3215,16 -3181,12 +3215,12 @@@ extern Lisp_Object get_frame_param (str
  extern Lisp_Object frame_buffer_predicate (Lisp_Object);
  EXFUN (Fselect_frame, 2);
  EXFUN (Fselected_frame, 0);
- EXFUN (Fwindow_frame, 1);
- EXFUN (Fframe_root_window, 1);
- EXFUN (Fframe_first_window, 1);
  EXFUN (Fmake_frame_visible, 1);
  EXFUN (Ficonify_frame, 1);
  EXFUN (Fframe_parameter, 2);
  EXFUN (Fmodify_frame_parameters, 2);
  EXFUN (Fraise_frame, 1);
  EXFUN (Fredirect_frame_focus, 2);
- EXFUN (Fset_frame_selected_window, 3);
  extern Lisp_Object frame_buffer_list (Lisp_Object);
  extern void frames_discard_buffer (Lisp_Object);
  extern void set_frame_buffer_list (Lisp_Object, Lisp_Object);