commit 27b35128a5565d89a4c9caf7b305d831bd772d90
Author: NRK <[email protected]>
Date:   Tue Aug 23 02:50:20 2022 +0600

    [dmenu][separator]: rebase + fix invalid free()
    
    when `separator_reverse` is active `items.text_output` and `items.text`
    will be swapped leading to trying to free a pointer that wasn't
    allocated.
    
    this was not a problem before b43ec05 since `items` were not being
    free-ed at cleanup() before that commit.
    
    also rm `separator_greedy` and use a function pointer instead.

diff --git 
a/tools.suckless.org/dmenu/patches/separator/dmenu-separator-20210904-d78ff08.diff
 b/tools.suckless.org/dmenu/patches/separator/dmenu-separator-e35976f.diff
similarity index 74%
rename from 
tools.suckless.org/dmenu/patches/separator/dmenu-separator-20210904-d78ff08.diff
rename to 
tools.suckless.org/dmenu/patches/separator/dmenu-separator-e35976f.diff
index be304204..bca59ef0 100644
--- 
a/tools.suckless.org/dmenu/patches/separator/dmenu-separator-20210904-d78ff08.diff
+++ b/tools.suckless.org/dmenu/patches/separator/dmenu-separator-e35976f.diff
@@ -29,10 +29,10 @@ index 323f93c..d511148 100644
  dmenu is completely controlled by the keyboard.  Items are selected using the
  arrow keys, page up, page down, home, and end.
 diff --git a/dmenu.c b/dmenu.c
-index 98507d9..82227c8 100644
+index 571bc35..f2add3b 100644
 --- a/dmenu.c
 +++ b/dmenu.c
-@@ -30,12 +30,16 @@ enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* 
color schemes */
+@@ -30,12 +30,15 @@ enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* 
color schemes */
  
  struct item {
        char *text;
@@ -43,13 +43,21 @@ index 98507d9..82227c8 100644
  
  static char text[BUFSIZ] = "";
  static char *embed;
-+static char separator;
-+static int separator_greedy;
-+static int separator_reverse;
++static char separator, separator_reverse;
++static char * (*sepchr)(const char *, int);
  static int bh, mw, mh;
  static int inputw = 0, promptw;
  static int lrpad; /* sum of left and right padding */
-@@ -473,7 +477,7 @@ insert:
+@@ -105,7 +108,7 @@ cleanup(void)
+       for (i = 0; i < SchemeLast; i++)
+               free(scheme[i]);
+       for (i = 0; items && items[i].text; ++i)
+-              free(items[i].text);
++              free(separator_reverse ? items[i].text_output : items[i].text);
+       free(items);
+       drw_free(drw);
+       XSync(dpy, False);
+@@ -490,7 +493,7 @@ insert:
                break;
        case XK_Return:
        case XK_KP_Enter:
@@ -58,12 +66,11 @@ index 98507d9..82227c8 100644
                if (!(ev->state & ControlMask)) {
                        cleanup();
                        exit(0);
-@@ -545,6 +549,18 @@ readstdin(void)
+@@ -561,6 +564,17 @@ readstdin(void)
                        *p = '                  if (!(items[i].text = 
strdup(buf)))
-                       die("cannot strdup %u bytes:", strlen(buf) + 1);
-+              if (separator && (p = separator_greedy ?
-+                  strrchr(items[i].text, separator) : strchr(items[i].text, 
separator))) {
+                       die("cannot strdup %zu bytes:", strlen(buf) + 1);
++              if (separator && (p = sepchr(items[i].text, separator)) != 
NULL) {
 +                      *p = ' +                        items[i].text_output = 
++p;
 +              } else {
@@ -75,9 +82,9 @@ index 98507d9..82227c8 100644
 +                      items[i].text_output = p;
 +              }
                items[i].out = 0;
-               drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
-               if (tmpmax > inputw) {
-@@ -701,7 +717,8 @@ static void
+       }
+       if (items)
+@@ -711,7 +725,8 @@ static void
  usage(void)
  {
        fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m 
monitor]
"
@@ -87,12 +94,13 @@ index 98507d9..82227c8 100644
        exit(1);
  }
  
-@@ -744,6 +761,11 @@ main(int argc, char *argv[])
+@@ -754,6 +769,12 @@ main(int argc, char *argv[])
                        colors[SchemeSel][ColFg] = argv[++i];
                else if (!strcmp(argv[i], "-w"))   /* embedding window id */
                        embed = argv[++i];
 +              else if (!strcmp(argv[i], "-d") || /* field separator */
-+                       (separator_greedy = !strcmp(argv[i], "-D"))) {
++                       !strcmp(argv[i], "-D")) {
++                      sepchr = argv[i][1] == 'D' ? strrchr : strchr;
 +                      separator = argv[++i][0];
 +                      separator_reverse = argv[i][1] == '|';
 +              }
diff --git a/tools.suckless.org/dmenu/patches/separator/index.md 
b/tools.suckless.org/dmenu/patches/separator/index.md
index d2f26cb6..5d47e802 100644
--- a/tools.suckless.org/dmenu/patches/separator/index.md
+++ b/tools.suckless.org/dmenu/patches/separator/index.md
@@ -8,9 +8,7 @@ The following example will split the input into two halves on 
the _first_
 occurrence of ' ' (space).  Meaning "alpha" will be displayed on dmenu, and
 "beta charlie" will be printed to stdout upon selection.
 
-`
-echo "alpha beta charlie" | dmenu -d ' '
-`
+       echo "alpha beta charlie" | dmenu -d ' '
 
 `-D` is similar but it separates the input based on the _last_ occurrence 
instead.
 
@@ -20,7 +18,8 @@ and display the latter half on dmenu while printing the first 
half upon selectio
 
 Download
 --------
-* 
[dmenu-separator-20210904-d78ff08.diff](dmenu-separator-20210904-d78ff08.diff)
+
+* [dmenu-separator-e35976f.diff](dmenu-separator-e35976f.diff)
 
 Author
 ------


Reply via email to