viruuid: Rework virUUIDIsValid()

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>
This commit is contained in:
Michal Privoznik 2020-11-24 11:12:27 +01:00
parent abf12f071b
commit 32217bb709
2 changed files with 8 additions and 11 deletions

View 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)
{

View 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;