]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
libvirt_nss: Allocate buffer in ERROR() dynamically
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 21 May 2025 16:14:38 +0000 (18:14 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 22 May 2025 13:44:12 +0000 (15:44 +0200)
So far, inside of the ERROR() macro there's pretty large buffer
allocated on the stack (for use by strerror_r()). Problem is,
with our current stack size limit of 2048 bytes we may come
pretty close to the limit or even overshoot it, e.g. in aiforaf()
where the function itself declares another stack allocated buffer
1024 bytes long.

Just allocate the buffer dynamically.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tools/nss/libvirt_nss.h

index a43731de45aa4083351e2f7a595e4f17a998710d..84db0444094a3c5239a2c45f47d11134104622e8 100644 (file)
 #if 0
 # include <errno.h>
 # include <stdio.h>
+# include <string.h>
 # define NULLSTR(s) ((s) ? (s) : "<null>")
 # define ERROR(...) \
 do { \
-    char ebuf[512]; \
-    const char *errmsg = strerror_r(errno, ebuf, sizeof(ebuf)); \
+    int saved_errno = errno; \
+    const size_t ebuf_size = 512; \
+    g_autofree char *ebuf = calloc(ebuf_size, sizeof(*ebuf)); \
+    if (ebuf) \
+        strerror_r(saved_errno, ebuf, ebuf_size); \
     fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \
     fprintf(stderr, __VA_ARGS__); \
-    fprintf(stderr, " : %s\n", errmsg); \
+    fprintf(stderr, " : %s\n", NULLSTR(ebuf)); \
     fprintf(stderr, "\n"); \
 } while (0)