From 1279886b9c2bbfced259b446a531f11ae0125ce3 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Fri, 7 Apr 2023 14:15:31 +0200 Subject: [PATCH] Bump to ba1a347. readstdin: allocate amount of items Keep track of the amount of items (not a total buffer size), allocate an array of new items. For now change BUFSIZ bytes to 256 * sizeof(struct item)). Ref. https://git.suckless.org/dmenu/commit/ba1a347dcaba055f824161007dfee60db3ea785b.html --- dmenu.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dmenu.c b/dmenu.c index c6d3227..2fe996b 100644 --- a/dmenu.c +++ b/dmenu.c @@ -1428,8 +1428,7 @@ readstdin(void) char *buf, *p; #endif // SEPARATOR_PATCH | TSV_PATCH - size_t size = 0; - size_t i, junk; + size_t i, junk, itemsiz = 0; ssize_t len; #if PASSWORD_PATCH @@ -1441,9 +1440,11 @@ readstdin(void) /* read each line from stdin and add it to the item list */ for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++) { - if (i + 1 >= size / sizeof *items) - if (!(items = realloc(items, (size += BUFSIZ)))) - die("cannot realloc %zu bytes:", size); + if (i + 1 >= itemsiz) { + itemsiz += 256; + if (!(items = realloc(items, itemsiz * sizeof(*items)))) + die("cannot realloc %zu bytes:", itemsiz * sizeof(*items)); + } if (line[len - 1] == '\n') line[len - 1] = '\0';