[Keymanager] Fix alias tokenizing 42/294442/2 accepted/tizen/unified/20230621.023231
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 19 Jun 2023 07:34:16 +0000 (09:34 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 19 Jun 2023 10:28:44 +0000 (10:28 +0000)
There are 2 problems with key-manager alias tokenizing code:
1. strtok_r() used for tokenizing affects internal/opaque key-manager's
   structure ckmc_alias_info_s by modifying the string pointed by the
   pointer returned from ckmc_alias_info_get_alias(). As a result, the
   following call to ckmc_alias_info_is_password_protected(), that uses
   this structure, fails.
2. strtok_r() splits the string when *any* of characters passed in
   'delim' argument is found. However, the ckmc_owner_id_separator is
   in fact a string, not a set of delimiting characters. It's current
   value is ' ' which happens to work well with strtok_r() but this is
   also a part of internal key-manager's code and may be changed in
   future.

Fix above by locating a complete ckmc_owner_id_separator substring and
tokenizing it without modifying the original string.

Change-Id: I86a4dcbb36dad98219acf32a69d9bbb4164c83b4

src/keymanager/keymanager_instance.cc

index 9bb567b..ec1b72b 100644 (file)
@@ -106,17 +106,14 @@ void KeyManagerInstance::KeyManagerGetDataAliasList(const picojson::value& args,
         ReportError(error, &out);
         return;
       }
-      LoggerD("Alias name: %s", name);
-      char* saveptr = nullptr;
-      char* tokenized = strtok_r(name, kSpace.c_str(), &saveptr);
 
-      if (nullptr != tokenized) {
-        obj["packageId"] = picojson::value(tokenized);
-      }
+      LoggerD("Alias name: %s", name);
 
-      tokenized = strtok_r(nullptr, kSpace.c_str(), &saveptr);
-      if (nullptr != tokenized) {
-        obj["name"] = picojson::value(tokenized);
+      auto separator = strstr(name, kSpace.c_str());
+      if (separator != NULL && strlen(name) > separator - name + kSpace.size()) {
+        std::string packageId(name, separator);
+        obj["packageId"] = picojson::value(std::move(packageId));
+        obj["name"] = picojson::value(&separator[kSpace.size()]);
       }
 
       bool is_password_protected = false;