Skip to content

Commit ba1a347

Browse files
committed
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)).
1 parent bcbc1ef commit ba1a347

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

dmenu.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,16 @@ static void
550550
readstdin(void)
551551
{
552552
char *line = NULL;
553-
size_t i, junk, size = 0;
553+
size_t i, junk, itemsiz = 0;
554554
ssize_t len;
555555

556556
/* read each line from stdin and add it to the item list */
557557
for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++) {
558-
if (i + 1 >= size / sizeof *items)
559-
if (!(items = realloc(items, (size += BUFSIZ))))
560-
die("cannot realloc %zu bytes:", size);
558+
if (i + 1 >= itemsiz) {
559+
itemsiz += 256;
560+
if (!(items = realloc(items, itemsiz * sizeof(*items))))
561+
die("cannot realloc %zu bytes:", itemsiz * sizeof(*items));
562+
}
561563
if (line[len - 1] == '\n')
562564
line[len - 1] = '\0';
563565
items[i].text = line;

0 commit comments

Comments
 (0)