libvirt_nss: Fix ERROR() macro

The purpose of ERROR() macro in our NSS module is to print error
message provided as arguments followed by error string
corresponding to errno. Historically, we've used strerror_r() for
that (please note, we want our NSS module to be free of libvirt
internal functions, or glib even - hence, g_strerror() is off the
table).

Now strerror_r() is documented as:

  Returns ... a pointer to a string that the function stores in
  buf, or a pointer to some (immutable) static string (in which
  case buf is unused).

Therefore, we can't rely the string being stored in the buf and
really need to store the retval and print that instead.

While touching this area, decrease the ebuf size, since its
current size (1KiB) is triggering our stack limit (2KiB) in some
cases.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Michal Privoznik 2024-04-18 09:25:07 +02:00
parent e314a8fe91
commit 753064963c

View File

@ -37,11 +37,11 @@
# define NULLSTR(s) ((s) ? (s) : "<null>") # define NULLSTR(s) ((s) ? (s) : "<null>")
# define ERROR(...) \ # define ERROR(...) \
do { \ do { \
char ebuf[1024]; \ char ebuf[512]; \
strerror_r(errno, ebuf, sizeof(ebuf)); \ const char *errmsg = strerror_r(errno, ebuf, sizeof(ebuf)); \
fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \ fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " : %s\n", ebuf); \ fprintf(stderr, " : %s\n", errmsg); \
fprintf(stderr, "\n"); \ fprintf(stderr, "\n"); \
} while (0) } while (0)