[PATCH] libvirt_nss: Fix ERROR() macro

Michal Privoznik posted 1 patch 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/9b33d4276cf21d5cfc14686d144c73627b62ac63.1713438048.git.mprivozn@redhat.com
tools/nss/libvirt_nss.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] libvirt_nss: Fix ERROR() macro
Posted by Michal Privoznik 2 weeks ago
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>
---
 tools/nss/libvirt_nss.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/nss/libvirt_nss.h b/tools/nss/libvirt_nss.h
index 2bb313f329..5f356618f3 100644
--- a/tools/nss/libvirt_nss.h
+++ b/tools/nss/libvirt_nss.h
@@ -37,11 +37,11 @@
 # define NULLSTR(s) ((s) ? (s) : "<null>")
 # define ERROR(...) \
 do { \
-    char ebuf[1024]; \
-    strerror_r(errno, ebuf, sizeof(ebuf)); \
+    char ebuf[512]; \
+    const char *errmsg = strerror_r(errno, ebuf, sizeof(ebuf)); \
     fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \
     fprintf(stderr, __VA_ARGS__); \
-    fprintf(stderr, " : %s\n", ebuf); \
+    fprintf(stderr, " : %s\n", errmsg); \
     fprintf(stderr, "\n"); \
 } while (0)
 
-- 
2.43.2
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [PATCH] libvirt_nss: Fix ERROR() macro
Posted by Peter Krempa 2 weeks ago
On Thu, Apr 18, 2024 at 13:00:48 +0200, Michal Privoznik wrote:
> 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>
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org