In commit 9911ecd985 ("make wtrimspace() use internal api") Tamas TEVESZ
pointed out that wtrimspace() used to segfault given null and fixed that.

I didn't check, but if the string had trailing whitespace it would also
segfault (and that was fixed in that commit too, although not explicitly
pointed out).

And currently there is a function cropline() -- duplicated in rootmenu.c
and workspace.c -- which looks like wtrimspace() used to be before Tamas'
fixes.

It segfaults if the string has trailing whitespace, see test program below.

So get rid of cropline() _twice_ and use WINGs wtrimspace() instead. The
fact that it can segfault is an extra reason to eliminate it (the segfault
is caused by the *end = 0; line). From a brief look at parseCascade() in
rootmenu.c, it looks like a menufile with a line containing trailing
whitespace could trigger a bug, but I haven't investigated it further.

Test program:

 #include <stdint.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>

static char *cropline(char *line)
{
        char *end;

        if (strlen(line) == 0)
                return line;

        end = &(line[strlen(line)]) - 1;
        while (isspace(*line) && *line != 0)
                line++;
        while (end > line && isspace(*end)) {
                *end = 0;
                end--;
        }
        return line;
}

int main(void)
{
        char *line = "  space before, space after  ";
        char *line_crop;

        printf("line_orig = %s\n", line);
        line_crop = cropline(line);
        printf("line_crop = %s\n", line_crop);

}

[mafra@Pilar:c]$ gcc -o cropline cropline.c
[mafra@Pilar:c]$ ./cropline
line_orig =   space before, space after
Speicherzugriffsfehler
[mafra@Pilar:c]$

Signed-off-by: Carlos R. Mafra <[email protected]>
---
 src/rootmenu.c  |   27 ++++-----------------------
 src/workspace.c |   19 +------------------
 2 files changed, 5 insertions(+), 41 deletions(-)

diff --git a/src/rootmenu.c b/src/rootmenu.c
index 0ecd3b4..cccbab8 100644
--- a/src/rootmenu.c
+++ b/src/rootmenu.c
@@ -473,25 +473,6 @@ static Bool addShortcut(char *file, char 
*shortcutDefinition, WMenu * menu, WMen
        return True;
 }
 
-/*******************************/
-
-static char *cropline(char *line)
-{
-       char *end;
-
-       if (strlen(line) == 0)
-               return line;
-
-       end = &(line[strlen(line)]) - 1;
-       while (isspace(*line) && *line != 0)
-               line++;
-       while (end > line && isspace(*end)) {
-               *end = 0;
-               end--;
-       }
-       return line;
-}
-
 static char *next_token(char *line, char **next)
 {
        char *tmp, c;
@@ -1020,14 +1001,14 @@ static WMenu *parseCascade(WScreen * scr, WMenu * menu, 
FILE * file, char *file_
 
                ok = 0;
                fgets(linebuf, MAXLINE, file);
-               line = cropline(linebuf);
+               line = wtrimspace(linebuf);
                lsize = strlen(line);
                do {
                        if (line[lsize - 1] == '\\') {
                                char *line2;
                                int lsize2;
                                fgets(elinebuf, MAXLINE, file);
-                               line2 = cropline(elinebuf);
+                               line2 = wtrimspace(elinebuf);
                                lsize2 = strlen(line2);
                                if (lsize2 + lsize > MAXLINE) {
                                        wwarning(_("%s:maximal line size 
exceeded in menu config: %s"),
@@ -1129,7 +1110,7 @@ static WMenu *readMenuFile(WScreen * scr, char *file_name)
        while (!feof(file)) {
                if (!fgets(linebuf, MAXLINE, file))
                        break;
-               line = cropline(linebuf);
+               line = wtrimspace(linebuf);
                if (line[0] == 0 || line[0] == '#' || (line[0] == '/' && 
line[1] == '/'))
                        continue;
 
@@ -1223,7 +1204,7 @@ static WMenu *readMenuPipe(WScreen * scr, char 
**file_name)
        while (!feof(file)) {
                if (!fgets(linebuf, MAXLINE, file))
                        break;
-               line = cropline(linebuf);
+               line = wtrimspace(linebuf);
                if (line[0] == 0 || line[0] == '#' || (line[0] == '/' && 
line[1] == '/'))
                        continue;
 
diff --git a/src/workspace.c b/src/workspace.c
index c0493f3..dc53d03 100644
--- a/src/workspace.c
+++ b/src/workspace.c
@@ -645,23 +645,6 @@ static void newWSCommand(WMenu * menu, WMenuEntry * foo)
           } */
 }
 
-static char *cropline(char *line)
-{
-       char *end;
-
-       if (strlen(line) == 0)
-               return line;
-
-       end = &(line[strlen(line)]) - 1;
-       while (isspace(*line) && *line != 0)
-               line++;
-       while (isspace(*end) && end != line) {
-               *end = 0;
-               end--;
-       }
-       return line;
-}
-
 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
 {
        char buf[MAX_WORKSPACENAME_WIDTH + 1];
@@ -671,7 +654,7 @@ void wWorkspaceRename(WScreen * scr, int workspace, char 
*name)
                return;
 
        /* trim white spaces */
-       tmp = cropline(name);
+       tmp = wtrimspace(name);
 
        if (strlen(tmp) == 0) {
                snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
-- 
1.7.3.4


-- 
To unsubscribe, send mail to [email protected].

Reply via email to