Commit: 42d31f4796d214139b77765c26e75ce9176b9cc0
Parent: 6f0913822ab45ff7f8ee3d8d0d462a57cf2416fa
Author: Randy Palamar
Date: Sun, 28 Mar 2021 01:58:13 -0600
use a simpler fix for buffer overflow
this doesn't introduce a VLA like I originally thought.
Diffstat:
3 files changed, 7 insertions(+), 16 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -3,8 +3,6 @@
#include "blocks/mpd.h"
#include "blocks/volume.h"
-#define STATUSLEN 1024
-
/* host for connecting to MPD, set to NULL for the MPD_HOST env variable */
const char *mpdhost = "localhost";
diff --git a/status.c b/status.c
@@ -10,6 +10,8 @@
#include "util.h"
#include "config.h"
+#define STATUSLEN ((LEN(blks) - 1) * BLOCKLEN + 1)
+
static int done = 0;
static int dflag = 0;
char buf[BLOCKLEN - BLOCKPAD];
@@ -40,25 +42,14 @@ updatestatus(void)
static char status[STATUSLEN];
struct Block *b;
char *s = status;
- ssize_t rem;
for (b = blks; b < dirty; b++)
s += b->len;
- rem = sizeof(status) - (s - status);
-
- for (; rem > 0 && b->fn; b++) {
- if (b->len < (size_t)rem) {
- memcpy(s, b->curstr, b->len);
- s += b->len;
- } else {
- memcpy(s, b->curstr, rem);
- s += rem;
- }
- rem = sizeof(status) - (s - status);
+ for (; b->fn; b++) {
+ memcpy(s, b->curstr, b->len);
+ s += b->len;
}
- if (rem < 0)
- s += rem;
s[0] = '\0';
dirty = NULL;
diff --git a/util.h b/util.h
@@ -1,3 +1,5 @@
+#define LEN(a) sizeof(a) / sizeof(*a)
+
void die(const char *errstr, ...);
size_t bprintf(char *buf, size_t buflen, const char *fmt, ...);
int pscanf(const char *path, const char *fmt, ...);