Xinqi Bao's Git

fix possible overflow
[dmenu.git] / draw.c
diff --git a/draw.c b/draw.c
index f9b8957..351a43d 100644 (file)
--- a/draw.c
+++ b/draw.c
@@ -15,24 +15,24 @@ static Bool loadfont(DC *dc, const char *fontstr);
 
 void
 drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
 
 void
 drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
-       XRectangle r = { dc->x + x, dc->y + y, w, h };
+       XRectangle r;
+
+       r.x = dc->x + x;
+       r.y = dc->y + y;
+       r.width  = fill ? w : w-1;
+       r.height = fill ? h : h-1;
 
 
-       if(!fill) {
-               r.width -= 1;
-               r.height -= 1;
-       }
        XSetForeground(dc->dpy, dc->gc, color);
        (fill ? XFillRectangles : XDrawRectangles)(dc->dpy, dc->canvas, dc->gc, &r, 1);
 }
 
        XSetForeground(dc->dpy, dc->gc, color);
        (fill ? XFillRectangles : XDrawRectangles)(dc->dpy, dc->canvas, dc->gc, &r, 1);
 }
 
-
 void
 drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
 void
 drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
-       char buf[256];
+       char buf[BUFSIZ];
        size_t mn, n = strlen(text);
 
        /* shorten text if necessary */
        size_t mn, n = strlen(text);
 
        /* shorten text if necessary */
-       for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) > dc->w - dc->font.height/2; mn--)
+       for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) + dc->font.height/2 > dc->w; mn--)
                if(mn == 0)
                        return;
        memcpy(buf, text, mn);
                if(mn == 0)
                        return;
        memcpy(buf, text, mn);
@@ -157,12 +157,11 @@ void
 resizedc(DC *dc, unsigned int w, unsigned int h) {
        if(dc->canvas)
                XFreePixmap(dc->dpy, dc->canvas);
 resizedc(DC *dc, unsigned int w, unsigned int h) {
        if(dc->canvas)
                XFreePixmap(dc->dpy, dc->canvas);
+
        dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
                                   DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
        dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
                                   DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
-       dc->x = dc->y = 0;
        dc->w = w;
        dc->h = h;
        dc->w = w;
        dc->h = h;
-       dc->invert = False;
 }
 
 int
 }
 
 int