From cf343fe58e663e9ccd1e5f91ea6511058b0da761 Mon Sep 17 00:00:00 2001 From: Ali Alzyod Date: Fri, 29 Mar 2019 09:52:51 -0400 Subject: [PATCH] elm_entry: Speedup finding new line, prevent readind invalid memory Summary: 1- Speed up detecting new lines. ``` if (!strncmp(text, "' || ((text[3] == '/') && (text[4] == '>'))) ``` string could reach last char in string (original string ends with "' || (len > 1 && ((text[0] == '/') && (text[1] == '>')))) ``` Test Plan: ``` static int oldFunc(const char *text) { if (!text) return 0; while (*text) { if (!strncmp(text, "' || ((text[3] == '/') && (text[4] == '>'))) { return 1; } } text++; } return 0; } static int newFunc(const char *text) { if (!text) return 0; char *pTemp = (char *)text; while (pTemp = strchr(pTemp, '<')) { pTemp++; if (!strncmp(pTemp, "br", 2) || !strncmp(pTemp, "ps", 2)) { pTemp += 2; if (pTemp[0] != '\0' && (pTemp[0] == '>' || (pTemp[0] == '/' && pTemp[1] == '>'))) { return 1; } } } return 0; } int main() { int counter = 1000; srand(time(NULL)); char pStr[50001] = {0}; char AllChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789<>"; int AllCharsLen = strlen(AllChars); for (int i = 0; i < 50000; i++) pStr[i] = AllChars[rand() % AllCharsLen]; clock_t start, end; double total_Time1 = 0; int i; for (int j = 0; j < 3; j++) { if (j == 0) { printf("random String\n"); } else if (j == 1) { printf("With Random
\n"); int location = rand()%(5000 - 5); pStr[location++] = '<'; pStr[location++] = 'b'; pStr[location++] = 'r'; pStr[location++] = '/'; pStr[location++] = '>'; } else if (j == 2) { printf("With Random \n"); int location = rand()%(5000 - 4); pStr[location++] = '<'; pStr[location++] = 'p'; pStr[location++] = 's'; pStr[location++] = '>'; } start = clock(); for (i = 0; i < counter; i++) oldFunc(pStr); end = clock(); total_Time1 = ((double)(end - start)) / CLOCKS_PER_SEC; printf("original = %f has new Line = %i\n", total_Time1, oldFunc(pStr)); start = clock(); for (i = 0; i < counter; i++) newFunc(pStr); end = clock(); total_Time1 = ((double)(end - start)) / CLOCKS_PER_SEC; printf("modified = %f has new line = %i\n\n", total_Time1, newFunc(pStr)); } } ``` output: random String original = 2.523000 has new Line = 0 modified = 0.090000 has new line = 0 With Random
original = 0.081000 has new Line = 1 modified = 0.003000 has new line = 1 With Random original = 0.016000 has new Line = 1 modified = 0.001000 has new line = 1 Reviewers: zmike, woohyun, bowonryu Reviewed By: zmike Subscribers: bu5hm4n, segfaultxavi, zmike, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D8497 --- src/lib/elementary/elm_entry.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib/elementary/elm_entry.c b/src/lib/elementary/elm_entry.c index 500757d..afc61e0 100644 --- a/src/lib/elementary/elm_entry.c +++ b/src/lib/elementary/elm_entry.c @@ -3904,18 +3904,20 @@ _entry_has_new_line(const char *text) { if (!text) return EINA_FALSE; - while (*text) - { - if (!strncmp(text, "' || ((text[3] == '/') && (text[4] == '>'))) - { - return EINA_TRUE; - } - } - text++; - } + const char * pTemp = text; + while ((pTemp = strchr(pTemp, '<'))) + { + pTemp++; + if (!strncmp(pTemp, "br", 2) || !strncmp(pTemp, "ps", 2)) + { + pTemp += 2; + if (pTemp[0] != '\0' && (pTemp[0] == '>' || (pTemp[0] == '/' && pTemp[1] == '>'))) + { + return EINA_TRUE; + } + } + } return EINA_FALSE; } -- 2.7.4