]> xenbits.xensource.com Git - libvirt.git/commitdiff
src: Don't rely on virAsprintf() returning string length
authorMichal Privoznik <mprivozn@redhat.com>
Sat, 19 Oct 2019 10:09:32 +0000 (12:09 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 23 Oct 2019 12:56:51 +0000 (14:56 +0200)
In a few places our code relies on the fact that virAsprintf()
not only prints to allocated string but also that it returns the
length of that string. Fortunately, only few such places were
identified:

  https://www.redhat.com/archives/libvir-list/2019-September/msg01382.html

In case of virNWFilterSnoopLeaseFileWrite() and virFilePrintf()
we can use strlen() right after virAsprintf() to calculate the
length. In case of virDoubleToStr() it's only caller checks for
error case only, so we can limit the set of returned values to
just [-1, 0].

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/nwfilter/nwfilter_dhcpsnoop.c
src/util/virfile.c
src/util/virstring.c

index bf25345e2ca7f1214dab362489fa439ad2674193..665e7bff9c67563eed764eea83b13bdb1f58fa58 100644 (file)
@@ -1766,13 +1766,12 @@ virNWFilterSnoopLeaseFileWrite(int lfd, const char *ifkey,
     }
 
     /* time intf ip dhcpserver */
-    len = virAsprintf(&lbuf, "%u %s %s %s\n", ipl->timeout,
-                      ifkey, ipstr, dhcpstr);
-
-    if (len < 0) {
+    if (virAsprintf(&lbuf, "%u %s %s %s\n", ipl->timeout,
+                    ifkey, ipstr, dhcpstr) < 0) {
         ret = -1;
         goto cleanup;
     }
+    len = strlen(lbuf);
 
     if (safewrite(lfd, lbuf, len) != len) {
         virReportSystemError(errno, "%s", _("lease file write failed"));
index c4d544be73a7521c9ab28738579af6d2315e9308..7c973179949e7b882576c83a68edf6b323b428a0 100644 (file)
@@ -3394,12 +3394,13 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
 {
     va_list vargs;
     g_autofree char *str = NULL;
-    int ret;
+    int ret = -1;
 
     va_start(vargs, msg);
 
-    if ((ret = virVasprintf(&str, msg, vargs)) < 0)
+    if (virVasprintf(&str, msg, vargs) < 0)
         goto cleanup;
+    ret = strlen(str);
 
     if (fwrite(str, 1, ret, fp) != ret) {
         virReportSystemError(errno, "%s",
index 1494eab13298ba17d7dd6295e0336e5291a2a30d..52e36fe4da5b044fcc6248804be548f902737098 100644 (file)
@@ -703,23 +703,25 @@ virStrToDouble(char const *s,
  *
  * converts double to string with C locale (thread-safe).
  *
- * Returns -1 on error, size of the string otherwise.
+ * Returns: 0 on success, -1 otherwise.
  */
 int
 virDoubleToStr(char **strp, double number)
 {
     virLocale oldlocale;
-    int ret = -1;
+    int rc;
 
     if (virLocaleSetRaw(&oldlocale) < 0)
         return -1;
 
-    ret = virAsprintf(strp, "%lf", number);
+    rc = virAsprintf(strp, "%lf", number);
 
     virLocaleRevert(&oldlocale);
     virLocaleFixupRadix(strp);
 
-    return ret;
+    if (rc < 0)
+        return -1;
+    return 0;
 }