aboutsummaryrefslogtreecommitdiffstats
path: root/src/frame.c
diff options
context:
space:
mode:
authorPaul Eggert2020-04-05 01:17:32 -0700
committerPaul Eggert2020-04-05 01:24:36 -0700
commitbec5cfee7660f6e283efbd30a693a6f8e9ea46b8 (patch)
treeb6b872dfb83579336e848a62f720b629426c0ac0 /src/frame.c
parent9b8dacdb264412b919782920da916e306102262a (diff)
downloademacs-bec5cfee7660f6e283efbd30a693a6f8e9ea46b8.tar.gz
emacs-bec5cfee7660f6e283efbd30a693a6f8e9ea46b8.zip
Improve integer range checking
* src/bignum.c (check_integer_range, check_uinteger_max) (check_int_nonnegative): New functions. * src/frame.c (check_frame_pixels): New function. (Fset_frame_height, Fset_frame_width, Fset_frame_size): Use it. * src/lisp.h (CHECK_RANGED_INTEGER, CHECK_TYPE_RANGED_INTEGER): Remove these macros. Unless otherwise specified, all callers replaced by calls to check_integer_range, check_uinteger_range, check_int_nonnegative. * src/frame.c (gui_set_right_divider_width) (gui_set_bottom_divider_width): * src/nsfns.m (ns_set_internal_border_width): * src/xfns.c (x_set_internal_border_width): Using check_int_nonnegative means these functions no longer incorrectly reject negative bignums; they treat them as 0, just like negative fixnums.
Diffstat (limited to 'src/frame.c')
-rw-r--r--src/frame.c99
1 files changed, 43 insertions, 56 deletions
diff --git a/src/frame.c b/src/frame.c
index c7e4f2f6bd9..884de2f5349 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -2558,26 +2558,26 @@ before calling this function on it, like this.
2558 (Lisp_Object frame, Lisp_Object x, Lisp_Object y) 2558 (Lisp_Object frame, Lisp_Object x, Lisp_Object y)
2559{ 2559{
2560 CHECK_LIVE_FRAME (frame); 2560 CHECK_LIVE_FRAME (frame);
2561 CHECK_TYPE_RANGED_INTEGER (int, x); 2561 int xval = check_integer_range (x, INT_MIN, INT_MAX);
2562 CHECK_TYPE_RANGED_INTEGER (int, y); 2562 int yval = check_integer_range (y, INT_MIN, INT_MAX);
2563 2563
2564 /* I think this should be done with a hook. */ 2564 /* I think this should be done with a hook. */
2565#ifdef HAVE_WINDOW_SYSTEM 2565#ifdef HAVE_WINDOW_SYSTEM
2566 if (FRAME_WINDOW_P (XFRAME (frame))) 2566 if (FRAME_WINDOW_P (XFRAME (frame)))
2567 /* Warping the mouse will cause enternotify and focus events. */ 2567 /* Warping the mouse will cause enternotify and focus events. */
2568 frame_set_mouse_position (XFRAME (frame), XFIXNUM (x), XFIXNUM (y)); 2568 frame_set_mouse_position (XFRAME (frame), xval, yval);
2569#else 2569#else
2570#if defined (MSDOS) 2570#if defined (MSDOS)
2571 if (FRAME_MSDOS_P (XFRAME (frame))) 2571 if (FRAME_MSDOS_P (XFRAME (frame)))
2572 { 2572 {
2573 Fselect_frame (frame, Qnil); 2573 Fselect_frame (frame, Qnil);
2574 mouse_moveto (XFIXNUM (x), XFIXNUM (y)); 2574 mouse_moveto (xval, yval);
2575 } 2575 }
2576#else 2576#else
2577#ifdef HAVE_GPM 2577#ifdef HAVE_GPM
2578 { 2578 {
2579 Fselect_frame (frame, Qnil); 2579 Fselect_frame (frame, Qnil);
2580 term_mouse_moveto (XFIXNUM (x), XFIXNUM (y)); 2580 term_mouse_moveto (xval, yval);
2581 } 2581 }
2582#endif 2582#endif
2583#endif 2583#endif
@@ -2599,26 +2599,26 @@ before calling this function on it, like this.
2599 (Lisp_Object frame, Lisp_Object x, Lisp_Object y) 2599 (Lisp_Object frame, Lisp_Object x, Lisp_Object y)
2600{ 2600{
2601 CHECK_LIVE_FRAME (frame); 2601 CHECK_LIVE_FRAME (frame);
2602 CHECK_TYPE_RANGED_INTEGER (int, x); 2602 int xval = check_integer_range (x, INT_MIN, INT_MAX);
2603 CHECK_TYPE_RANGED_INTEGER (int, y); 2603 int yval = check_integer_range (y, INT_MIN, INT_MAX);
2604 2604
2605 /* I think this should be done with a hook. */ 2605 /* I think this should be done with a hook. */
2606#ifdef HAVE_WINDOW_SYSTEM 2606#ifdef HAVE_WINDOW_SYSTEM
2607 if (FRAME_WINDOW_P (XFRAME (frame))) 2607 if (FRAME_WINDOW_P (XFRAME (frame)))
2608 /* Warping the mouse will cause enternotify and focus events. */ 2608 /* Warping the mouse will cause enternotify and focus events. */
2609 frame_set_mouse_pixel_position (XFRAME (frame), XFIXNUM (x), XFIXNUM (y)); 2609 frame_set_mouse_pixel_position (XFRAME (frame), xval, yval);
2610#else 2610#else
2611#if defined (MSDOS) 2611#if defined (MSDOS)
2612 if (FRAME_MSDOS_P (XFRAME (frame))) 2612 if (FRAME_MSDOS_P (XFRAME (frame)))
2613 { 2613 {
2614 Fselect_frame (frame, Qnil); 2614 Fselect_frame (frame, Qnil);
2615 mouse_moveto (XFIXNUM (x), XFIXNUM (y)); 2615 mouse_moveto (xval, yval);
2616 } 2616 }
2617#else 2617#else
2618#ifdef HAVE_GPM 2618#ifdef HAVE_GPM
2619 { 2619 {
2620 Fselect_frame (frame, Qnil); 2620 Fselect_frame (frame, Qnil);
2621 term_mouse_moveto (XFIXNUM (x), XFIXNUM (y)); 2621 term_mouse_moveto (xval, yval);
2622 } 2622 }
2623#endif 2623#endif
2624#endif 2624#endif
@@ -3545,6 +3545,21 @@ DEFUN ("frame-bottom-divider-width", Fbottom_divider_width, Sbottom_divider_widt
3545 return make_fixnum (FRAME_BOTTOM_DIVIDER_WIDTH (decode_any_frame (frame))); 3545 return make_fixnum (FRAME_BOTTOM_DIVIDER_WIDTH (decode_any_frame (frame)));
3546} 3546}
3547 3547
3548static int
3549check_frame_pixels (Lisp_Object size, Lisp_Object pixelwise, int item_size)
3550{
3551 CHECK_INTEGER (size);
3552 if (!NILP (pixelwise))
3553 item_size = 1;
3554 intmax_t sz;
3555 int pixel_size; /* size * item_size */
3556 if (! integer_to_intmax (size, &sz)
3557 || INT_MULTIPLY_WRAPV (sz, item_size, &pixel_size))
3558 args_out_of_range_3 (size, make_int (INT_MIN / item_size),
3559 make_int (INT_MAX / item_size));
3560 return pixel_size;
3561}
3562
3548DEFUN ("set-frame-height", Fset_frame_height, Sset_frame_height, 2, 4, 3563DEFUN ("set-frame-height", Fset_frame_height, Sset_frame_height, 2, 4,
3549 "(list (selected-frame) (prefix-numeric-value current-prefix-arg))", 3564 "(list (selected-frame) (prefix-numeric-value current-prefix-arg))",
3550 doc: /* Set text height of frame FRAME to HEIGHT lines. 3565 doc: /* Set text height of frame FRAME to HEIGHT lines.
@@ -3562,15 +3577,9 @@ currently selected frame will be set to this height. */)
3562 (Lisp_Object frame, Lisp_Object height, Lisp_Object pretend, Lisp_Object pixelwise) 3577 (Lisp_Object frame, Lisp_Object height, Lisp_Object pretend, Lisp_Object pixelwise)
3563{ 3578{
3564 struct frame *f = decode_live_frame (frame); 3579 struct frame *f = decode_live_frame (frame);
3565 int pixel_height; 3580 int pixel_height = check_frame_pixels (height, pixelwise,
3566 3581 FRAME_LINE_HEIGHT (f));
3567 CHECK_TYPE_RANGED_INTEGER (int, height);
3568
3569 pixel_height = (!NILP (pixelwise)
3570 ? XFIXNUM (height)
3571 : XFIXNUM (height) * FRAME_LINE_HEIGHT (f));
3572 adjust_frame_size (f, -1, pixel_height, 1, !NILP (pretend), Qheight); 3582 adjust_frame_size (f, -1, pixel_height, 1, !NILP (pretend), Qheight);
3573
3574 return Qnil; 3583 return Qnil;
3575} 3584}
3576 3585
@@ -3591,15 +3600,9 @@ currently selected frame will be set to this width. */)
3591 (Lisp_Object frame, Lisp_Object width, Lisp_Object pretend, Lisp_Object pixelwise) 3600 (Lisp_Object frame, Lisp_Object width, Lisp_Object pretend, Lisp_Object pixelwise)
3592{ 3601{
3593 struct frame *f = decode_live_frame (frame); 3602 struct frame *f = decode_live_frame (frame);
3594 int pixel_width; 3603 int pixel_width = check_frame_pixels (width, pixelwise,
3595 3604 FRAME_COLUMN_WIDTH (f));
3596 CHECK_TYPE_RANGED_INTEGER (int, width);
3597
3598 pixel_width = (!NILP (pixelwise)
3599 ? XFIXNUM (width)
3600 : XFIXNUM (width) * FRAME_COLUMN_WIDTH (f));
3601 adjust_frame_size (f, pixel_width, -1, 1, !NILP (pretend), Qwidth); 3605 adjust_frame_size (f, pixel_width, -1, 1, !NILP (pretend), Qwidth);
3602
3603 return Qnil; 3606 return Qnil;
3604} 3607}
3605 3608
@@ -3613,19 +3616,11 @@ font height. */)
3613 (Lisp_Object frame, Lisp_Object width, Lisp_Object height, Lisp_Object pixelwise) 3616 (Lisp_Object frame, Lisp_Object width, Lisp_Object height, Lisp_Object pixelwise)
3614{ 3617{
3615 struct frame *f = decode_live_frame (frame); 3618 struct frame *f = decode_live_frame (frame);
3616 int pixel_width, pixel_height; 3619 int pixel_width = check_frame_pixels (width, pixelwise,
3617 3620 FRAME_COLUMN_WIDTH (f));
3618 CHECK_TYPE_RANGED_INTEGER (int, width); 3621 int pixel_height = check_frame_pixels (height, pixelwise,
3619 CHECK_TYPE_RANGED_INTEGER (int, height); 3622 FRAME_LINE_HEIGHT (f));
3620
3621 pixel_width = (!NILP (pixelwise)
3622 ? XFIXNUM (width)
3623 : XFIXNUM (width) * FRAME_COLUMN_WIDTH (f));
3624 pixel_height = (!NILP (pixelwise)
3625 ? XFIXNUM (height)
3626 : XFIXNUM (height) * FRAME_LINE_HEIGHT (f));
3627 adjust_frame_size (f, pixel_width, pixel_height, 1, 0, Qsize); 3623 adjust_frame_size (f, pixel_width, pixel_height, 1, 0, Qsize);
3628
3629 return Qnil; 3624 return Qnil;
3630} 3625}
3631 3626
@@ -3655,18 +3650,14 @@ bottom edge of FRAME's display. */)
3655 (Lisp_Object frame, Lisp_Object x, Lisp_Object y) 3650 (Lisp_Object frame, Lisp_Object x, Lisp_Object y)
3656{ 3651{
3657 struct frame *f = decode_live_frame (frame); 3652 struct frame *f = decode_live_frame (frame);
3658 3653 int xval = check_integer_range (x, INT_MIN, INT_MAX);
3659 CHECK_TYPE_RANGED_INTEGER (int, x); 3654 int yval = check_integer_range (y, INT_MIN, INT_MAX);
3660 CHECK_TYPE_RANGED_INTEGER (int, y);
3661 3655
3662 if (FRAME_WINDOW_P (f)) 3656 if (FRAME_WINDOW_P (f))
3663 { 3657 {
3664#ifdef HAVE_WINDOW_SYSTEM 3658#ifdef HAVE_WINDOW_SYSTEM
3665 if (FRAME_TERMINAL (f)->set_frame_offset_hook) 3659 if (FRAME_TERMINAL (f)->set_frame_offset_hook)
3666 FRAME_TERMINAL (f)->set_frame_offset_hook (f, 3660 FRAME_TERMINAL (f)->set_frame_offset_hook (f, xval, yval, 1);
3667 XFIXNUM (x),
3668 XFIXNUM (y),
3669 1);
3670#endif 3661#endif
3671 } 3662 }
3672 3663
@@ -4641,23 +4632,22 @@ gui_set_right_fringe (struct frame *f, Lisp_Object new_value, Lisp_Object old_va
4641void 4632void
4642gui_set_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval) 4633gui_set_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
4643{ 4634{
4644 CHECK_TYPE_RANGED_INTEGER (int, arg); 4635 int border_width = check_integer_range (arg, INT_MIN, INT_MAX);
4645 4636
4646 if (XFIXNUM (arg) == f->border_width) 4637 if (border_width == f->border_width)
4647 return; 4638 return;
4648 4639
4649 if (FRAME_NATIVE_WINDOW (f) != 0) 4640 if (FRAME_NATIVE_WINDOW (f) != 0)
4650 error ("Cannot change the border width of a frame"); 4641 error ("Cannot change the border width of a frame");
4651 4642
4652 f->border_width = XFIXNUM (arg); 4643 f->border_width = border_width;
4653} 4644}
4654 4645
4655void 4646void
4656gui_set_right_divider_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval) 4647gui_set_right_divider_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
4657{ 4648{
4658 int old = FRAME_RIGHT_DIVIDER_WIDTH (f); 4649 int old = FRAME_RIGHT_DIVIDER_WIDTH (f);
4659 CHECK_TYPE_RANGED_INTEGER (int, arg); 4650 int new = check_int_nonnegative (arg);
4660 int new = max (0, XFIXNUM (arg));
4661 if (new != old) 4651 if (new != old)
4662 { 4652 {
4663 f->right_divider_width = new; 4653 f->right_divider_width = new;
@@ -4671,8 +4661,7 @@ void
4671gui_set_bottom_divider_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval) 4661gui_set_bottom_divider_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
4672{ 4662{
4673 int old = FRAME_BOTTOM_DIVIDER_WIDTH (f); 4663 int old = FRAME_BOTTOM_DIVIDER_WIDTH (f);
4674 CHECK_TYPE_RANGED_INTEGER (int, arg); 4664 int new = check_int_nonnegative (arg);
4675 int new = max (0, XFIXNUM (arg));
4676 if (new != old) 4665 if (new != old)
4677 { 4666 {
4678 f->bottom_divider_width = new; 4667 f->bottom_divider_width = new;
@@ -5651,8 +5640,7 @@ gui_figure_window_size (struct frame *f, Lisp_Object parms, bool tabbar_p,
5651 f->top_pos = 0; 5640 f->top_pos = 0;
5652 else 5641 else
5653 { 5642 {
5654 CHECK_TYPE_RANGED_INTEGER (int, top); 5643 f->top_pos = check_integer_range (top, INT_MIN, INT_MAX);
5655 f->top_pos = XFIXNUM (top);
5656 if (f->top_pos < 0) 5644 if (f->top_pos < 0)
5657 window_prompting |= YNegative; 5645 window_prompting |= YNegative;
5658 } 5646 }
@@ -5682,8 +5670,7 @@ gui_figure_window_size (struct frame *f, Lisp_Object parms, bool tabbar_p,
5682 f->left_pos = 0; 5670 f->left_pos = 0;
5683 else 5671 else
5684 { 5672 {
5685 CHECK_TYPE_RANGED_INTEGER (int, left); 5673 f->left_pos = check_integer_range (left, INT_MIN, INT_MAX);
5686 f->left_pos = XFIXNUM (left);
5687 if (f->left_pos < 0) 5674 if (f->left_pos < 0)
5688 window_prompting |= XNegative; 5675 window_prompting |= XNegative;
5689 } 5676 }