[PATCH v2 1/5] introduce virSnprintf helper

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virstring.c     | 25 +++++++++++++++++++++++++
 src/util/virstring.h     | 20 ++++++++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index fdf4548..80d8e7d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1956,6 +1956,7 @@ virAsprintfInternal;
 virSkipSpaces;
 virSkipSpacesAndBackslash;
 virSkipSpacesBackwards;
+virSnprintfInternal;
 virStrcpy;
 virStrdup;
 virStringArrayHasString;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index b14f785..2a657b3 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -483,6 +483,31 @@ virAsprintfInternal(bool report,
     return ret;
 }
 
+int
+virSnprintfInternal(int domcode,
+                    const char *filename,
+                    const char *funcname,
+                    size_t linenr,
+                    char *strp,
+                    size_t strLen,
+                    const char *fmt, ...)
+{
+    va_list ap;
+    int ret;
+
+    va_start(ap, fmt);
+    ret = vsnprintf(strp, strLen, fmt, ap);
+
+    if (ret >= strLen)
+        virReportErrorHelper(domcode, VIR_ERR_INTERNAL_ERROR,
+                             filename, funcname, linenr,
+                             _("Size of src string '%d' is greater than available size of dst string '%zu'"),
+                             ret, strLen);
+    va_end(ap);
+    return ret;
+
+}
+
 /**
  * virStrncpy
  *
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 267fbd0..c526c12 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -129,6 +129,11 @@ int virVasprintfInternal(bool report, int domcode, const char *filename,
                          const char *fmt, va_list list)
     ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 0)
     ATTRIBUTE_RETURN_CHECK;
+int virSnprintfInternal(int domcode, const char *filename, const char *funcname,
+                        size_t linenr, char *strp, size_t strLen,
+                        const char *fmt, ...)
+    ATTRIBUTE_NONNULL(5) ATTRIBUTE_FMT_PRINTF(7, 8)
+    ATTRIBUTE_RETURN_CHECK;
 
 /**
  * VIR_STRDUP:
@@ -252,6 +257,21 @@ size_t virStringListLength(char **strings);
     virAsprintfInternal(false, 0, NULL, NULL, 0, \
                         strp, __VA_ARGS__)
 
+/**
+ * virSnprintf:
+ * @strp: variable to hold result (char*)
+ * @fmt: printf format
+ *
+ * Like glibc's_snprintf but report error if the src string is longer
+ * then dst string.
+ *
+ * Returns -1 on failure, number of bytes printed on success.
+ */
+
+# define virSnprintf(strp, ...) \
+    virSnprintfInternal(VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__, \
+                        strp, ARRAY_CARDINALITY(strp), __VA_ARGS__)
+
 int virStringSortCompare(const void *a, const void *b);
 int virStringSortRevCompare(const void *a, const void *b);
 
-- 
1.8.5.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]