]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: choose whether to require micro in version strings
authorEric Blake <eblake@redhat.com>
Fri, 1 Jul 2011 13:23:02 +0000 (07:23 -0600)
committerEric Blake <eblake@redhat.com>
Fri, 1 Jul 2011 17:22:11 +0000 (11:22 -0600)
To avoid regressions, we let callers specify whether to require a
minor and micro version.  Callers that were parsing uname() output
benefit from defaulting to 0, whereas callers that were parsing
version strings from other sources should not change in behavior.

* src/util/util.c (virParseVersionString): Allow caller to choose
whether to fail if minor or micro is missing.
* src/util/util.h (virParseVersionString): Update signature.
* src/esx/esx_driver.c (esxGetVersion): Update callers.
* src/lxc/lxc_driver.c (lxcVersion): Likewise.
* src/openvz/openvz_conf.c (openvzExtractVersionInfo): Likewise.
* src/uml/uml_driver.c (umlGetVersion): Likewise.
* src/vbox/vbox_MSCOMGlue.c (vboxLookupVersionInRegistry):
Likewise.
* src/vbox/vbox_tmpl.c (vboxExtractVersion): Likewise.
* src/vmware/vmware_conf.c (vmwareExtractVersion): Likewise.
* src/xenapi/xenapi_driver.c (xenapiGetVersion): Likewise.
Reported by Matthias Bolte.

src/esx/esx_driver.c
src/lxc/lxc_driver.c
src/openvz/openvz_conf.c
src/uml/uml_driver.c
src/util/util.c
src/util/util.h
src/vbox/vbox_MSCOMGlue.c
src/vbox/vbox_tmpl.c
src/vmware/vmware_conf.c
src/xenapi/xenapi_driver.c

index 3a140e96cfd00bbf640d4fb328c18a891d34a5f8..a6e47a40a90d5c3e448635e01f0b3040ed34600a 100644 (file)
@@ -1202,7 +1202,7 @@ esxGetVersion(virConnectPtr conn, unsigned long *version)
     esxPrivate *priv = conn->privateData;
 
     if (virParseVersionString(priv->primary->service->about->version,
-                              version) < 0) {
+                              version, false) < 0) {
         ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                   _("Could not parse version number from '%s'"),
                   priv->primary->service->about->version);
index d0f715860d58a6f7c82c9bc6a36d7d13c895f007..7220a9b64e6b273d6e4bd77fe8c5f93fd2960de4 100644 (file)
@@ -2249,7 +2249,7 @@ static int lxcVersion(virConnectPtr conn ATTRIBUTE_UNUSED, unsigned long *versio
 
     uname(&ver);
 
-    if (virParseVersionString(ver.release, version) < 0) {
+    if (virParseVersionString(ver.release, version, true) < 0) {
         lxcError(VIR_ERR_INTERNAL_ERROR, _("Unknown release: %s"), ver.release);
         return -1;
     }
index 189279dbe5e4d29b6449e7f33989e8d67efc4137..561cc990d0df1360766a741a2598552f8584b78e 100644 (file)
@@ -99,7 +99,7 @@ openvzExtractVersionInfo(const char *cmdstr, int *retversion)
     if ((tmp = STRSKIP(tmp, "vzctl version ")) == NULL)
         goto cleanup;
 
-    if (virParseVersionString(tmp, &version) < 0)
+    if (virParseVersionString(tmp, &version, false) < 0)
         goto cleanup;
 
     if (retversion)
index e557fe89c211f188a6108ebc58301de67e6828d8..a71ea2174c87b499fb94d3300566bc0788c24a3c 100644 (file)
@@ -1227,7 +1227,7 @@ static int umlGetVersion(virConnectPtr conn, unsigned long *version) {
     if (driver->umlVersion == 0) {
         uname(&ut);
 
-        if (virParseVersionString(ut.release, &driver->umlVersion) < 0) {
+        if (virParseVersionString(ut.release, &driver->umlVersion, true) < 0) {
             umlReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot parse version %s"), ut.release);
             goto cleanup;
index da2485998a4f0c0cc9b8bc0eae4d904dbb8ed7e1..13973c359dadfbcb3c3d12deafdb8ac785c92761 100644 (file)
@@ -1585,6 +1585,8 @@ virParseNumber(const char **str)
  * virParseVersionString:
  * @str: const char pointer to the version string
  * @version: unsigned long pointer to output the version number
+ * @allowMissing: true to treat 3 like 3.0.0, false to error out on
+ * missing minor or micro
  *
  * Parse an unsigned version number from a version string. Expecting
  * 'major.minor.micro' format, ignoring an optional suffix.
@@ -1596,7 +1598,8 @@ virParseNumber(const char **str)
  * Returns the 0 for success, -1 for error.
  */
 int
-virParseVersionString(const char *str, unsigned long *version)
+virParseVersionString(const char *str, unsigned long *version,
+                      bool allowMissing)
 {
     unsigned int major, minor = 0, micro = 0;
     char *tmp;
@@ -1604,12 +1607,21 @@ virParseVersionString(const char *str, unsigned long *version)
     if (virStrToLong_ui(str, &tmp, 10, &major) < 0)
         return -1;
 
+    if (!allowMissing && *tmp != '.')
+        return -1;
+
     if ((*tmp == '.') && virStrToLong_ui(tmp + 1, &tmp, 10, &minor) < 0)
         return -1;
 
+    if (!allowMissing && *tmp != '.')
+        return -1;
+
     if ((*tmp == '.') && virStrToLong_ui(tmp + 1, &tmp, 10, &micro) < 0)
         return -1;
 
+    if (major > UINT_MAX / 1000000 || minor > 999 || micro > 999)
+        return -1;
+
     *version = 1000000 * major + 1000 * minor + micro;
 
     return 0;
index 0c43f7a77fccb631e70e3986a863099697b2d237..155565364f2e6a2020441d3b72973cd5fad42229 100644 (file)
@@ -168,7 +168,8 @@ int virMacAddrCompare (const char *mac1, const char *mac2);
 
 void virSkipSpaces(const char **str);
 int virParseNumber(const char **str);
-int virParseVersionString(const char *str, unsigned long *version);
+int virParseVersionString(const char *str, unsigned long *version,
+                          bool allowMissing);
 int virAsprintf(char **strp, const char *fmt, ...)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3);
 int virVasprintf(char **strp, const char *fmt, va_list list)
index 8aef2660cf6df00b2af409a937f0c4ff6cb0283d..31e14a14c154f980d7718d5083b72fbe1965999c 100644 (file)
@@ -430,7 +430,7 @@ vboxLookupVersionInRegistry(void)
         }
     }
 
-    if (virParseVersionString(value, &vboxVersion) < 0) {
+    if (virParseVersionString(value, &vboxVersion, false) < 0) {
         VIR_ERROR(_("Could not parse version number from '%s'"), value);
         goto cleanup;
     }
index 7fd120078c1c81c3f60aec5b9b63496fb1f57fef..37fe248f3f1f7adc5ff13a07a38d2f1a84539d8e 100644 (file)
@@ -929,7 +929,7 @@ static int vboxExtractVersion(vboxGlobalData *data) {
 
         VBOX_UTF16_TO_UTF8(versionUtf16, &vboxVersion);
 
-        if (virParseVersionString(vboxVersion, &data->version) >= 0)
+        if (virParseVersionString(vboxVersion, &data->version, false) >= 0)
             ret = 0;
 
         VBOX_UTF8_FREE(vboxVersion);
index 4ec33aebde122a99d3668804fcc33ccda38c8dec..044784ef608559342c2b5c87b4c653f6fb6e3579 100644 (file)
@@ -248,7 +248,7 @@ vmwareExtractVersion(struct vmware_driver *driver)
         goto cleanup;
     }
 
-    if (virParseVersionString(tmp, &version) < 0) {
+    if (virParseVersionString(tmp, &version, false) < 0) {
         vmwareError(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("version parsing error"));
         goto cleanup;
index 77d17ee960b8dc87cc2ed2fb30a971fbfcd928b6..0a618ab3b160bb942260f966e24e430b2520352b 100644 (file)
@@ -302,7 +302,7 @@ xenapiGetVersion (virConnectPtr conn, unsigned long *hvVer)
             }
         }
         if (version) {
-            if (virParseVersionString(version, hvVer) < 0)
+            if (virParseVersionString(version, hvVer, false) < 0)
                 xenapiSessionErrorHandler(conn, VIR_ERR_INTERNAL_ERROR,
                                           _("Couldn't parse version info"));
             else