[hackers] [st] Fixing an out-of-bound bug in the selection code. Thanks Szabolczs Nagy! || Christoph Lohmann

From: <hg_AT_suckless.org>
Date: Sat, 3 Nov 2012 03:26:22 +0100 (CET)

changeset: 380:94dafc55f45b
tag: tip
user: Christoph Lohmann <20h_AT_r-36.net>
date: Sat Nov 03 03:24:22 2012 +0100
files: config.def.h st.c
description:
Fixing an out-of-bound bug in the selection code. Thanks Szabolczs Nagy!


diff -r 88ca50b8e7f7 -r 94dafc55f45b config.def.h
--- a/config.def.h Fri Nov 02 23:19:56 2012 +0100
+++ b/config.def.h Sat Nov 03 03:24:22 2012 +0100
_AT_@ -2,7 +2,7 @@
 
 /* appearance */
 static char font[] = "Liberation Mono:pixelsize=12:antialias=false:autohint=false";
-static unsigned int borderpx = 2;
+static int borderpx = 2;
 static char shell[] = "/bin/sh";
 
 /* double-click timeout (in milliseconds) between clicks for selection */
diff -r 88ca50b8e7f7 -r 94dafc55f45b st.c
--- a/st.c Fri Nov 02 23:19:56 2012 +0100
+++ b/st.c Sat Nov 03 03:24:22 2012 +0100
_AT_@ -72,8 +72,6 @@
 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
 #define IS_SET(flag) (term.mode & (flag))
 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
-#define X2COL(x) (((x) - borderpx)/xw.cw)
-#define Y2ROW(y) (((y) - borderpx)/xw.ch)
 
 #define VT102ID "\033[?6c"
 
_AT_@ -582,6 +580,22 @@
                 sel.xtarget = XA_STRING;
 }
 
+static int
+x2col(int x) {
+ x -= borderpx;
+ x /= xw.cw;
+
+ return LIMIT(x, 0, term.col-1);
+}
+
+static int
+y2row(int y) {
+ y -= borderpx;
+ y /= xw.ch;
+
+ return LIMIT(y, 0, term.row-1);
+}
+
 static inline bool
 selected(int x, int y) {
         int bx, ex;
_AT_@ -603,8 +617,9 @@
         if(b)
                 *b = e->xbutton.button;
 
- *x = X2COL(e->xbutton.x);
- *y = Y2ROW(e->xbutton.y);
+ *x = x2col(e->xbutton.x);
+ *y = y2row(e->xbutton.y);
+
         sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
         sel.b.y = MIN(sel.by, sel.ey);
         sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
_AT_@ -613,8 +628,8 @@
 
 void
 mousereport(XEvent *e) {
- int x = X2COL(e->xbutton.x);
- int y = Y2ROW(e->xbutton.y);
+ int x = x2col(e->xbutton.x);
+ int y = y2row(e->xbutton.y);
         int button = e->xbutton.button;
         int state = e->xbutton.state;
         char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
_AT_@ -656,8 +671,8 @@
                         draw();
                 }
                 sel.mode = 1;
- sel.ex = sel.bx = X2COL(e->xbutton.x);
- sel.ey = sel.by = Y2ROW(e->xbutton.y);
+ sel.ex = sel.bx = x2col(e->xbutton.x);
+ sel.ey = sel.by = y2row(e->xbutton.y);
         }
 }
 
Received on Sat Nov 03 2012 - 03:26:22 CET

This archive was generated by hypermail 2.3.0 : Sat Nov 03 2012 - 03:36:06 CET