script: fix buffer resize bug

If the amount of data coming in is more than we can hold in our
buffer, we resized the buffer by doubling its size. However, there
were two(!) issues here:

* If this was the first resize, the buffer size was set to 1024. This
  may not be enough (i.e. there may be more than 1024 bytes to process).
* In all other cases, the buffer size was doubled. However, there is
  still no guarantee the buffer is large enough.

Fix by looping until the buffer *is* large enough.
This commit is contained in:
Daniel Eklöf 2024-02-05 12:52:40 +01:00
parent aeeef4f236
commit f2d25c8341
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 2 additions and 1 deletions

View file

@ -40,6 +40,7 @@
([#330][330]).
* i3/sway: incorrect empty/title state of workspaces ([#343][343]).
* mem: state updated on each bar redraw ([#352][352]).
* script: buffer overflow when reading large amounts of data.
[311]: https://codeberg.org/dnkl/yambar/issues/311
[302]: https://codeberg.org/dnkl/yambar/issues/302

View file

@ -298,7 +298,7 @@ data_received(struct module *mod, const char *data, size_t len)
{
struct private *m = mod->private;
if (len > m->recv_buf.sz - m->recv_buf.idx) {
while (len > m->recv_buf.sz - m->recv_buf.idx) {
size_t new_sz = m->recv_buf.sz == 0 ? 1024 : m->recv_buf.sz * 2;
char *new_buf = realloc(m->recv_buf.data, new_sz);