Make opr_uuid_toString return a status

Don't assume that converting a UUID to a string will always succeed.
Instead, opr_uuid_toString should return a status result to indicate
whether the operation was successful or not.

Change-Id: I49e6bf53b2a878342d3137510d2eca522e58604d
Reviewed-on: http://gerrit.openafs.org/9990
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@your-file-system.com>
Reviewed-by: Jeffrey Altman <jaltman@your-file-system.com>
This commit is contained in:
Jeffrey Hutzelman 2013-06-18 23:34:45 -04:00 committed by Jeffrey Altman
parent 6aefd0d93b
commit 21d8df0432
3 changed files with 17 additions and 10 deletions

View File

@ -64,17 +64,23 @@ opr_uuid_hash(const opr_uuid_t *uuid)
}
#if !defined(KERNEL)
void
int
opr_uuid_toString(const opr_uuid_t *uuid, char **string)
{
unsigned const char *p;
int r;
p = uuid->data;
asprintf(string,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x%02x%02x%02x%02x",
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
r = asprintf(string,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x%02x%02x%02x%02x",
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
if (r < 0) {
*string = NULL;
return ENOMEM;
}
return 0;
}
void

View File

@ -27,7 +27,7 @@ extern int opr_uuid_equal(const opr_uuid_t *uuid1, const opr_uuid_t *uuid2);
extern unsigned int opr_uuid_hash(const opr_uuid_t *uuid);
#if !defined(KERNEL)
extern void opr_uuid_toString(const opr_uuid_t *uuid, char **string);
extern int opr_uuid_toString(const opr_uuid_t *uuid, char **string);
extern void opr_uuid_freeString(char *string);
extern int opr_uuid_fromString(opr_uuid_t *uuid, const char *string);
#endif

View File

@ -23,7 +23,7 @@ main(int argc, char **argv)
int version;
struct opr_uuid_unpacked raw;
plan(16);
plan(18);
memset(&uuidC, 0, sizeof(opr_uuid_t));
@ -37,9 +37,10 @@ main(int argc, char **argv)
is_int(1187447773, opr_uuid_hash(&uuidA), "opr_uuid_hash(A) works");
is_int(1251907497, opr_uuid_hash(&uuidB), "opr_uuid_hash(B) works");
opr_uuid_toString(&uuidA, &str);
ok(!opr_uuid_toString(&uuidA, &str), "opr_uuid_toString(uuidA) works");
ok(str != NULL, "... and result is not NULL");
is_string("4f449447-76ba-472c-971a-866bc0101a4b", str,
"opr_uuid_toString(uuidA) works");
"... and string is correct");
opr_uuid_freeString(str);
is_int(0, opr_uuid_fromString(&uuidC, "4F449447-76BA-472C-971A-866BC0101A4B"),