]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
viruuid: Rework virUUIDIsValid()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 24 Nov 2020 10:12:27 +0000 (11:12 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 4 Dec 2020 15:24:19 +0000 (16:24 +0100)
The only test we do when checking for UUID validity is that
whether all bytes are the same (invalid UUID) or not (valid
UUID). The algorithm we use is needlessly complicated.

Also, the checked UUID is not modified and hence the argument can
be of 'const' type.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Han Han <hhan@redhat.com>
src/util/viruuid.c
src/util/viruuid.h

index 908b09945d32bc9a1d4288d7e6e7a7e6fd80d801..558fbb9c0d9fee7ab3b51853965ae108ee4ccba6 100644 (file)
@@ -170,25 +170,22 @@ virUUIDFormat(const unsigned char *uuid, char *uuidstr)
  * Basic tests:
  *  - Not all of the digits may be equal
  */
-int
-virUUIDIsValid(unsigned char *uuid)
+bool
+virUUIDIsValid(const unsigned char *uuid)
 {
     size_t i;
-    unsigned int ctr = 1;
-    unsigned char c;
 
     if (!uuid)
-        return 0;
-
-    c = uuid[0];
+        return false;
 
     for (i = 1; i < VIR_UUID_BUFLEN; i++)
-        if (uuid[i] == c)
-            ctr++;
+        if (uuid[i] != uuid[0])
+            return true;
 
-    return ctr != VIR_UUID_BUFLEN;
+    return false;
 }
 
+
 static int
 getDMISystemUUID(char *uuid, int len)
 {
index 5d64e5840552b5ad418fa713921a3b734b0ee06c..b403b1906a2d23a5a1bcf931a94a8e095fee3e79 100644 (file)
@@ -43,7 +43,7 @@
 int virSetHostUUIDStr(const char *host_uuid);
 int virGetHostUUID(unsigned char *host_uuid) ATTRIBUTE_NONNULL(1) G_GNUC_NO_INLINE;
 
-int virUUIDIsValid(unsigned char *uuid);
+bool virUUIDIsValid(const unsigned char *uuid);
 
 int virUUIDGenerate(unsigned char *uuid) G_GNUC_NO_INLINE;