]> xenbits.xensource.com Git - libvirt.git/commitdiff
storage_conf: Use uid_t/gid_t instead of int to cast the value
authorOsier Yang <jyang@redhat.com>
Wed, 22 May 2013 12:05:22 +0000 (20:05 +0800)
committerOsier Yang <jyang@redhat.com>
Wed, 29 May 2013 10:19:19 +0000 (18:19 +0800)
And error out if the casted value is not same with the original
one, which prevents the bug on platform(s) where uid_t/gid_t
has different size with long.

src/conf/storage_conf.c

index b9215220eecc77921c45ef215e1db5763210d394..f0ea41dfda8c37a9ec68ca1d07b7c1dcb4469288 100644 (file)
@@ -44,6 +44,7 @@
 #include "viralloc.h"
 #include "virfile.h"
 #include "virstring.h"
+#include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -747,7 +748,7 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
                         int defaultmode)
 {
     char *mode;
-    long v;
+    long val;
     int ret = -1;
     xmlNodePtr relnode;
     xmlNodePtr node;
@@ -784,23 +785,28 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
     if (virXPathNode("./owner", ctxt) == NULL) {
         perms->uid = (uid_t) -1;
     } else {
-        if (virXPathLong("number(./owner)", ctxt, &v) < 0) {
+        if (virXPathLong("number(./owner)", ctxt, &val) < 0 ||
+            ((uid_t)val != val &&
+             val != -1)) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("malformed owner element"));
             goto error;
         }
-        perms->uid = (int)v;
+
+        perms->uid = val;
     }
 
     if (virXPathNode("./group", ctxt) == NULL) {
         perms->gid = (gid_t) -1;
     } else {
-        if (virXPathLong("number(./group)", ctxt, &v) < 0) {
+        if (virXPathLong("number(./group)", ctxt, &val) < 0 ||
+            ((gid_t) val != val &&
+             val != -1)) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("malformed group element"));
             goto error;
         }
-        perms->gid = (int)v;
+        perms->gid = val;
     }
 
     /* NB, we're ignoring missing labels here - they'll simply inherit */