diff --git a/src/widgets/dropdown/lv_dropdown.c b/src/widgets/dropdown/lv_dropdown.c index ea93be3a4c..da18176283 100644 --- a/src/widgets/dropdown/lv_dropdown.c +++ b/src/widgets/dropdown/lv_dropdown.c @@ -404,11 +404,13 @@ int32_t lv_dropdown_get_option_index(lv_obj_t * obj, const char * option) uint32_t char_i = 0; uint32_t opt_i = 0; const char * start = opts; + const size_t option_len = lv_strlen(option); /*avoid recomputing this multiple times in the loop*/ while(start[0] != '\0') { for(char_i = 0; (start[char_i] != '\n') && (start[char_i] != '\0'); char_i++); - if(memcmp(start, option, LV_MIN(lv_strlen(option), char_i)) == 0) return opt_i; + if(option_len == char_i && + memcmp(start, option, option_len) == 0) return opt_i; /*cannot match exactly unless they are the same length*/ start = &start[char_i]; if(start[0] == '\n') start++; opt_i++; diff --git a/tests/src/test_cases/test_dropdown.c b/tests/src/test_cases/test_dropdown.c index a1c30b1a06..546be0da30 100644 --- a/tests/src/test_cases/test_dropdown.c +++ b/tests/src/test_cases/test_dropdown.c @@ -441,5 +441,17 @@ void test_dropdown_should_list_on_top(void) TEST_ASSERT_EQUAL_INT(2, lv_obj_get_index(list)); } +/* See #4191 */ +void test_dropdown_get_options_should_check_lengths(void) +{ + lv_obj_t * dd = lv_dropdown_create(lv_scr_act()); + lv_dropdown_set_options(dd, "abc\nab"); + TEST_ASSERT_EQUAL_INT(1, lv_dropdown_get_option_index(dd, "ab")); + + lv_dropdown_set_options(dd, "Option 1\nOption 2\nOption 3\nOption"); + TEST_ASSERT_EQUAL_INT(3, lv_dropdown_get_option_index(dd, "Option")); + TEST_ASSERT_EQUAL_INT(-1, lv_dropdown_get_option_index(dd, "Option ")); +} + #endif