Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
short isCode = 0;
char *text;
char *p;
/* Optimization: Use stack buffer for status text to avoid malloc overhead */
char buf[1024];
int use_malloc = 0;
Comment on lines +59 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider aligning types and limits when comparing len to sizeof(buf).

Here sizeof(buf) is size_t while len is an int, which can trigger signed/unsigned comparison warnings and obscure overflow/limit issues. Please make len a size_t (matching strlen and sizeof(buf)), and then you can simply use malloc(len) instead of sizeof(char) * len.

Suggested implementation:

    short isCode = 0;
    char *text;
    char *p;
    /* Optimization: Use stack buffer for status text to avoid malloc overhead */
    char buf[1024];
    int use_malloc = 0;
    size_t len;

    len = strlen(stext) + 1;
    if (len > sizeof(buf)) {
        if (!(text = (char *)malloc(len))) {
            die("malloc");
        }
        use_malloc = 1;
    } else {
        text = buf;
    }

If len is declared elsewhere in this function or file (e.g., as int len;), that declaration should be updated to size_t len; and the duplicate declaration here removed to avoid redefinition errors.


len = strlen(stext) + 1;
if (!(text = (char *)malloc(sizeof(char) * len))) {
die("malloc");
if (len > sizeof(buf)) {
if (!(text = (char *)malloc(sizeof(char) * len))) {
die("malloc");
}
use_malloc = 1;
} else {
text = buf;
}
p = text;
memcpy(text, stext, len);
Expand Down Expand Up @@ -194,7 +202,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
}

drw_setscheme(drw, statusscheme);
free(p);
if (use_malloc) {
free(p);
}

return ret;
}
Expand Down