Fix parameter type, segfault, logs, and minor issues 33/281533/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 14 Sep 2022 08:59:21 +0000 (17:59 +0900)
committerTizen AI <ai.tzn.sec@samsung.com>
Tue, 20 Sep 2022 02:54:14 +0000 (11:54 +0900)
This patch includes these changes:
- Use reference type parameter for reducing copy time
- Add logs for better debugging
- Remove overflowed array access
- Remove null pointer access
- Fix error handling logic

Change-Id: I025e5eb4edb0749a8f0b52a8ade08ce461961562
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
src/mmimgr/iu/CommandFinder.cpp
src/mmimgr/iu/CommandFinder.h
src/mmimgr/iu/CommandManager.cpp
src/mmimgr/iu/CommandManager.h
src/mmimgr/iu/StringUtil.cpp

index 4dd2b19..ceb0fdd 100644 (file)
@@ -33,7 +33,7 @@
 
 using namespace std;
 
-CommandFinder::CommandFinder(std::string text, std::list<std::shared_ptr<ClickableItem>>& commands) :
+CommandFinder::CommandFinder(std::string &text, std::list<std::shared_ptr<ClickableItem>>& commands) :
                __WORD_MATCHING_RATE(0.3), __CHAR_MATCHING_RATE(0.25), __MIN_WORD_SIZE(3), __commands(commands)
 {
        _I("[CommandFinder] Initiate command searcher. text(%s)", text.c_str());
@@ -58,7 +58,7 @@ ClickableItem* CommandFinder::findCommand()
 {
        ClickableItem* result = nullptr;
        if (__isLabel) {
-               _I("[CommandFinder] Finde command by label text. text(%s)", __loweredText.c_str());
+               _I("[CommandFinder] Find command by label text. text(%s)", __loweredText.c_str());
                result = findCommandByText();
                if (result != nullptr) {
                        return result;
@@ -74,7 +74,7 @@ ClickableItem* CommandFinder::findCommand()
                        return result;
                }
        } else {
-               _I("[CommandFinder] Finde command by index. index(%d)", __index);
+               _I("[CommandFinder] Find command by index. index(%d)", __index);
                result = findCommandByIndex();
        }
 
@@ -92,6 +92,7 @@ ClickableItem* CommandFinder::findCommandByIndex()
 
        for (auto command : __commands) {
                if (command->index == __index) {
+                       _I("[CommandFinder] result command(%s)", command->label.c_str());
                        return command.get();
                }
        }
@@ -115,6 +116,7 @@ ClickableItem* CommandFinder::findCommandByText()
                }
 
                if (isValid) {
+                       _I("[CommandFinder] result command(%s)", command->label.c_str());
                        return command.get();
                }
        }
@@ -125,10 +127,10 @@ ClickableItem* CommandFinder::findCommandByText()
 ClickableItem* CommandFinder::findCommandByWord()
 {
        _I("[CommandFinder] Find command by word");
-
        int maxNumber = __splitText.size() * __WORD_MATCHING_RATE;
        ClickableItem* result = nullptr;
 
+       _I("[CommandFinder] Minimum matching word number (%d)", maxNumber);
        for (auto command : __commands) {
                string label = StringUtil::makeLowerCase(command->label);
 
@@ -145,12 +147,16 @@ ClickableItem* CommandFinder::findCommandByWord()
                }
        }
 
+       if (result != nullptr) {
+               _I("[CommandFinder] result command(%s)", result->label.c_str());
+       }
+
        return result;
 }
 
 ClickableItem* CommandFinder::findCommandByChar()
 {
-       _I("[CommandFinder] Find command by word");
+       _I("[CommandFinder] Find command by char");
 
        ClickableItem* result = nullptr;
        int resultScore = numeric_limits<int>::max();
@@ -158,6 +164,7 @@ ClickableItem* CommandFinder::findCommandByChar()
        for (auto command : __commands) {
                string label = StringUtil::makeLowerCase(command->label);
                vector<string> splitLabel = StringUtil::splitText(label, ' ');
+               _D("[CommandFinder] Command(%s), candidate(%s)", __loweredText.c_str(), label.c_str());
 
                int score = 0;
                for (auto& word : __splitText) {
@@ -167,7 +174,8 @@ ClickableItem* CommandFinder::findCommandByChar()
 
                        // This is for calculating Levenshtein distance as similarity matric.
                        const int __MIN_THRESHOLD = word.size() * __CHAR_MATCHING_RATE;
-                       int minScore = __MIN_THRESHOLD;
+                       int minScore = __MIN_THRESHOLD + 1;
+                       _D("[CommandFinder] Minimum matching character number (%d)", __MIN_THRESHOLD);
                        for (auto& labelWord : splitLabel) {
                                if (labelWord.size() < __MIN_WORD_SIZE) {
                                        continue;
@@ -187,8 +195,8 @@ ClickableItem* CommandFinder::findCommandByChar()
                                int longSize = longString.size();
                                int shortSize = shortString.size();
 
-                               int* cost = new int[longSize] {0, };
-                               int* ncost = new int[longSize] {0, };
+                               int* cost = new int[longSize + 1] {0, };
+                               int* ncost = new int[longSize + 1] {0, };
 
                                for (int l = 0; l <= longSize; l++) {
                                        cost[l] = l;
@@ -220,9 +228,11 @@ ClickableItem* CommandFinder::findCommandByChar()
                        }
 
 
-                       score += (minScore < __MIN_THRESHOLD ? minScore : word.size());
+                       _D("[CommandFinder] word(%s), labelword(%s), minscore(%d)", word.c_str(), label.c_str(), minScore);
+                       score += (minScore <= __MIN_THRESHOLD ? minScore : word.size());
                }
 
+               _D("[CommandFinder] result score(%d), score(%d)", resultScore, score);
                if (resultScore > score) {
                        resultScore = score;
                        result = command.get();
@@ -237,6 +247,7 @@ ClickableItem* CommandFinder::findCommandByChar()
        }
 
        if (threshold > resultScore) {
+               _I("[CommandFinder] result command(%s)", result->label.c_str());
                return result;
        }
 
index 31f43b9..ace8201 100644 (file)
@@ -34,7 +34,7 @@
 
 class CommandFinder {
        public:
-               CommandFinder(std::string text, std::list<std::shared_ptr<ClickableItem>> &__commands);
+               CommandFinder(std::string &text, std::list<std::shared_ptr<ClickableItem>> &__commands);
                CommandFinder(int index, std::list<std::shared_ptr<ClickableItem>> &__commands);
 
                ClickableItem* findCommand();
index e9f1856..0efb6f2 100644 (file)
@@ -33,7 +33,7 @@
 
 using namespace std;
 
-void CommandManager::addCommand(ClickableItem item)
+void CommandManager::addCommand(ClickableItem &item)
 {
        try {
                shared_ptr<ClickableItem> command = make_shared<ClickableItem>(item);
@@ -49,7 +49,7 @@ void CommandManager::cleanCommands()
        __commands.clear();
 }
 
-ClickableItem* CommandManager::findCommand(std::string text, bool isIndex)
+ClickableItem* CommandManager::findCommand(std::string &text, bool isIndex)
 {
        int index = -1;
        try {
@@ -58,7 +58,8 @@ ClickableItem* CommandManager::findCommand(std::string text, bool isIndex)
                _E("[CommandManager] Fail to convert. exception(%s)", e.what());
        }
 
-       if (isIndex) {
+       if (isIndex && index < 0) {
+               _E("[CommandManager] Index is not valid(%s)", text.c_str());
                return nullptr;
        }
 
@@ -70,7 +71,11 @@ ClickableItem* CommandManager::findCommand(std::string text, bool isIndex)
        }
 
        auto command = finder->findCommand();
-       _I("[CommandManager] Find command. text(%s). command(%s)", text.c_str(), command->label.c_str());
+       if (command != nullptr) {
+               _I("[CommandManager] Find command. text(%s). command(%s)", text.c_str(), command->label.c_str());
+       } else {
+               _E("[CommandManager] Fail to Find command. text(%s)", text.c_str());
+       }
 
        return command;
 }
index c467080..b55a37a 100644 (file)
@@ -33,9 +33,9 @@
 
 class CommandManager {
        public:
-               void addCommand(ClickableItem item);
+               void addCommand(ClickableItem &item);
                void cleanCommands();
-               ClickableItem* findCommand(std::string text, bool isIndex);
+               ClickableItem* findCommand(std::string &text, bool isIndex);
 
        private:
                std::list<std::shared_ptr<ClickableItem>> __commands;
index d30834b..ba0c129 100644 (file)
@@ -31,10 +31,8 @@ using namespace std;
 
 std::string StringUtil::makeLowerCase(std::string text)
 {
-       string result;
-       transform(text.begin(), text.end(), result.begin(), ::tolower);
-
-       return result;
+       transform(text.begin(), text.end(), text.begin(), ::tolower);
+       return text;
 }
 
 std::vector<std::string> StringUtil::splitText(std::string text, char delimiter)