From 21d8df0432af14a95dcf6bd583ba2122afb10b28 Mon Sep 17 00:00:00 2001 From: Jeffrey Hutzelman Date: Tue, 18 Jun 2013 23:34:45 -0400 Subject: [PATCH] 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 Reviewed-by: Derrick Brashear Reviewed-by: Jeffrey Altman --- src/opr/uuid.c | 18 ++++++++++++------ src/opr/uuid.h | 2 +- tests/opr/uuid-t.c | 7 ++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/opr/uuid.c b/src/opr/uuid.c index 433723c4a..493c660b2 100644 --- a/src/opr/uuid.c +++ b/src/opr/uuid.c @@ -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 diff --git a/src/opr/uuid.h b/src/opr/uuid.h index b526a1abd..3423be1ce 100644 --- a/src/opr/uuid.h +++ b/src/opr/uuid.h @@ -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 diff --git a/tests/opr/uuid-t.c b/tests/opr/uuid-t.c index 3e2724a54..276dea87f 100644 --- a/tests/opr/uuid-t.c +++ b/tests/opr/uuid-t.c @@ -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"), -- 2.39.5