1
0
mirror of https://github.com/Cloudef/bemenu synced 2024-09-25 13:30:43 +02:00

bemenu: reuse the calculated buffer end

Also assert that we don't overflow on realloc
This commit is contained in:
Jari Vetoniemi 2020-02-11 10:23:33 +02:00
parent a89799a6b5
commit 467ac971d1

View File

@ -34,15 +34,16 @@ read_items_to_menu_from_stdin(struct bm_menu *menu)
// make sure we can null terminate the input
const size_t end = allocated - step + read;
assert(end != ~(size_t)0);
if (end >= allocated && !(buffer = realloc(buffer, end + 1))) {
fprintf(stderr, "Out of memory\n");
return;
}
buffer[allocated - step + read] = 0;
buffer[end] = 0;
char *s = buffer;
while ((size_t)(s - buffer) < allocated - step + read && *s != 0) {
while ((size_t)(s - buffer) < end && *s != 0) {
const size_t pos = strcspn(s, "\n");
s[pos] = 0;