diff --git a/src/draw/lv_draw_label.c b/src/draw/lv_draw_label.c index 5fa0e4060f..d49486de18 100644 --- a/src/draw/lv_draw_label.c +++ b/src/draw/lv_draw_label.c @@ -85,8 +85,7 @@ LV_ATTRIBUTE_FAST_MEM void lv_draw_label(lv_layer_t * layer, const lv_draw_label /*The text is stored in a local variable so malloc memory for it*/ if(dsc->text_local) { lv_draw_label_dsc_t * new_dsc = t->draw_dsc; - new_dsc->text = lv_malloc(lv_strlen(new_dsc->text) + 1); - lv_strcpy((char *)new_dsc->text, dsc->text); + new_dsc->text = lv_strdup(dsc->text); } lv_draw_finalize_task_creation(layer, t); diff --git a/src/others/file_explorer/lv_file_explorer.c b/src/others/file_explorer/lv_file_explorer.c index 00d93ea49f..6a391a3cc7 100644 --- a/src/others/file_explorer/lv_file_explorer.c +++ b/src/others/file_explorer/lv_file_explorer.c @@ -118,15 +118,8 @@ void lv_file_explorer_set_quick_access_path(lv_obj_t * obj, lv_file_explorer_dir *dir_str = NULL; } - /*Get the size of the text*/ - size_t len = lv_strlen(path) + 1; - /*Allocate space for the new text*/ - *dir_str = lv_malloc(len); - LV_ASSERT_MALLOC(*dir_str); - if(*dir_str == NULL) return; - - lv_strcpy(*dir_str, path); + *dir_str = lv_strdup(path); } #endif diff --git a/src/stdlib/builtin/lv_string_builtin.c b/src/stdlib/builtin/lv_string_builtin.c index e604751bfa..ae828e59e2 100644 --- a/src/stdlib/builtin/lv_string_builtin.c +++ b/src/stdlib/builtin/lv_string_builtin.c @@ -11,6 +11,7 @@ #include "../../misc/lv_log.h" #include "../../misc/lv_math.h" #include "../../stdlib/lv_string.h" +#include "../../stdlib/lv_mem.h" /********************* * DEFINES @@ -170,6 +171,16 @@ char * lv_strcpy(char * dst, const char * src) return tmp; } +char * lv_strdup(const char * src) +{ + size_t len = lv_strlen(src) + 1; + char * dst = lv_malloc(len); + if(dst == NULL) return NULL; + + lv_memcpy(dst, src, len); /*do memcpy is faster than strncpy when length is known*/ + return dst; +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/stdlib/clib/lv_string_clib.c b/src/stdlib/clib/lv_string_clib.c index 5f593edbba..ea6f09ac31 100644 --- a/src/stdlib/clib/lv_string_clib.c +++ b/src/stdlib/clib/lv_string_clib.c @@ -64,6 +64,11 @@ char * lv_strcpy(char * dst, const char * src) return strcpy(dst, src); } +char * lv_strdup(const char * src) +{ + return strdup(src); +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/stdlib/lv_string.h b/src/stdlib/lv_string.h index f8262f0698..126aca9fb5 100644 --- a/src/stdlib/lv_string.h +++ b/src/stdlib/lv_string.h @@ -83,6 +83,12 @@ char * lv_strncpy(char * dst, const char * src, size_t dest_size); */ char * lv_strcpy(char * dst, const char * src); +/** + * @brief Duplicate a string by allocating a new one and copying the content. + * @param src Pointer to the source of data to be copied. + * @return A pointer to the new allocated string. NULL if failed. + */ +char * lv_strdup(const char * src); /********************** * MACROS diff --git a/src/widgets/dropdown/lv_dropdown.c b/src/widgets/dropdown/lv_dropdown.c index ecc7f74162..5cff946fd5 100644 --- a/src/widgets/dropdown/lv_dropdown.c +++ b/src/widgets/dropdown/lv_dropdown.c @@ -198,13 +198,9 @@ void lv_dropdown_add_option(lv_obj_t * obj, const char * option, uint32_t pos) /*Convert static options to dynamic*/ if(dropdown->static_txt != 0) { char * static_options = dropdown->options; - size_t len = lv_strlen(static_options) + 1; - - dropdown->options = lv_malloc(len); + dropdown->options = lv_strdup(static_options); LV_ASSERT_MALLOC(dropdown->options); if(dropdown->options == NULL) return; - - lv_strcpy(dropdown->options, static_options); dropdown->static_txt = 0; } diff --git a/src/widgets/img/lv_img.c b/src/widgets/img/lv_img.c index 5a766c8ceb..fbced5c6b0 100644 --- a/src/widgets/img/lv_img.c +++ b/src/widgets/img/lv_img.c @@ -118,10 +118,9 @@ void lv_img_set_src(lv_obj_t * obj, const void * src) if(img->src_type == LV_IMG_SRC_FILE || img->src_type == LV_IMG_SRC_SYMBOL) { old_src = img->src; } - char * new_str = lv_malloc(lv_strlen(src) + 1); + char * new_str = lv_strdup(src); LV_ASSERT_MALLOC(new_str); if(new_str == NULL) return; - lv_strcpy(new_str, src); img->src = new_str; if(old_src) lv_free((void *)old_src); diff --git a/src/widgets/menu/lv_menu.c b/src/widgets/menu/lv_menu.c index 7106cfc87b..6a6fa821d8 100644 --- a/src/widgets/menu/lv_menu.c +++ b/src/widgets/menu/lv_menu.c @@ -393,15 +393,12 @@ void lv_menu_set_page_title(lv_obj_t * page_obj, char const * const title) } if(title) { - size_t len = lv_strlen(title) + 1; - page->title = lv_malloc(len); page->static_title = false; - + page->title = lv_strdup(title); LV_ASSERT_MALLOC(page->title); if(page->title == NULL) { return; } - lv_strcpy(page->title, title); } else { page->title = NULL; diff --git a/src/widgets/span/lv_span.c b/src/widgets/span/lv_span.c index 4b1aebdf39..2e81932c5c 100644 --- a/src/widgets/span/lv_span.c +++ b/src/widgets/span/lv_span.c @@ -178,7 +178,7 @@ void lv_span_set_text(lv_span_t * span, const char * text) if(span->txt == NULL) return; span->static_flag = 0; - lv_strcpy(span->txt, text); + lv_memcpy(span->txt, text, text_alloc_len); refresh_self_size(span->spangroup); } diff --git a/src/widgets/tabview/lv_tabview.c b/src/widgets/tabview/lv_tabview.c index 33c87abef8..26b34dab5a 100644 --- a/src/widgets/tabview/lv_tabview.c +++ b/src/widgets/tabview/lv_tabview.c @@ -89,10 +89,8 @@ lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name) if(tabview->tab_pos & LV_DIR_VER) { new_map = lv_malloc((tab_id + 1) * sizeof(const char *)); lv_memcpy(new_map, old_map, sizeof(const char *) * (tab_id - 1)); - size_t len = lv_strlen(name) + 1; - new_map[tab_id - 1] = lv_malloc(len); + new_map[tab_id - 1] = lv_strdup(name); LV_ASSERT_MALLOC(new_map[tab_id - 1]); - lv_strcpy((char *)new_map[tab_id - 1], name); new_map[tab_id] = (char *)""; } /*left or right dir*/ @@ -100,18 +98,14 @@ lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name) new_map = lv_malloc((tab_id * 2) * sizeof(const char *)); lv_memcpy(new_map, old_map, sizeof(const char *) * (tab_id - 1) * 2); if(tabview->tab_cnt == 0) { - size_t len = lv_strlen(name) + 1; - new_map[0] = lv_malloc(len); + new_map[0] = lv_strdup(name); LV_ASSERT_MALLOC(new_map[0]); - lv_strcpy((char *)new_map[0], name); new_map[1] = (char *)""; } else { - size_t len = lv_strlen(name) + 1; new_map[tab_id * 2 - 3] = (char *)"\n"; - new_map[tab_id * 2 - 2] = lv_malloc(len); + new_map[tab_id * 2 - 2] = lv_strdup(name); new_map[tab_id * 2 - 1] = (char *)""; - lv_strcpy((char *)new_map[(tab_id * 2) - 2], name); } } tabview->map = new_map; @@ -140,10 +134,8 @@ void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t id, const char * new_name) if(tabview->tab_pos & LV_DIR_HOR) id *= 2; lv_free(tabview->map[id]); - size_t len = lv_strlen(new_name) + 1; - tabview->map[id] = lv_malloc(len); + tabview->map[id] = lv_strdup(new_name); LV_ASSERT_MALLOC(tabview->map[id]); - lv_strcpy(tabview->map[id], new_name); lv_obj_invalidate(obj); } diff --git a/src/widgets/textarea/lv_textarea.c b/src/widgets/textarea/lv_textarea.c index 511cff230e..0a12b750a5 100644 --- a/src/widgets/textarea/lv_textarea.c +++ b/src/widgets/textarea/lv_textarea.c @@ -301,11 +301,9 @@ void lv_textarea_set_text(lv_obj_t * obj, const char * txt) } if(ta->pwd_mode) { - size_t len = lv_strlen(txt) + 1; - ta->pwd_tmp = lv_realloc(ta->pwd_tmp, len); + ta->pwd_tmp = lv_strdup(txt); LV_ASSERT_MALLOC(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; - lv_strcpy(ta->pwd_tmp, txt); /*Auto hide characters*/ auto_hide_characters(obj); @@ -414,14 +412,10 @@ void lv_textarea_set_password_mode(lv_obj_t * obj, bool en) /*Pwd mode is now enabled*/ if(en) { char * txt = lv_label_get_text(ta->label); - size_t len = lv_strlen(txt); - - ta->pwd_tmp = lv_malloc(len + 1); + ta->pwd_tmp = lv_strdup(txt); LV_ASSERT_MALLOC(ta->pwd_tmp); if(ta->pwd_tmp == NULL) return; - lv_strcpy(ta->pwd_tmp, txt); - pwd_char_hider(obj); lv_textarea_clear_selection(obj); @@ -460,7 +454,7 @@ void lv_textarea_set_password_bullet(lv_obj_t * obj, const char * bullet) return; } - lv_strcpy(ta->pwd_bullet, bullet); + lv_memcpy(ta->pwd_bullet, bullet, txt_len); ta->pwd_bullet[txt_len] = '\0'; }