[dev][dmenu] loadfont() segmentation fault patch

From: lolilolicon <lolilolicon_AT_gmail.com>
Date: Fri, 22 Jul 2011 21:14:28 +0800

To reproduce the segmentation fault:

dmenu -fn "-windows-montecarlo-medium-r-normal--11-110-72-72-c-60-microsoft-cp1252"
<<< hello

The culprit is loadfont(). This font[1] triggers XLoadQueryFont(), and
loadfont()
fails in the for loop. At least that's what my quick debug indicated.

I did a diff between dmenu 4.3.1 and 4.4.
Do we really want this twisted pretentious piece of code inside loadfont()?
I for one like the 4.3.1 code better. And it worked.

Patch attached inline. I do it the humble way.

[1] The font is MonteCarlo:
http://bok.net/MonteCarlo/downloads/MonteCarlo-PCF.tgz

--- dmenu-4.4-a/draw.c 2011-07-19 20:31:28.000000000 +0000
+++ dmenu-4.4-b/draw.c 2011-07-22 12:34:35.026736893 +0000
@@ -121,23 +121,25 @@ initfont(DC *dc, const char *fontstr) {
 Bool
 loadfont(DC *dc, const char *fontstr) {
         char *def, **missing, **names;
- int i, n = 1;
+ int i, n;
         XFontStruct **xfonts;

         if(!*fontstr)
                 return False;
- if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def)))
+ if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
                 n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
- else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr)))
- xfonts = &dc->font.xfont;
- else
- n = 0;
-
- for(i = 0; i < n; i++) {
- dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
- dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
- dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width);
+ for(i = 0; i < n; i++) {
+ dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
+ dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
+ dc->font.width = MAX(dc->font.width, xfonts[i]->max_bounds.width);
+ }
+ }
+ else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
+ dc->font.ascent = dc->font.xfont->ascent;
+ dc->font.descent = dc->font.xfont->descent;
+ dc->font.width = dc->font.xfont->max_bounds.width;
         }
+
         if(missing)
                 XFreeStringList(missing);
         return (dc->font.set || dc->font.xfont);
Received on Fri Jul 22 2011 - 15:14:28 CEST

This archive was generated by hypermail 2.2.0 : Fri Jul 22 2011 - 15:24:03 CEST