limit strchr checking until original string length 97/107297/1
authorJeesun Kim <iamjs.kim@samsung.com>
Tue, 27 Dec 2016 10:52:43 +0000 (19:52 +0900)
committerJeesun Kim <iamjs.kim@samsung.com>
Tue, 27 Dec 2016 10:53:34 +0000 (19:53 +0900)
Change-Id: I6c7b0ca2834a779062a2212af5f0a416a4ba4d51

server/ctsvc_utils_string.c

index 275c606..b2211f0 100644 (file)
@@ -484,10 +484,14 @@ static bool _get_modified_number(char *temp, char *keyword, int *out_len_keyword
        return true;
 }
 
-static char *_strchr_with_nth(char *s, int c, int nth)
+static char *_strchr_with_nth(char *end_pointer, char *s, int c, int nth)
 {
        char *cursor = s;
        while (*cursor) {
+               if (end_pointer <= cursor) {
+                       ERR("over end");
+                       return end_pointer;
+               }
                if (*cursor == c)
                        nth--;
 
@@ -499,17 +503,16 @@ static char *_strchr_with_nth(char *s, int c, int nth)
        return cursor;
 }
 
-static char *_strrchr_with_nth(char *ori, char *s, int c, int nth)
+static char *_strrchr_with_nth(char *start_pointer, char *s, int c, int nth)
 {
        char *cursor = s;
        while (*cursor) {
-               if (cursor <= ori) {
+               if (cursor <= start_pointer) {
                        /* LCOV_EXCL_START */
-                       ERR("over");
-                       return ori;
+                       ERR("over start");
+                       return start_pointer;
                        /* LCOV_EXCL_STOP */
                }
-
                if (*cursor == c)
                        nth--;
 
@@ -591,11 +594,13 @@ char *ctsvc_utils_get_modified_str(char *temp, bool is_snippet, const char *keyw
                /* LCOV_EXCL_STOP */
        }
 
+       /* keyword length could be changed when chosung is inserted.
+        * so length could be calculated again manually.
+        */
        char *keyword_start = NULL;
        snprintf(mod_temp, len_offset + 1, "%s", temp);
        len_print = len_offset;
-       len_print += snprintf(mod_temp + len_print, len_full - len_print,
-                       "%s", start_match);
+       len_print += snprintf(mod_temp + len_print, len_full - len_print, "%s", start_match);
        keyword_start = mod_temp + len_print;
        snprintf(mod_temp + len_print, len_keyword + 1, "%s", temp + len_offset);
        len_print += len_keyword;
@@ -606,7 +611,7 @@ char *ctsvc_utils_get_modified_str(char *temp, bool is_snippet, const char *keyw
                return mod_temp;
 
        char *cursor = NULL;
-       cursor = _strchr_with_nth(keyword_start, ' ', token_number);
+       cursor = _strchr_with_nth(mod_temp + len_full, keyword_start, ' ', token_number);
        *cursor = '\0';
        cursor = _strrchr_with_nth(mod_temp, keyword_start, ' ', token_number);
        memcpy(mod_temp, cursor, len_full);