Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-01-19 - [Stack Allocation in drawstatusbar]
**Learning:** `drawstatusbar` in `bar.c` uses `malloc` for a temporary string buffer on every redraw. This is a hot path. The source string `stext` is a global fixed-size buffer of 1024 bytes.
**Action:** Replace `malloc` with a stack buffer `char text[1024]` to avoid heap allocation overhead and potential fragmentation. This is safe because the input size is bounded by the global `stext` size.
12 changes: 6 additions & 6 deletions bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
int len;
int cmdcounter;
short isCode = 0;
char *text;
char *p;
char text_buf[1024];
char *text = text_buf;
char *p = text_buf;

len = strlen(stext) + 1;
if (!(text = (char *)malloc(sizeof(char) * len))) {
die("malloc");
if (len > sizeof(text_buf)) {
len = sizeof(text_buf);
}
p = text;
memcpy(text, stext, len);
text[sizeof(text_buf) - 1] = '\0';

/* compute width of the status text */
w = 0;
Expand Down Expand Up @@ -194,7 +195,6 @@ int drawstatusbar(Monitor *m, int bh, char *stext) {
}

drw_setscheme(drw, statusscheme);
free(p);

return ret;
}
Expand Down