aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2019-02-27 01:14:27 -0800
committerPaul Eggert2019-02-27 01:23:30 -0800
commite828765d01313acddcf17279b6b43ae9f777f2a4 (patch)
tree8dd465c5899c62ad5613fe97172fad1ddf419097 /src
parent2f7885a4b3609dec19e4595c6c24f3a21f33c5d6 (diff)
downloademacs-e828765d01313acddcf17279b6b43ae9f777f2a4.tar.gz
emacs-e828765d01313acddcf17279b6b43ae9f777f2a4.zip
DEFVAR_INT variables are now intmax_t
Formerly they were fixnums, which led to problems when dealing with values that might not fit on 32-bit platforms, such as string-chars-consed or floats_consed. 64-bit counters should be good enough for these (for a while, anyway...). While we’re at it, fix some unlikely integer overflow bugs that have been in the code for a while. * lib-src/make-docfile.c (write_globals): * src/data.c (do_symval_forwarding, store_symval_forwarding): * src/eval.c (restore_stack_limits, call_debugger): * src/frame.h (struct frame.cost_calculation_baud_rate): * src/keyboard.c (last_auto_save, bind_polling_period, read_char): * src/lisp.h (struct Lisp_Intfwd.intvar): * src/lread.c (defvar_int): * src/pdumper.c (dump_fwd_int): * src/thread.h (struct thread_state.m_lisp_eval_depth): * src/undo.c (truncate_undo_list): * src/xselect.c (wait_for_property_change) (x_get_foreign_selection): * src/xterm.c (x_emacs_to_x_modifiers): DEFVAR_INT variables now have the C type intmax_t, not EMACS_INT. * src/data.c (store_symval_forwarding): * src/gnutls.c (Fgnutls_boot): * src/keyboard.c (bind_polling_period): * src/macros.c (pop_kbd_macro, Fexecute_kbd_macro): * src/undo.c (truncate_undo_list): Allow any integer that fits into intmax_t, instead of requiring it to be a Lisp fixnum. * src/dispnew.c (update_window): * src/frame.c (x_figure_window_size): * src/gnutls.c (init_gnutls_functions) (emacs_gnutls_handle_error): * src/keyboard.c (make_lisp_event): * src/nsterm.m (ns_dumpglyphs_image): * src/profiler.c (make_log): * src/scroll.c (calculate_scrolling) (calculate_direct_scrolling): * src/termcap.c (tputs): * src/xterm.c (x_draw_image_relief): Avoid implementation-defined behavior on conversion of out-of-range integers. * src/eval.c (when_entered_debugger): Now intmax_t. (max_ensure_room): New function, that avoids signed integer overflow. (call_debugger, signal_or_quit): Use it. * src/fileio.c (Fdo_auto_save): * src/keyboard.c (make_lisp_event): * src/term.c (calculate_costs): * src/xdisp.c (build_desired_tool_bar_string) (hscroll_window_tree, try_scrolling, decode_mode_spec) (x_produce_glyphs): Avoid signed integer overflow. * src/lisp.h (clip_to_bounds): Generalize to intmax_t. * src/pdumper.c (dump_emacs_reloc_immediate_emacs_int): Remove, ... (dump_emacs_reloc_immediate_intmax_t): ... replacing with this function. All uses changed. * src/profiler.c (make_log): Omit args. All callers changed. * src/termcap.c: Include stdlib.h, for atoi. Include intprops.h. * src/window.c (sanitize_next_screen_context_lines): New function. (window_scroll_pixel_based, window_scroll_line_based): Use it to avoid signed integer overflow.
Diffstat (limited to 'src')
-rw-r--r--src/data.c11
-rw-r--r--src/dispnew.c7
-rw-r--r--src/eval.c56
-rw-r--r--src/fileio.c5
-rw-r--r--src/frame.c6
-rw-r--r--src/frame.h2
-rw-r--r--src/gnutls.c15
-rw-r--r--src/keyboard.c81
-rw-r--r--src/lisp.h8
-rw-r--r--src/lread.c4
-rw-r--r--src/macros.c4
-rw-r--r--src/nsterm.m5
-rw-r--r--src/pdumper.c6
-rw-r--r--src/profiler.c20
-rw-r--r--src/scroll.c12
-rw-r--r--src/term.c4
-rw-r--r--src/termcap.c35
-rw-r--r--src/thread.h2
-rw-r--r--src/undo.c11
-rw-r--r--src/window.c23
-rw-r--r--src/xdisp.c37
-rw-r--r--src/xselect.c16
-rw-r--r--src/xterm.c6
23 files changed, 188 insertions, 188 deletions
diff --git a/src/data.c b/src/data.c
index 65f2b0f2f1b..15b6106cfe8 100644
--- a/src/data.c
+++ b/src/data.c
@@ -985,7 +985,7 @@ do_symval_forwarding (union Lisp_Fwd *valcontents)
985 switch (XFWDTYPE (valcontents)) 985 switch (XFWDTYPE (valcontents))
986 { 986 {
987 case Lisp_Fwd_Int: 987 case Lisp_Fwd_Int:
988 return make_fixnum (*XFIXNUMFWD (valcontents)->intvar); 988 return make_int (*XFIXNUMFWD (valcontents)->intvar);
989 989
990 case Lisp_Fwd_Bool: 990 case Lisp_Fwd_Bool:
991 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil); 991 return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
@@ -1076,8 +1076,13 @@ store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newva
1076 switch (XFWDTYPE (valcontents)) 1076 switch (XFWDTYPE (valcontents))
1077 { 1077 {
1078 case Lisp_Fwd_Int: 1078 case Lisp_Fwd_Int:
1079 CHECK_FIXNUM (newval); 1079 {
1080 *XFIXNUMFWD (valcontents)->intvar = XFIXNUM (newval); 1080 intmax_t i;
1081 CHECK_INTEGER (newval);
1082 if (! integer_to_intmax (newval, &i))
1083 xsignal1 (Qoverflow_error, newval);
1084 *XFIXNUMFWD (valcontents)->intvar = i;
1085 }
1081 break; 1086 break;
1082 1087
1083 case Lisp_Fwd_Bool: 1088 case Lisp_Fwd_Bool:
diff --git a/src/dispnew.c b/src/dispnew.c
index 300f1c2d70a..9220213b907 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -3389,7 +3389,7 @@ update_window (struct window *w, bool force_p)
3389{ 3389{
3390 struct glyph_matrix *desired_matrix = w->desired_matrix; 3390 struct glyph_matrix *desired_matrix = w->desired_matrix;
3391 bool paused_p; 3391 bool paused_p;
3392 int preempt_count = baud_rate / 2400 + 1; 3392 int preempt_count = clip_to_bounds (1, baud_rate / 2400 + 1, INT_MAX);
3393 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w))); 3393 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3394#ifdef GLYPH_DEBUG 3394#ifdef GLYPH_DEBUG
3395 /* Check that W's frame doesn't have glyph matrices. */ 3395 /* Check that W's frame doesn't have glyph matrices. */
@@ -4485,16 +4485,13 @@ update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p,
4485 struct glyph_matrix *desired_matrix = f->desired_matrix; 4485 struct glyph_matrix *desired_matrix = f->desired_matrix;
4486 int i; 4486 int i;
4487 bool pause_p; 4487 bool pause_p;
4488 int preempt_count = baud_rate / 2400 + 1; 4488 int preempt_count = clip_to_bounds (1, baud_rate / 2400 + 1, INT_MAX);
4489 4489
4490 eassert (current_matrix && desired_matrix); 4490 eassert (current_matrix && desired_matrix);
4491 4491
4492 if (baud_rate != FRAME_COST_BAUD_RATE (f)) 4492 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4493 calculate_costs (f); 4493 calculate_costs (f);
4494 4494
4495 if (preempt_count <= 0)
4496 preempt_count = 1;
4497
4498 if (!force_p && detect_input_pending_ignore_squeezables ()) 4495 if (!force_p && detect_input_pending_ignore_squeezables ())
4499 { 4496 {
4500 pause_p = 1; 4497 pause_p = 1;
diff --git a/src/eval.c b/src/eval.c
index b6cdfc911d0..bf16a709b15 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -56,26 +56,6 @@ Lisp_Object Vautoload_queue;
56 is shutting down. */ 56 is shutting down. */
57Lisp_Object Vrun_hooks; 57Lisp_Object Vrun_hooks;
58 58
59/* The commented-out variables below are macros defined in thread.h. */
60
61/* Current number of specbindings allocated in specpdl, not counting
62 the dummy entry specpdl[-1]. */
63
64/* ptrdiff_t specpdl_size; */
65
66/* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
67 only so that its address can be taken. */
68
69/* union specbinding *specpdl; */
70
71/* Pointer to first unused element in specpdl. */
72
73/* union specbinding *specpdl_ptr; */
74
75/* Depth in Lisp evaluations and function calls. */
76
77/* static EMACS_INT lisp_eval_depth; */
78
79/* The value of num_nonmacro_input_events as of the last time we 59/* The value of num_nonmacro_input_events as of the last time we
80 started to enter the debugger. If we decide to enter the debugger 60 started to enter the debugger. If we decide to enter the debugger
81 again when this is still equal to num_nonmacro_input_events, then we 61 again when this is still equal to num_nonmacro_input_events, then we
@@ -83,7 +63,7 @@ Lisp_Object Vrun_hooks;
83 signal the error instead of entering an infinite loop of debugger 63 signal the error instead of entering an infinite loop of debugger
84 invocations. */ 64 invocations. */
85 65
86static EMACS_INT when_entered_debugger; 66static intmax_t when_entered_debugger;
87 67
88/* The function from which the last `signal' was called. Set in 68/* The function from which the last `signal' was called. Set in
89 Fsignal. */ 69 Fsignal. */
@@ -285,13 +265,23 @@ init_eval (void)
285 when_entered_debugger = -1; 265 when_entered_debugger = -1;
286} 266}
287 267
268/* Ensure that *M is at least A + B if possible, or is its maximum
269 value otherwise. */
270
271static void
272max_ensure_room (intmax_t *m, intmax_t a, intmax_t b)
273{
274 intmax_t sum = INT_ADD_WRAPV (a, b, &sum) ? INTMAX_MAX : sum;
275 *m = max (*m, sum);
276}
277
288/* Unwind-protect function used by call_debugger. */ 278/* Unwind-protect function used by call_debugger. */
289 279
290static void 280static void
291restore_stack_limits (Lisp_Object data) 281restore_stack_limits (Lisp_Object data)
292{ 282{
293 max_specpdl_size = XFIXNUM (XCAR (data)); 283 integer_to_intmax (XCAR (data), &max_specpdl_size);
294 max_lisp_eval_depth = XFIXNUM (XCDR (data)); 284 integer_to_intmax (XCDR (data), &max_lisp_eval_depth);
295} 285}
296 286
297static void grow_specpdl (void); 287static void grow_specpdl (void);
@@ -304,21 +294,19 @@ call_debugger (Lisp_Object arg)
304 bool debug_while_redisplaying; 294 bool debug_while_redisplaying;
305 ptrdiff_t count = SPECPDL_INDEX (); 295 ptrdiff_t count = SPECPDL_INDEX ();
306 Lisp_Object val; 296 Lisp_Object val;
307 EMACS_INT old_depth = max_lisp_eval_depth; 297 intmax_t old_depth = max_lisp_eval_depth;
308 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */ 298 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
309 EMACS_INT old_max = max (max_specpdl_size, count); 299 intmax_t old_max = max (max_specpdl_size, count);
310 300
311 /* The previous value of 40 is too small now that the debugger 301 /* The previous value of 40 is too small now that the debugger
312 prints using cl-prin1 instead of prin1. Printing lists nested 8 302 prints using cl-prin1 instead of prin1. Printing lists nested 8
313 deep (which is the value of print-level used in the debugger) 303 deep (which is the value of print-level used in the debugger)
314 currently requires 77 additional frames. See bug#31919. */ 304 currently requires 77 additional frames. See bug#31919. */
315 if (lisp_eval_depth + 100 > max_lisp_eval_depth) 305 max_ensure_room (&max_lisp_eval_depth, lisp_eval_depth, 100);
316 max_lisp_eval_depth = lisp_eval_depth + 100;
317 306
318 /* While debugging Bug#16603, previous value of 100 was found 307 /* While debugging Bug#16603, previous value of 100 was found
319 too small to avoid specpdl overflow in the debugger itself. */ 308 too small to avoid specpdl overflow in the debugger itself. */
320 if (max_specpdl_size - 200 < count) 309 max_ensure_room (&max_specpdl_size, count, 200);
321 max_specpdl_size = count + 200;
322 310
323 if (old_max == count) 311 if (old_max == count)
324 { 312 {
@@ -329,8 +317,7 @@ call_debugger (Lisp_Object arg)
329 317
330 /* Restore limits after leaving the debugger. */ 318 /* Restore limits after leaving the debugger. */
331 record_unwind_protect (restore_stack_limits, 319 record_unwind_protect (restore_stack_limits,
332 Fcons (make_fixnum (old_max), 320 Fcons (make_int (old_max), make_int (old_depth)));
333 make_fixnum (old_depth)));
334 321
335#ifdef HAVE_WINDOW_SYSTEM 322#ifdef HAVE_WINDOW_SYSTEM
336 if (display_hourglass_p) 323 if (display_hourglass_p)
@@ -1654,11 +1641,8 @@ signal_or_quit (Lisp_Object error_symbol, Lisp_Object data, bool keyboard_quit)
1654 && specpdl_ptr < specpdl + specpdl_size) 1641 && specpdl_ptr < specpdl + specpdl_size)
1655 { 1642 {
1656 /* Edebug takes care of restoring these variables when it exits. */ 1643 /* Edebug takes care of restoring these variables when it exits. */
1657 if (lisp_eval_depth + 20 > max_lisp_eval_depth) 1644 max_ensure_room (&max_lisp_eval_depth, lisp_eval_depth, 20);
1658 max_lisp_eval_depth = lisp_eval_depth + 20; 1645 max_ensure_room (&max_specpdl_size, SPECPDL_INDEX (), 40);
1659
1660 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1661 max_specpdl_size = SPECPDL_INDEX () + 40;
1662 1646
1663 call2 (Vsignal_hook_function, error_symbol, data); 1647 call2 (Vsignal_hook_function, error_symbol, data);
1664 } 1648 }
diff --git a/src/fileio.c b/src/fileio.c
index 55c9f26b753..cac8ed0aeef 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -5706,8 +5706,9 @@ A non-nil CURRENT-ONLY argument means save only current buffer. */)
5706 bool old_message_p = 0; 5706 bool old_message_p = 0;
5707 struct auto_save_unwind auto_save_unwind; 5707 struct auto_save_unwind auto_save_unwind;
5708 5708
5709 if (max_specpdl_size < specpdl_size + 40) 5709 intmax_t sum = INT_ADD_WRAPV (specpdl_size, 40, &sum) ? INTMAX_MAX : sum;
5710 max_specpdl_size = specpdl_size + 40; 5710 if (max_specpdl_size < sum)
5711 max_specpdl_size = sum;
5711 5712
5712 if (minibuf_level) 5713 if (minibuf_level)
5713 no_message = Qt; 5714 no_message = Qt;
diff --git a/src/frame.c b/src/frame.c
index aa1a15ff006..d1d6993e94b 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -5309,9 +5309,9 @@ x_figure_window_size (struct frame *f, Lisp_Object parms, bool toolbar_p, int *x
5309 { 5309 {
5310 int margin, relief; 5310 int margin, relief;
5311 5311
5312 relief = (tool_bar_button_relief >= 0 5312 relief = (tool_bar_button_relief < 0
5313 ? tool_bar_button_relief 5313 ? DEFAULT_TOOL_BAR_BUTTON_RELIEF
5314 : DEFAULT_TOOL_BAR_BUTTON_RELIEF); 5314 : min (tool_bar_button_relief, 1000000));
5315 5315
5316 if (RANGED_FIXNUMP (1, Vtool_bar_button_margin, INT_MAX)) 5316 if (RANGED_FIXNUMP (1, Vtool_bar_button_margin, INT_MAX))
5317 margin = XFIXNAT (Vtool_bar_button_margin); 5317 margin = XFIXNAT (Vtool_bar_button_margin);
diff --git a/src/frame.h b/src/frame.h
index b7cbdd95571..544e0bef17a 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -580,7 +580,7 @@ struct frame
580 int config_scroll_bar_lines; 580 int config_scroll_bar_lines;
581 581
582 /* The baud rate that was used to calculate costs for this frame. */ 582 /* The baud rate that was used to calculate costs for this frame. */
583 int cost_calculation_baud_rate; 583 intmax_t cost_calculation_baud_rate;
584 584
585 /* Frame opacity 585 /* Frame opacity
586 alpha[0]: alpha transparency of the active frame 586 alpha[0]: alpha transparency of the active frame
diff --git a/src/gnutls.c b/src/gnutls.c
index 63dbcf4162b..2951c8d074c 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -395,8 +395,7 @@ init_gnutls_functions (void)
395# endif 395# endif
396# endif /* HAVE_GNUTLS3 */ 396# endif /* HAVE_GNUTLS3 */
397 397
398 max_log_level = global_gnutls_log_level; 398 max_log_level = clip_to_bounds (INT_MIN, global_gnutls_log_level, INT_MAX);
399
400 { 399 {
401 Lisp_Object name = CAR_SAFE (Fget (Qgnutls, QCloaded_from)); 400 Lisp_Object name = CAR_SAFE (Fget (Qgnutls, QCloaded_from));
402 GNUTLS_LOG2 (1, max_log_level, "GnuTLS library loaded:", 401 GNUTLS_LOG2 (1, max_log_level, "GnuTLS library loaded:",
@@ -760,7 +759,8 @@ emacs_gnutls_handle_error (gnutls_session_t session, int err)
760 759
761 check_memory_full (err); 760 check_memory_full (err);
762 761
763 int max_log_level = global_gnutls_log_level; 762 int max_log_level
763 = clip_to_bounds (INT_MIN, global_gnutls_log_level, INT_MAX);
764 764
765 /* TODO: use gnutls-error-fatalp and gnutls-error-string. */ 765 /* TODO: use gnutls-error-fatalp and gnutls-error-string. */
766 766
@@ -1691,14 +1691,17 @@ one trustfile (usually a CA bundle). */)
1691 1691
1692 state = XPROCESS (proc)->gnutls_state; 1692 state = XPROCESS (proc)->gnutls_state;
1693 1693
1694 if (TYPE_RANGED_FIXNUMP (int, loglevel)) 1694 if (INTEGERP (loglevel))
1695 { 1695 {
1696 gnutls_global_set_log_function (gnutls_log_function); 1696 gnutls_global_set_log_function (gnutls_log_function);
1697# ifdef HAVE_GNUTLS3 1697# ifdef HAVE_GNUTLS3
1698 gnutls_global_set_audit_log_function (gnutls_audit_log_function); 1698 gnutls_global_set_audit_log_function (gnutls_audit_log_function);
1699# endif 1699# endif
1700 gnutls_global_set_log_level (XFIXNUM (loglevel)); 1700 int level = (FIXNUMP (loglevel)
1701 max_log_level = XFIXNUM (loglevel); 1701 ? clip_to_bounds (INT_MIN, XFIXNUM (loglevel), INT_MAX)
1702 : NILP (Fnatnump (loglevel)) ? INT_MIN : INT_MAX);
1703 gnutls_global_set_log_level (level);
1704 max_log_level = level;
1702 XPROCESS (proc)->gnutls_log_level = max_log_level; 1705 XPROCESS (proc)->gnutls_log_level = max_log_level;
1703 } 1706 }
1704 1707
diff --git a/src/keyboard.c b/src/keyboard.c
index 1d67c3eaabd..3af487cf070 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -208,7 +208,7 @@ struct buffer *buffer_before_last_command_or_undo;
208 208
209/* Value of num_nonmacro_input_events as of last auto save. */ 209/* Value of num_nonmacro_input_events as of last auto save. */
210 210
211static EMACS_INT last_auto_save; 211static intmax_t last_auto_save;
212 212
213/* The value of point when the last command was started. */ 213/* The value of point when the last command was started. */
214static ptrdiff_t last_point_position; 214static ptrdiff_t last_point_position;
@@ -1213,7 +1213,7 @@ some_mouse_moved (void)
1213 1213
1214 if (ignore_mouse_drag_p) 1214 if (ignore_mouse_drag_p)
1215 { 1215 {
1216 /* ignore_mouse_drag_p = 0; */ 1216 /* ignore_mouse_drag_p = false; */
1217 return 0; 1217 return 0;
1218 } 1218 }
1219 1219
@@ -1301,7 +1301,7 @@ command_loop_1 (void)
1301 loop. (This flag is set in xdisp.c whenever the tool bar is 1301 loop. (This flag is set in xdisp.c whenever the tool bar is
1302 resized, because the resize moves text up or down, and would 1302 resized, because the resize moves text up or down, and would
1303 generate false mouse drag events if we don't ignore them.) */ 1303 generate false mouse drag events if we don't ignore them.) */
1304 ignore_mouse_drag_p = 0; 1304 ignore_mouse_drag_p = false;
1305 1305
1306 /* If minibuffer on and echo area in use, 1306 /* If minibuffer on and echo area in use,
1307 wait a short time and redraw minibuffer. */ 1307 wait a short time and redraw minibuffer. */
@@ -1966,14 +1966,14 @@ void
1966bind_polling_period (int n) 1966bind_polling_period (int n)
1967{ 1967{
1968#ifdef POLL_FOR_INPUT 1968#ifdef POLL_FOR_INPUT
1969 EMACS_INT new = polling_period; 1969 intmax_t new = polling_period;
1970 1970
1971 if (n > new) 1971 if (n > new)
1972 new = n; 1972 new = n;
1973 1973
1974 stop_other_atimers (poll_timer); 1974 stop_other_atimers (poll_timer);
1975 stop_polling (); 1975 stop_polling ();
1976 specbind (Qpolling_period, make_fixnum (new)); 1976 specbind (Qpolling_period, make_int (new));
1977 /* Start a new alarm with the new period. */ 1977 /* Start a new alarm with the new period. */
1978 start_polling (); 1978 start_polling ();
1979#endif 1979#endif
@@ -2422,7 +2422,7 @@ read_char (int commandflag, Lisp_Object map,
2422 goto exit; 2422 goto exit;
2423 } 2423 }
2424 2424
2425 c = Faref (Vexecuting_kbd_macro, make_fixnum (executing_kbd_macro_index)); 2425 c = Faref (Vexecuting_kbd_macro, make_int (executing_kbd_macro_index));
2426 if (STRINGP (Vexecuting_kbd_macro) 2426 if (STRINGP (Vexecuting_kbd_macro)
2427 && (XFIXNAT (c) & 0x80) && (XFIXNAT (c) <= 0xff)) 2427 && (XFIXNAT (c) & 0x80) && (XFIXNAT (c) <= 0xff))
2428 XSETFASTINT (c, CHAR_META | (XFIXNAT (c) & ~0x80)); 2428 XSETFASTINT (c, CHAR_META | (XFIXNAT (c) & ~0x80));
@@ -5585,7 +5585,7 @@ make_lispy_event (struct input_event *event)
5585 double-click-fuzz as is. On other frames, interpret it 5585 double-click-fuzz as is. On other frames, interpret it
5586 as a multiple of 1/8 characters. */ 5586 as a multiple of 1/8 characters. */
5587 struct frame *f; 5587 struct frame *f;
5588 int fuzz; 5588 intmax_t fuzz;
5589 5589
5590 if (WINDOWP (event->frame_or_window)) 5590 if (WINDOWP (event->frame_or_window))
5591 f = XFRAME (XWINDOW (event->frame_or_window)->frame); 5591 f = XFRAME (XWINDOW (event->frame_or_window)->frame);
@@ -5628,7 +5628,7 @@ make_lispy_event (struct input_event *event)
5628 double_click_count = 1; 5628 double_click_count = 1;
5629 button_down_time = event->timestamp; 5629 button_down_time = event->timestamp;
5630 *start_pos_ptr = Fcopy_alist (position); 5630 *start_pos_ptr = Fcopy_alist (position);
5631 ignore_mouse_drag_p = 0; 5631 ignore_mouse_drag_p = false;
5632 } 5632 }
5633 5633
5634 /* Now we're releasing a button - check the co-ordinates to 5634 /* Now we're releasing a button - check the co-ordinates to
@@ -5644,11 +5644,14 @@ make_lispy_event (struct input_event *event)
5644 if (!CONSP (start_pos)) 5644 if (!CONSP (start_pos))
5645 return Qnil; 5645 return Qnil;
5646 5646
5647 event->modifiers &= ~up_modifier; 5647 unsigned click_or_drag_modifier = click_modifier;
5648 5648
5649 if (ignore_mouse_drag_p)
5650 ignore_mouse_drag_p = false;
5651 else
5649 { 5652 {
5650 Lisp_Object new_down, down; 5653 Lisp_Object new_down, down;
5651 EMACS_INT xdiff = double_click_fuzz, ydiff = double_click_fuzz; 5654 intmax_t xdiff = double_click_fuzz, ydiff = double_click_fuzz;
5652 5655
5653 /* The third element of every position 5656 /* The third element of every position
5654 should be the (x,y) pair. */ 5657 should be the (x,y) pair. */
@@ -5662,39 +5665,37 @@ make_lispy_event (struct input_event *event)
5662 ydiff = XFIXNUM (XCDR (new_down)) - XFIXNUM (XCDR (down)); 5665 ydiff = XFIXNUM (XCDR (new_down)) - XFIXNUM (XCDR (down));
5663 } 5666 }
5664 5667
5665 if (ignore_mouse_drag_p) 5668 if (! (0 < double_click_fuzz
5666 { 5669 && - double_click_fuzz < xdiff
5667 event->modifiers |= click_modifier; 5670 && xdiff < double_click_fuzz
5668 ignore_mouse_drag_p = 0; 5671 && - double_click_fuzz < ydiff
5669 } 5672 && ydiff < double_click_fuzz
5670 else if (xdiff < double_click_fuzz && xdiff > - double_click_fuzz 5673 /* Maybe the mouse has moved a lot, caused scrolling, and
5671 && ydiff < double_click_fuzz && ydiff > - double_click_fuzz 5674 eventually ended up at the same screen position (but
5672 /* Maybe the mouse has moved a lot, caused scrolling, and 5675 not buffer position) in which case it is a drag, not
5673 eventually ended up at the same screen position (but 5676 a click. */
5674 not buffer position) in which case it is a drag, not 5677 /* FIXME: OTOH if the buffer position has changed
5675 a click. */ 5678 because of a timer or process filter rather than
5676 /* FIXME: OTOH if the buffer position has changed 5679 because of mouse movement, it should be considered as
5677 because of a timer or process filter rather than 5680 a click. But mouse-drag-region completely ignores
5678 because of mouse movement, it should be considered as 5681 this case and it hasn't caused any real problem, so
5679 a click. But mouse-drag-region completely ignores 5682 it's probably OK to ignore it as well. */
5680 this case and it hasn't caused any real problem, so 5683 && EQ (Fcar (Fcdr (start_pos)), Fcar (Fcdr (position)))))
5681 it's probably OK to ignore it as well. */
5682 && EQ (Fcar (Fcdr (start_pos)), Fcar (Fcdr (position))))
5683 /* Mouse hasn't moved (much). */
5684 event->modifiers |= click_modifier;
5685 else
5686 { 5684 {
5685 /* Mouse has moved enough. */
5687 button_down_time = 0; 5686 button_down_time = 0;
5688 event->modifiers |= drag_modifier; 5687 click_or_drag_modifier = drag_modifier;
5689 } 5688 }
5690
5691 /* Don't check is_double; treat this as multiple
5692 if the down-event was multiple. */
5693 if (double_click_count > 1)
5694 event->modifiers |= ((double_click_count > 2)
5695 ? triple_modifier
5696 : double_modifier);
5697 } 5689 }
5690
5691 /* Don't check is_double; treat this as multiple if the
5692 down-event was multiple. */
5693 event->modifiers
5694 = ((event->modifiers & ~up_modifier)
5695 | click_or_drag_modifier
5696 | (double_click_count < 2 ? 0
5697 : double_click_count == 2 ? double_modifier
5698 : triple_modifier));
5698 } 5699 }
5699 else 5700 else
5700 /* Every mouse event should either have the down_modifier or 5701 /* Every mouse event should either have the down_modifier or
@@ -5743,7 +5744,7 @@ make_lispy_event (struct input_event *event)
5743 double-click-fuzz as is. On other frames, interpret it 5744 double-click-fuzz as is. On other frames, interpret it
5744 as a multiple of 1/8 characters. */ 5745 as a multiple of 1/8 characters. */
5745 struct frame *fr; 5746 struct frame *fr;
5746 int fuzz; 5747 intmax_t fuzz;
5747 int symbol_num; 5748 int symbol_num;
5748 bool is_double; 5749 bool is_double;
5749 5750
diff --git a/src/lisp.h b/src/lisp.h
index 3b481089cfd..388cd04163a 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -1260,8 +1260,8 @@ INLINE bool
1260#define FIXNUM_OVERFLOW_P(i) \ 1260#define FIXNUM_OVERFLOW_P(i) \
1261 (! ((0 <= (i) || MOST_NEGATIVE_FIXNUM <= (i)) && (i) <= MOST_POSITIVE_FIXNUM)) 1261 (! ((0 <= (i) || MOST_NEGATIVE_FIXNUM <= (i)) && (i) <= MOST_POSITIVE_FIXNUM))
1262 1262
1263INLINE ptrdiff_t 1263INLINE intmax_t
1264clip_to_bounds (ptrdiff_t lower, EMACS_INT num, ptrdiff_t upper) 1264clip_to_bounds (intmax_t lower, intmax_t num, intmax_t upper)
1265{ 1265{
1266 return num < lower ? lower : num <= upper ? num : upper; 1266 return num < lower ? lower : num <= upper ? num : upper;
1267} 1267}
@@ -2664,7 +2664,7 @@ make_uint (uintmax_t n)
2664struct Lisp_Intfwd 2664struct Lisp_Intfwd
2665 { 2665 {
2666 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Int */ 2666 enum Lisp_Fwd_Type type; /* = Lisp_Fwd_Int */
2667 EMACS_INT *intvar; 2667 intmax_t *intvar;
2668 }; 2668 };
2669 2669
2670/* Boolean forwarding pointer to an int variable. 2670/* Boolean forwarding pointer to an int variable.
@@ -3099,7 +3099,7 @@ enum maxargs
3099extern void defvar_lisp (struct Lisp_Objfwd *, const char *, Lisp_Object *); 3099extern void defvar_lisp (struct Lisp_Objfwd *, const char *, Lisp_Object *);
3100extern void defvar_lisp_nopro (struct Lisp_Objfwd *, const char *, Lisp_Object *); 3100extern void defvar_lisp_nopro (struct Lisp_Objfwd *, const char *, Lisp_Object *);
3101extern void defvar_bool (struct Lisp_Boolfwd *, const char *, bool *); 3101extern void defvar_bool (struct Lisp_Boolfwd *, const char *, bool *);
3102extern void defvar_int (struct Lisp_Intfwd *, const char *, EMACS_INT *); 3102extern void defvar_int (struct Lisp_Intfwd *, const char *, intmax_t *);
3103extern void defvar_kboard (struct Lisp_Kboard_Objfwd *, const char *, int); 3103extern void defvar_kboard (struct Lisp_Kboard_Objfwd *, const char *, int);
3104 3104
3105/* Macros we use to define forwarded Lisp variables. 3105/* Macros we use to define forwarded Lisp variables.
diff --git a/src/lread.c b/src/lread.c
index 6389e3ed48e..8b0d693daf2 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -4422,11 +4422,11 @@ defalias (struct Lisp_Subr *sname, char *string)
4422#endif /* NOTDEF */ 4422#endif /* NOTDEF */
4423 4423
4424/* Define an "integer variable"; a symbol whose value is forwarded to a 4424/* Define an "integer variable"; a symbol whose value is forwarded to a
4425 C variable of type EMACS_INT. Sample call (with "xx" to fool make-docfile): 4425 C variable of type intmax_t. Sample call (with "xx" to fool make-docfile):
4426 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */ 4426 DEFxxVAR_INT ("emacs-priority", &emacs_priority, "Documentation"); */
4427void 4427void
4428defvar_int (struct Lisp_Intfwd *i_fwd, 4428defvar_int (struct Lisp_Intfwd *i_fwd,
4429 const char *namestring, EMACS_INT *address) 4429 const char *namestring, intmax_t *address)
4430{ 4430{
4431 Lisp_Object sym; 4431 Lisp_Object sym;
4432 sym = intern_c_string (namestring); 4432 sym = intern_c_string (namestring);
diff --git a/src/macros.c b/src/macros.c
index eaf9c83fb88..2d927ffc408 100644
--- a/src/macros.c
+++ b/src/macros.c
@@ -267,7 +267,7 @@ pop_kbd_macro (Lisp_Object info)
267 Lisp_Object tem; 267 Lisp_Object tem;
268 Vexecuting_kbd_macro = XCAR (info); 268 Vexecuting_kbd_macro = XCAR (info);
269 tem = XCDR (info); 269 tem = XCDR (info);
270 executing_kbd_macro_index = XFIXNUM (XCAR (tem)); 270 integer_to_intmax (XCAR (tem), &executing_kbd_macro_index);
271 Vreal_this_command = XCDR (tem); 271 Vreal_this_command = XCDR (tem);
272 run_hook (Qkbd_macro_termination_hook); 272 run_hook (Qkbd_macro_termination_hook);
273} 273}
@@ -301,7 +301,7 @@ each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
301 error ("Keyboard macros must be strings or vectors"); 301 error ("Keyboard macros must be strings or vectors");
302 302
303 tem = Fcons (Vexecuting_kbd_macro, 303 tem = Fcons (Vexecuting_kbd_macro,
304 Fcons (make_fixnum (executing_kbd_macro_index), 304 Fcons (make_int (executing_kbd_macro_index),
305 Vreal_this_command)); 305 Vreal_this_command));
306 record_unwind_protect (pop_kbd_macro, tem); 306 record_unwind_protect (pop_kbd_macro, tem);
307 307
diff --git a/src/nsterm.m b/src/nsterm.m
index 2bf3e007860..d0fe206d2e3 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -3911,8 +3911,9 @@ ns_dumpglyphs_image (struct glyph_string *s, NSRect r)
3911 { 3911 {
3912 if (s->hl == DRAW_IMAGE_SUNKEN || s->hl == DRAW_IMAGE_RAISED) 3912 if (s->hl == DRAW_IMAGE_SUNKEN || s->hl == DRAW_IMAGE_RAISED)
3913 { 3913 {
3914 th = tool_bar_button_relief >= 0 ? 3914 th = (tool_bar_button_relief < 0
3915 tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF; 3915 ? DEFAULT_TOOL_BAR_BUTTON_RELIEF
3916 : min (tool_bar_button_relief, 1000000));
3916 raised_p = (s->hl == DRAW_IMAGE_RAISED); 3917 raised_p = (s->hl == DRAW_IMAGE_RAISED);
3917 } 3918 }
3918 else 3919 else
diff --git a/src/pdumper.c b/src/pdumper.c
index 301a52804f0..2f5c7198032 100644
--- a/src/pdumper.c
+++ b/src/pdumper.c
@@ -1615,7 +1615,7 @@ dump_emacs_reloc_immediate (struct dump_context *ctx,
1615 1615
1616DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_lv, Lisp_Object); 1616DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_lv, Lisp_Object);
1617DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_ptrdiff_t, ptrdiff_t); 1617DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_ptrdiff_t, ptrdiff_t);
1618DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_emacs_int, EMACS_INT); 1618DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_intmax_t, intmax_t);
1619DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_int, int); 1619DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_int, int);
1620DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_bool, bool); 1620DEFINE_EMACS_IMMEDIATE_FN (dump_emacs_reloc_immediate_bool, bool);
1621 1621
@@ -2286,10 +2286,10 @@ dump_float (struct dump_context *ctx, const struct Lisp_Float *lfloat)
2286static dump_off 2286static dump_off
2287dump_fwd_int (struct dump_context *ctx, const struct Lisp_Intfwd *intfwd) 2287dump_fwd_int (struct dump_context *ctx, const struct Lisp_Intfwd *intfwd)
2288{ 2288{
2289#if CHECK_STRUCTS && !defined (HASH_Lisp_Intfwd_1225FA32CC) 2289#if CHECK_STRUCTS && !defined HASH_Lisp_Intfwd_4D887A7387
2290# error "Lisp_Intfwd changed. See CHECK_STRUCTS comment." 2290# error "Lisp_Intfwd changed. See CHECK_STRUCTS comment."
2291#endif 2291#endif
2292 dump_emacs_reloc_immediate_emacs_int (ctx, intfwd->intvar, *intfwd->intvar); 2292 dump_emacs_reloc_immediate_intmax_t (ctx, intfwd->intvar, *intfwd->intvar);
2293 struct Lisp_Intfwd out; 2293 struct Lisp_Intfwd out;
2294 dump_object_start (ctx, &out, sizeof (out)); 2294 dump_object_start (ctx, &out, sizeof (out));
2295 DUMP_FIELD_COPY (&out, intfwd, type); 2295 DUMP_FIELD_COPY (&out, intfwd, type);
diff --git a/src/profiler.c b/src/profiler.c
index 2aa5f345740..87be30acc30 100644
--- a/src/profiler.c
+++ b/src/profiler.c
@@ -52,12 +52,16 @@ static const struct hash_table_test hashtest_profiler =
52 }; 52 };
53 53
54static Lisp_Object 54static Lisp_Object
55make_log (EMACS_INT heap_size, EMACS_INT max_stack_depth) 55make_log (void)
56{ 56{
57 /* We use a standard Elisp hash-table object, but we use it in 57 /* We use a standard Elisp hash-table object, but we use it in
58 a special way. This is OK as long as the object is not exposed 58 a special way. This is OK as long as the object is not exposed
59 to Elisp, i.e. until it is returned by *-profiler-log, after which 59 to Elisp, i.e. until it is returned by *-profiler-log, after which
60 it can't be used any more. */ 60 it can't be used any more. */
61 EMACS_INT heap_size
62 = clip_to_bounds (0, profiler_log_size, MOST_POSITIVE_FIXNUM);
63 ptrdiff_t max_stack_depth
64 = clip_to_bounds (0, profiler_max_stack_depth, PTRDIFF_MAX);;
61 Lisp_Object log = make_hash_table (hashtest_profiler, heap_size, 65 Lisp_Object log = make_hash_table (hashtest_profiler, heap_size,
62 DEFAULT_REHASH_SIZE, 66 DEFAULT_REHASH_SIZE,
63 DEFAULT_REHASH_THRESHOLD, 67 DEFAULT_REHASH_THRESHOLD,
@@ -342,8 +346,7 @@ See also `profiler-log-size' and `profiler-max-stack-depth'. */)
342 if (NILP (cpu_log)) 346 if (NILP (cpu_log))
343 { 347 {
344 cpu_gc_count = 0; 348 cpu_gc_count = 0;
345 cpu_log = make_log (profiler_log_size, 349 cpu_log = make_log ();
346 profiler_max_stack_depth);
347 } 350 }
348 351
349 int status = setup_cpu_timer (sampling_interval); 352 int status = setup_cpu_timer (sampling_interval);
@@ -419,9 +422,7 @@ Before returning, a new log is allocated for future samples. */)
419 /* Here we're making the log visible to Elisp, so it's not safe any 422 /* Here we're making the log visible to Elisp, so it's not safe any
420 more for our use afterwards since we can't rely on its special 423 more for our use afterwards since we can't rely on its special
421 pre-allocated keys anymore. So we have to allocate a new one. */ 424 pre-allocated keys anymore. So we have to allocate a new one. */
422 cpu_log = (profiler_cpu_running 425 cpu_log = profiler_cpu_running ? make_log () : Qnil;
423 ? make_log (profiler_log_size, profiler_max_stack_depth)
424 : Qnil);
425 Fputhash (make_vector (1, QAutomatic_GC), 426 Fputhash (make_vector (1, QAutomatic_GC),
426 make_fixnum (cpu_gc_count), 427 make_fixnum (cpu_gc_count),
427 result); 428 result);
@@ -450,8 +451,7 @@ See also `profiler-log-size' and `profiler-max-stack-depth'. */)
450 error ("Memory profiler is already running"); 451 error ("Memory profiler is already running");
451 452
452 if (NILP (memory_log)) 453 if (NILP (memory_log))
453 memory_log = make_log (profiler_log_size, 454 memory_log = make_log ();
454 profiler_max_stack_depth);
455 455
456 profiler_memory_running = true; 456 profiler_memory_running = true;
457 457
@@ -494,9 +494,7 @@ Before returning, a new log is allocated for future samples. */)
494 /* Here we're making the log visible to Elisp , so it's not safe any 494 /* Here we're making the log visible to Elisp , so it's not safe any
495 more for our use afterwards since we can't rely on its special 495 more for our use afterwards since we can't rely on its special
496 pre-allocated keys anymore. So we have to allocate a new one. */ 496 pre-allocated keys anymore. So we have to allocate a new one. */
497 memory_log = (profiler_memory_running 497 memory_log = profiler_memory_running ? make_log () : Qnil;
498 ? make_log (profiler_log_size, profiler_max_stack_depth)
499 : Qnil);
500 return result; 498 return result;
501} 499}
502 500
diff --git a/src/scroll.c b/src/scroll.c
index e523a19ab89..8eda510945f 100644
--- a/src/scroll.c
+++ b/src/scroll.c
@@ -107,10 +107,8 @@ calculate_scrolling (struct frame *frame,
107 /* Discourage long scrolls on fast lines. 107 /* Discourage long scrolls on fast lines.
108 Don't scroll nearly a full frame height unless it saves 108 Don't scroll nearly a full frame height unless it saves
109 at least 1/4 second. */ 109 at least 1/4 second. */
110 int extra_cost = baud_rate / (10 * 4 * frame_total_lines); 110 int extra_cost
111 111 = clip_to_bounds (1, baud_rate / (10 * 4) / frame_total_lines, INT_MAX / 2);
112 if (baud_rate <= 0)
113 extra_cost = 1;
114 112
115 /* initialize the top left corner of the matrix */ 113 /* initialize the top left corner of the matrix */
116 matrix->writecost = 0; 114 matrix->writecost = 0;
@@ -446,10 +444,8 @@ calculate_direct_scrolling (struct frame *frame,
446 /* Discourage long scrolls on fast lines. 444 /* Discourage long scrolls on fast lines.
447 Don't scroll nearly a full frame height unless it saves 445 Don't scroll nearly a full frame height unless it saves
448 at least 1/4 second. */ 446 at least 1/4 second. */
449 int extra_cost = baud_rate / (10 * 4 * frame_total_lines); 447 int extra_cost
450 448 = clip_to_bounds (1, baud_rate / (10 * 4) / frame_total_lines, INT_MAX / 2);
451 if (baud_rate <= 0)
452 extra_cost = 1;
453 449
454 /* Overhead of setting the scroll window, plus the extra 450 /* Overhead of setting the scroll window, plus the extra
455 cost of scrolling by a distance of one. The extra cost is 451 cost of scrolling by a distance of one. The extra cost is
diff --git a/src/term.c b/src/term.c
index 60ee8619484..a492276c888 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1201,7 +1201,9 @@ calculate_costs (struct frame *frame)
1201 calculate_ins_del_char_costs (frame); 1201 calculate_ins_del_char_costs (frame);
1202 1202
1203 /* Don't use TS_repeat if its padding is worse than sending the chars */ 1203 /* Don't use TS_repeat if its padding is worse than sending the chars */
1204 if (tty->TS_repeat && per_line_cost (tty->TS_repeat) * baud_rate < 9000) 1204 if (tty->TS_repeat
1205 && (baud_rate <= 0
1206 || per_line_cost (tty->TS_repeat) < 9000 / baud_rate))
1205 tty->RPov = string_cost (tty->TS_repeat); 1207 tty->RPov = string_cost (tty->TS_repeat);
1206 else 1208 else
1207 tty->RPov = FRAME_COLS (frame) * 2; 1209 tty->RPov = FRAME_COLS (frame) * 2;
diff --git a/src/termcap.c b/src/termcap.c
index 2f2a0b29d5e..9e081baa62e 100644
--- a/src/termcap.c
+++ b/src/termcap.c
@@ -20,10 +20,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 20
21/* Emacs config.h may rename various library functions such as malloc. */ 21/* Emacs config.h may rename various library functions such as malloc. */
22#include <config.h> 22#include <config.h>
23
24#include <stdlib.h>
23#include <sys/file.h> 25#include <sys/file.h>
24#include <fcntl.h> 26#include <fcntl.h>
25#include <unistd.h> 27#include <unistd.h>
26 28
29#include <intprops.h>
30
27#include "lisp.h" 31#include "lisp.h"
28#include "tparam.h" 32#include "tparam.h"
29#ifdef MSDOS 33#ifdef MSDOS
@@ -265,14 +269,7 @@ char PC;
265void 269void
266tputs (register const char *str, int nlines, int (*outfun) (int)) 270tputs (register const char *str, int nlines, int (*outfun) (int))
267{ 271{
268 register int padcount = 0; 272 int padcount = 0;
269 register int speed;
270
271 speed = baud_rate;
272 /* For quite high speeds, convert to the smaller
273 units to avoid overflow. */
274 if (speed > 10000)
275 speed = - speed / 100;
276 273
277 if (!str) 274 if (!str)
278 return; 275 return;
@@ -296,21 +293,13 @@ tputs (register const char *str, int nlines, int (*outfun) (int))
296 (*outfun) (*str++); 293 (*outfun) (*str++);
297 294
298 /* PADCOUNT is now in units of tenths of msec. 295 /* PADCOUNT is now in units of tenths of msec.
299 SPEED is measured in characters per 10 seconds 296 BAUD_RATE is measured in characters per 10 seconds.
300 or in characters per .1 seconds (if negative). 297 Compute PADFACTOR = 100000 * (how many padding bytes are needed). */
301 We use the smaller units for larger speeds to avoid overflow. */ 298 intmax_t padfactor;
302 padcount *= speed; 299 if (INT_MULTIPLY_WRAPV (padcount, baud_rate, &padfactor))
303 padcount += 500; 300 padfactor = baud_rate < 0 ? INTMAX_MIN : INTMAX_MAX;
304 padcount /= 1000;
305 if (speed < 0)
306 padcount = -padcount;
307 else
308 {
309 padcount += 50;
310 padcount /= 100;
311 }
312 301
313 while (padcount-- > 0) 302 for (; 50000 <= padfactor; padfactor -= 100000)
314 (*outfun) (PC); 303 (*outfun) (PC);
315} 304}
316 305
@@ -426,7 +415,7 @@ tgetent (char *bp, const char *name)
426 } 415 }
427 416
428 if (!termcap_name || !filep) 417 if (!termcap_name || !filep)
429 termcap_name = TERMCAP_FILE; 418 termcap_name = (char *) TERMCAP_FILE;
430 419
431 /* Here we know we must search a file and termcap_name has its name. */ 420 /* Here we know we must search a file and termcap_name has its name. */
432 421
diff --git a/src/thread.h b/src/thread.h
index 5e003761e85..e46545baf27 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -104,7 +104,7 @@ struct thread_state
104#define specpdl_ptr (current_thread->m_specpdl_ptr) 104#define specpdl_ptr (current_thread->m_specpdl_ptr)
105 105
106 /* Depth in Lisp evaluations and function calls. */ 106 /* Depth in Lisp evaluations and function calls. */
107 EMACS_INT m_lisp_eval_depth; 107 intmax_t m_lisp_eval_depth;
108#define lisp_eval_depth (current_thread->m_lisp_eval_depth) 108#define lisp_eval_depth (current_thread->m_lisp_eval_depth)
109 109
110 /* This points to the current buffer. */ 110 /* This points to the current buffer. */
diff --git a/src/undo.c b/src/undo.c
index 2929f792128..3c1251dae6e 100644
--- a/src/undo.c
+++ b/src/undo.c
@@ -291,7 +291,7 @@ truncate_undo_list (struct buffer *b)
291{ 291{
292 Lisp_Object list; 292 Lisp_Object list;
293 Lisp_Object prev, next, last_boundary; 293 Lisp_Object prev, next, last_boundary;
294 EMACS_INT size_so_far = 0; 294 intmax_t size_so_far = 0;
295 295
296 /* Make sure that calling undo-outer-limit-function 296 /* Make sure that calling undo-outer-limit-function
297 won't cause another GC. */ 297 won't cause another GC. */
@@ -348,14 +348,17 @@ truncate_undo_list (struct buffer *b)
348 348
349 /* If by the first boundary we have already passed undo_outer_limit, 349 /* If by the first boundary we have already passed undo_outer_limit,
350 we're heading for memory full, so offer to clear out the list. */ 350 we're heading for memory full, so offer to clear out the list. */
351 if (FIXNUMP (Vundo_outer_limit) 351 intmax_t undo_outer_limit;
352 && size_so_far > XFIXNUM (Vundo_outer_limit) 352 if ((INTEGERP (Vundo_outer_limit)
353 && (integer_to_intmax (Vundo_outer_limit, &undo_outer_limit)
354 ? undo_outer_limit < size_so_far
355 : NILP (Fnatnump (Vundo_outer_limit))))
353 && !NILP (Vundo_outer_limit_function)) 356 && !NILP (Vundo_outer_limit_function))
354 { 357 {
355 Lisp_Object tem; 358 Lisp_Object tem;
356 359
357 /* Normally the function this calls is undo-outer-limit-truncate. */ 360 /* Normally the function this calls is undo-outer-limit-truncate. */
358 tem = call1 (Vundo_outer_limit_function, make_fixnum (size_so_far)); 361 tem = call1 (Vundo_outer_limit_function, make_int (size_so_far));
359 if (! NILP (tem)) 362 if (! NILP (tem))
360 { 363 {
361 /* The function is responsible for making 364 /* The function is responsible for making
diff --git a/src/window.c b/src/window.c
index 49d7fc2426b..fe685d5ab09 100644
--- a/src/window.c
+++ b/src/window.c
@@ -5445,6 +5445,11 @@ window_scroll_margin (struct window *window, enum margin_unit unit)
5445 return 0; 5445 return 0;
5446} 5446}
5447 5447
5448static int
5449sanitize_next_screen_context_lines (void)
5450{
5451 return clip_to_bounds (0, next_screen_context_lines, 1000000);
5452}
5448 5453
5449/* Implementation of window_scroll that works based on pixel line 5454/* Implementation of window_scroll that works based on pixel line
5450 heights. See the comment of window_scroll for parameter 5455 heights. See the comment of window_scroll for parameter
@@ -5515,9 +5520,11 @@ window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
5515 height. This is important to ensure we get back to the 5520 height. This is important to ensure we get back to the
5516 same position when scrolling up, then down. */ 5521 same position when scrolling up, then down. */
5517 if (whole) 5522 if (whole)
5518 dy = max ((window_box_height (w) / dy 5523 {
5519 - next_screen_context_lines) * dy, 5524 int ht = window_box_height (w);
5520 dy); 5525 int nscls = sanitize_next_screen_context_lines ();
5526 dy = max (dy, (ht / dy - nscls) * dy);
5527 }
5521 dy *= n; 5528 dy *= n;
5522 5529
5523 if (n < 0) 5530 if (n < 0)
@@ -5598,13 +5605,14 @@ window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
5598 { 5605 {
5599 ptrdiff_t start_pos = IT_CHARPOS (it); 5606 ptrdiff_t start_pos = IT_CHARPOS (it);
5600 int dy = frame_line_height; 5607 int dy = frame_line_height;
5608 int ht = window_box_height (w);
5609 int nscls = sanitize_next_screen_context_lines ();
5601 /* In the below we divide the window box height by the frame's 5610 /* In the below we divide the window box height by the frame's
5602 line height to make the result predictable when the window 5611 line height to make the result predictable when the window
5603 box is not an integral multiple of the line height. This is 5612 box is not an integral multiple of the line height. This is
5604 important to ensure we get back to the same position when 5613 important to ensure we get back to the same position when
5605 scrolling up, then down. */ 5614 scrolling up, then down. */
5606 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy, 5615 dy = n * max (dy, (ht / dy - nscls) * dy);
5607 dy) * n;
5608 5616
5609 /* Note that move_it_vertically always moves the iterator to the 5617 /* Note that move_it_vertically always moves the iterator to the
5610 start of a line. So, if the last line doesn't have a newline, 5618 start of a line. So, if the last line doesn't have a newline,
@@ -5902,7 +5910,10 @@ window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5902 /* If scrolling screen-fulls, compute the number of lines to 5910 /* If scrolling screen-fulls, compute the number of lines to
5903 scroll from the window's height. */ 5911 scroll from the window's height. */
5904 if (whole) 5912 if (whole)
5905 n *= max (1, ht - next_screen_context_lines); 5913 {
5914 int nscls = sanitize_next_screen_context_lines ();
5915 n *= max (1, ht - nscls);
5916 }
5906 5917
5907 if (!NILP (Vscroll_preserve_screen_position)) 5918 if (!NILP (Vscroll_preserve_screen_position))
5908 { 5919 {
diff --git a/src/xdisp.c b/src/xdisp.c
index 0bffaeb60bc..760c31c6768 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -12533,7 +12533,8 @@ build_desired_tool_bar_string (struct frame *f)
12533 12533
12534 /* Compute margin and relief to draw. */ 12534 /* Compute margin and relief to draw. */
12535 relief = (tool_bar_button_relief >= 0 12535 relief = (tool_bar_button_relief >= 0
12536 ? tool_bar_button_relief 12536 ? min (tool_bar_button_relief,
12537 min (INT_MAX, MOST_POSITIVE_FIXNUM))
12537 : DEFAULT_TOOL_BAR_BUTTON_RELIEF); 12538 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12538 hmargin = vmargin = relief; 12539 hmargin = vmargin = relief;
12539 12540
@@ -13334,7 +13335,8 @@ hscroll_window_tree (Lisp_Object window)
13334 text_area_width = window_box_width (w, TEXT_AREA); 13335 text_area_width = window_box_width (w, TEXT_AREA);
13335 13336
13336 /* Scroll when cursor is inside this scroll margin. */ 13337 /* Scroll when cursor is inside this scroll margin. */
13337 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w); 13338 h_margin = (clip_to_bounds (0, hscroll_margin, 1000000)
13339 * WINDOW_FRAME_COLUMN_WIDTH (w));
13338 13340
13339 /* If the position of this window's point has explicitly 13341 /* If the position of this window's point has explicitly
13340 changed, no more suspend auto hscrolling. */ 13342 changed, no more suspend auto hscrolling. */
@@ -15765,7 +15767,7 @@ enum
15765 15767
15766static int 15768static int
15767try_scrolling (Lisp_Object window, bool just_this_one_p, 15769try_scrolling (Lisp_Object window, bool just_this_one_p,
15768 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step, 15770 intmax_t arg_scroll_conservatively, intmax_t scroll_step,
15769 bool temp_scroll_step, bool last_line_misfit) 15771 bool temp_scroll_step, bool last_line_misfit)
15770{ 15772{
15771 struct window *w = XWINDOW (window); 15773 struct window *w = XWINDOW (window);
@@ -15797,12 +15799,15 @@ try_scrolling (Lisp_Object window, bool just_this_one_p,
15797 arg_scroll_conservatively = scroll_limit + 1; 15799 arg_scroll_conservatively = scroll_limit + 1;
15798 scroll_max = scroll_limit * frame_line_height; 15800 scroll_max = scroll_limit * frame_line_height;
15799 } 15801 }
15800 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step) 15802 else if (0 < scroll_step || 0 < arg_scroll_conservatively || temp_scroll_step)
15801 /* Compute how much we should try to scroll maximally to bring 15803 /* Compute how much we should try to scroll maximally to bring
15802 point into view. */ 15804 point into view. */
15803 scroll_max = (max (scroll_step, 15805 {
15804 max (arg_scroll_conservatively, temp_scroll_step)) 15806 intmax_t scroll_lines_max
15805 * frame_line_height); 15807 = max (scroll_step, max (arg_scroll_conservatively, temp_scroll_step));
15808 int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
15809 scroll_max = scroll_lines * frame_line_height;
15810 }
15806 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively)) 15811 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15807 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))) 15812 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15808 /* We're trying to scroll because of aggressive scrolling but no 15813 /* We're trying to scroll because of aggressive scrolling but no
@@ -17295,8 +17300,8 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
17295 } 17300 }
17296 17301
17297 /* Try to scroll by specified few lines. */ 17302 /* Try to scroll by specified few lines. */
17298 if ((scroll_conservatively 17303 if ((0 < scroll_conservatively
17299 || emacs_scroll_step 17304 || 0 < emacs_scroll_step
17300 || temp_scroll_step 17305 || temp_scroll_step
17301 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)) 17306 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17302 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively))) 17307 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
@@ -24749,8 +24754,12 @@ decode_mode_spec (struct window *w, register int c, int field_width,
24749 ptrdiff_t limit = BUF_BEGV (b); 24754 ptrdiff_t limit = BUF_BEGV (b);
24750 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b); 24755 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24751 ptrdiff_t position; 24756 ptrdiff_t position;
24752 ptrdiff_t distance = 24757 ptrdiff_t distance
24753 (height * 2 + 30) * line_number_display_limit_width; 24758 = (line_number_display_limit_width < 0 ? 0
24759 : INT_MULTIPLY_WRAPV (line_number_display_limit_width,
24760 height * 2 + 30,
24761 &distance)
24762 ? PTRDIFF_MAX : distance);
24754 24763
24755 if (startpos - distance > limit) 24764 if (startpos - distance > limit)
24756 { 24765 {
@@ -28377,7 +28386,7 @@ x_produce_glyphs (struct it *it)
28377 /* If face has an overline, add the height of the overline 28386 /* If face has an overline, add the height of the overline
28378 (1 pixel) and a 1 pixel margin to the character height. */ 28387 (1 pixel) and a 1 pixel margin to the character height. */
28379 if (face->overline_p) 28388 if (face->overline_p)
28380 it->ascent += overline_margin; 28389 it->ascent += clip_to_bounds (0, overline_margin, 1000000);
28381 28390
28382 if (it->constrain_row_ascent_descent_p) 28391 if (it->constrain_row_ascent_descent_p)
28383 { 28392 {
@@ -28918,7 +28927,7 @@ x_produce_glyphs (struct it *it)
28918 /* If face has an overline, add the height of the overline 28927 /* If face has an overline, add the height of the overline
28919 (1 pixel) and a 1 pixel margin to the character height. */ 28928 (1 pixel) and a 1 pixel margin to the character height. */
28920 if (face->overline_p) 28929 if (face->overline_p)
28921 it->ascent += overline_margin; 28930 it->ascent += clip_to_bounds (0, overline_margin, 1000000);
28922 28931
28923 take_vertical_position_into_account (it); 28932 take_vertical_position_into_account (it);
28924 if (it->ascent < 0) 28933 if (it->ascent < 0)
@@ -28967,7 +28976,7 @@ x_produce_glyphs (struct it *it)
28967 /* If face has an overline, add the height of the overline 28976 /* If face has an overline, add the height of the overline
28968 (1 pixel) and a 1 pixel margin to the character height. */ 28977 (1 pixel) and a 1 pixel margin to the character height. */
28969 if (face->overline_p) 28978 if (face->overline_p)
28970 it->ascent += overline_margin; 28979 it->ascent += clip_to_bounds (0, overline_margin, 1000000);
28971 take_vertical_position_into_account (it); 28980 take_vertical_position_into_account (it);
28972 if (it->ascent < 0) 28981 if (it->ascent < 0)
28973 it->ascent = 0; 28982 it->ascent = 0;
diff --git a/src/xselect.c b/src/xselect.c
index 37efd43b955..5f0bb44cc9a 100644
--- a/src/xselect.c
+++ b/src/xselect.c
@@ -1085,10 +1085,10 @@ wait_for_property_change (struct prop_location *location)
1085 property_change_reply, because property_change_reply_object says so. */ 1085 property_change_reply, because property_change_reply_object says so. */
1086 if (! location->arrived) 1086 if (! location->arrived)
1087 { 1087 {
1088 EMACS_INT timeout = max (0, x_selection_timeout); 1088 intmax_t timeout = max (0, x_selection_timeout);
1089 EMACS_INT secs = timeout / 1000; 1089 intmax_t secs = timeout / 1000;
1090 int nsecs = (timeout % 1000) * 1000000; 1090 int nsecs = (timeout % 1000) * 1000000;
1091 TRACE2 (" Waiting %"pI"d secs, %d nsecs", secs, nsecs); 1091 TRACE2 (" Waiting %"PRIdMAX" secs, %d nsecs", secs, nsecs);
1092 wait_reading_process_output (secs, nsecs, 0, false, 1092 wait_reading_process_output (secs, nsecs, 0, false,
1093 property_change_reply, NULL, 0); 1093 property_change_reply, NULL, 0);
1094 1094
@@ -1158,8 +1158,6 @@ x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1158 Atom type_atom = (CONSP (target_type) 1158 Atom type_atom = (CONSP (target_type)
1159 ? symbol_to_x_atom (dpyinfo, XCAR (target_type)) 1159 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1160 : symbol_to_x_atom (dpyinfo, target_type)); 1160 : symbol_to_x_atom (dpyinfo, target_type));
1161 EMACS_INT timeout, secs;
1162 int nsecs;
1163 1161
1164 if (!FRAME_LIVE_P (f)) 1162 if (!FRAME_LIVE_P (f))
1165 return Qnil; 1163 return Qnil;
@@ -1195,10 +1193,10 @@ x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1195 unblock_input (); 1193 unblock_input ();
1196 1194
1197 /* This allows quits. Also, don't wait forever. */ 1195 /* This allows quits. Also, don't wait forever. */
1198 timeout = max (0, x_selection_timeout); 1196 intmax_t timeout = max (0, x_selection_timeout);
1199 secs = timeout / 1000; 1197 intmax_t secs = timeout / 1000;
1200 nsecs = (timeout % 1000) * 1000000; 1198 int nsecs = (timeout % 1000) * 1000000;
1201 TRACE1 (" Start waiting %"pI"d secs for SelectionNotify", secs); 1199 TRACE1 (" Start waiting %"PRIdMAX" secs for SelectionNotify", secs);
1202 wait_reading_process_output (secs, nsecs, 0, false, 1200 wait_reading_process_output (secs, nsecs, 0, false,
1203 reading_selection_reply, NULL, 0); 1201 reading_selection_reply, NULL, 0);
1204 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply))); 1202 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
diff --git a/src/xterm.c b/src/xterm.c
index d8eb45a00c0..453669f6e02 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -3131,7 +3131,9 @@ x_draw_image_relief (struct glyph_string *s)
3131 if (s->hl == DRAW_IMAGE_SUNKEN 3131 if (s->hl == DRAW_IMAGE_SUNKEN
3132 || s->hl == DRAW_IMAGE_RAISED) 3132 || s->hl == DRAW_IMAGE_RAISED)
3133 { 3133 {
3134 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF; 3134 thick = (tool_bar_button_relief < 0
3135 ? DEFAULT_TOOL_BAR_BUTTON_RELIEF
3136 : min (tool_bar_button_relief, 1000000));
3135 raised_p = s->hl == DRAW_IMAGE_RAISED; 3137 raised_p = s->hl == DRAW_IMAGE_RAISED;
3136 } 3138 }
3137 else 3139 else
@@ -4884,7 +4886,7 @@ x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4884} 4886}
4885 4887
4886static int 4888static int
4887x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state) 4889x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, intmax_t state)
4888{ 4890{
4889 EMACS_INT mod_ctrl = ctrl_modifier; 4891 EMACS_INT mod_ctrl = ctrl_modifier;
4890 EMACS_INT mod_meta = meta_modifier; 4892 EMACS_INT mod_meta = meta_modifier;