Fix the bug in UUID convertor.
authorleechul <chuls.lee@samsung.com>
Tue, 29 Mar 2016 07:01:42 +0000 (16:01 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Tue, 29 Mar 2016 10:57:27 +0000 (10:57 +0000)
Previous ConvertStrToUuid() API does not guarantee correct result in case of empty UUID.
Even if input string is empty string,the input string  may contain the garbage except first byte.

Here is bad case for ConvertStrToUuid() API :

Input : b'00 00 00 00 00 00 00 00 00 00 CC CC FF FF FF FF'
Since first byte is NULL character, the input string is empty string exactly.
The expected result is "00000000-0000-0000-0000-000000000000" in case of empty string.
But current ConvertStrToUuid() will be generated "00000000-0000-0000-0000-CCCCFFFFFFFF" for above input.

This patch is intended to fix it.

[Patch #1] Initial upload
[Patch #2] Update commit message
[Patch #3] Update according to comments.
[Patch #4] Update log message.
[Patch #5] Fix the typo on log message according to review comment.

Change-Id: I97c95a8fea30ffdf7e4c4ff6622e083b8357705a
Signed-off-by: leechul <chuls.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7431
Reviewed-by: Kyungsun Cho <goodsun.cho@samsung.com>
Reviewed-by: Yonggoo Kang <ygace.kang@samsung.com>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/src/srmutility.c

index 8c9e991..1cc5694 100644 (file)
@@ -181,15 +181,33 @@ OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
 
     size_t urnIdx = 0;
     size_t uuidIdx = 0;
+    size_t strUuidLen = 0;
     char convertedUuid[UUID_LENGTH * 2] = {0};
 
-    for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2)
+    strUuidLen = strlen(strUuid);
+    if(0 == strUuidLen)
     {
-        if(*(strUuid + urnIdx) == '-')
+        OIC_LOG(INFO, TAG, "The empty string detected, The UUID will be converted to "\
+                           "\"00000000-0000-0000-0000-000000000000\"");
+    }
+    else if(UUID_LENGTH * 2 + 4 == strUuidLen)
+    {
+        for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2)
         {
-            urnIdx++;
+            if(*(strUuid + urnIdx) == '-')
+            {
+                urnIdx++;
+            }
+            sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
         }
-        sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
+    }
+    else
+    {
+        OIC_LOG(ERROR, TAG, "Invalid string uuid format, Please set the uuid as correct format");
+        OIC_LOG(ERROR, TAG, "e.g) \"72616E64-5069-6E44-6576-557569643030\" (4-2-2-2-6)");
+        OIC_LOG(ERROR, TAG, "e.g) \"\"");
+
+        return OC_STACK_INVALID_PARAM;
     }
 
     memcpy(uuid->id, convertedUuid, UUID_LENGTH);