util: file: Shrink buffer returned from 'saferead_lim' to actual size

The bufer resizing algorithm resizes the buffer in increments of the
value of the 'BUFSIZ' macro. On linux it's currently 8k. In case when
the caller wants to retain the buffer for long time with little data
read this creates massive overhead.

Realloc the buffer to actual size before returning it to the user.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Peter Krempa committed 2026-03-27 06:53:07 +01:00
1 parent 74b24ffc24
commit 458c6a2810
1 file changed
+1
+1
View File
@@ -1523,6 +1523,7 @@ saferead_lim(int fd, size_t max_len, size_t *length)
break;
buf[size] = '\0';
*length = size;
VIR_REALLOC_N(buf, size + 1);
return buf;
}
}