
On 05/25/2010 08:26 AM, Daniel P. Berrange wrote:
+ +# UUID of the host: +# Provide the UUID of the host here in case the command +# 'dmidecode -s system-uuid' does not provide a valid uuid. In case +# 'dmidecode' does not provide a valid UUID and none is provided here, a +# temporary UUID will be generated. +# Keep the format of the example UUID below. UUID must not have all digits +# be the same. + +#host_uuid = "8510b1a1-1afa-4da6-8111-785fae202c1e"
What's the likelihood that people just uncomment this line, and this becomes a very common "uuid" that is no longer unique? http://www.snopes.com/business/taxes/woolworth.asp The sample is worthwhile, to show the locations where - must appear, but is it any better to write the sample as a known-invalid string, with all digits the same?
+ +/** + * virUUIDIsValid + * + * @uuid: The UUID to test + * + * Do some basic tests to check whether the given UUID is + * valid as a host UUID. + * Basic tests: + * - Not all of the digits may be equal + */ +int +virUUIDIsValid(unsigned char *uuid) +{ + unsigned int i, ctr = 1; + unsigned char c; + + if (!uuid) + return 0; + + c = uuid[0]; + + for (i = 1; i < VIR_UUID_BUFLEN; i++) + if (uuid[i] == c) + ctr++; + + return (ctr != VIR_UUID_BUFLEN);
Is it worth the micro-optimization of breaking out of the loop on the first uuid[i] != c, rather than parsing the entire string? That way, you would only need two or three comparisons on the common case, rather than a full 32. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org