diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..1d04f7e --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-02-05 - Masked Global Variables in C +**Learning:** The global variable `stext` is masked by a function argument of the same name in `drawstatusbar(..., char *stext)`. This requires careful inspection of variable scope when optimizing, as `sizeof(stext)` yields different results (pointer size vs array size) depending on the scope. +**Action:** Always verify variable declarations and scope before applying size-based optimizations or `sizeof` checks in C codebases. diff --git a/bar.c b/bar.c index 8476144..95ba42b 100644 --- a/bar.c +++ b/bar.c @@ -56,8 +56,12 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { char *text; char *p; + char buf[1024]; + len = strlen(stext) + 1; - if (!(text = (char *)malloc(sizeof(char) * len))) { + if (len < sizeof(buf)) { + text = buf; + } else if (!(text = (char *)malloc(sizeof(char) * len))) { die("malloc"); } p = text; @@ -194,7 +198,9 @@ int drawstatusbar(Monitor *m, int bh, char *stext) { } drw_setscheme(drw, statusscheme); - free(p); + if (p != buf) { + free(p); + } return ret; }