Re: [PATCH] Don't use enums in TPM struct fields

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

 



On Wed, Jun 06, 2018 at 08:32:39PM +0200, Ján Tomko wrote:
> This deprives us of the -Wswitch-enum warning on all compilers
> because some don't detect the bogus negative value comparison.
> And the comment has even less power than the clang warning. So:
> 
> 1. Is it actually worth the trouble to store enum values in
>   typedef'd enums?
> 2. If so, can we make TypeFromString usage less cumbersome?
> 
> The fact that the compiler can choose different types rules out
> returning the parsed value via a pointer.

Actually that is not a problem - the TypeToString methods are
autogenerated from a macro, so we can just make the macro
use the correct typename and adds a cast to deal with the signed
vs unsignedness of the value.

diff --git a/src/util/virutil.c b/src/util/virutil.c
index a908422feb..d8adc57931 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -444,15 +444,20 @@ virParseVersionString(const char *str, unsigned long *version,
 
 int virEnumFromString(const char *const*types,
                       unsigned int ntypes,
-                      const char *type)
+                      const char *type,
+                      int *val)
 {
     size_t i;
+    *val = 0;
     if (!type)
         return -1;
 
-    for (i = 0; i < ntypes; i++)
-        if (STREQ(types[i], type))
-            return i;
+    for (i = 0; i < ntypes; i++) {
+        if (STREQ(types[i], type)) {
+            *val = i;
+            return 0;
+        }
+    }
 
     return -1;
 }
diff --git a/src/util/virutil.h b/src/util/virutil.h
index 1ba9635bd9..7fea6c3a92 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -90,10 +90,11 @@ const char *virEnumToString(const char *const*types,
                                ARRAY_CARDINALITY(name ## TypeList), \
                                type); \
     } \
-    int name ## TypeFromString(const char *type) { \
+    int name ## TypeFromString(const char *type, name *val) {   \
         return virEnumFromString(name ## TypeList, \
                                  ARRAY_CARDINALITY(name ## TypeList), \
-                                 type); \
+                                 type,                                \
+                                 (int *)val);                         \
     }
 
 # define VIR_ENUM_DECL(name) \



Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
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]

  Powered by Linux