(word wrap functions in stdstring.c) Avoid implicit strlens, caller

should pass the 'length' to the function instead
This commit is contained in:
LibretroAdmin 2022-08-23 17:06:01 +02:00
parent 85c44f9245
commit e769436876
11 changed files with 101 additions and 59 deletions

View File

@ -1886,6 +1886,7 @@ bool gfx_animation_line_ticker(gfx_animation_ctx_line_ticker_t *line_ticker)
{
char *wrapped_str = NULL;
size_t wrapped_str_len = 0;
size_t line_ticker_str_len = 0;
struct string_list lines = {0};
size_t line_offset = 0;
bool success = false;
@ -1902,7 +1903,8 @@ bool gfx_animation_line_ticker(gfx_animation_ctx_line_ticker_t *line_ticker)
goto end;
/* Line wrap input string */
wrapped_str_len = strlen(line_ticker->str) + 1 + 10; /* 10 bytes use for inserting '\n' */
line_ticker_str_len = strlen(line_ticker->str);
wrapped_str_len = line_ticker_str_len + 1 + 10; /* 10 bytes use for inserting '\n' */
if (!(wrapped_str = (char*)malloc(wrapped_str_len)))
goto end;
wrapped_str[0] = '\0';
@ -1911,6 +1913,7 @@ bool gfx_animation_line_ticker(gfx_animation_ctx_line_ticker_t *line_ticker)
wrapped_str,
wrapped_str_len,
line_ticker->str,
line_ticker_str_len,
(int)line_ticker->line_len,
100, 0);
@ -1979,6 +1982,7 @@ end:
bool gfx_animation_line_ticker_smooth(gfx_animation_ctx_line_ticker_smooth_t *line_ticker)
{
char *wrapped_str = NULL;
size_t line_ticker_src_len = 0;
size_t wrapped_str_len = 0;
struct string_list lines = {0};
int glyph_width = 0;
@ -1995,7 +1999,8 @@ bool gfx_animation_line_ticker_smooth(gfx_animation_ctx_line_ticker_smooth_t *li
gfx_animation_t *p_anim = &anim_st;
const char *wideglyph_str = msg_hash_get_wideglyph_str();
int wideglyph_width = 100;
void (*word_wrap_func)(char *dst, size_t dst_size, const char *src,
void (*word_wrap_func)(char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines)
= wideglyph_str ? word_wrap_wideglyph : word_wrap;
@ -2047,8 +2052,10 @@ bool gfx_animation_line_ticker_smooth(gfx_animation_ctx_line_ticker_smooth_t *li
goto end;
/* Line wrap input string */
wrapped_str_len = strlen(line_ticker->src_str) + 1 + 10; /* 10 bytes use for inserting '\n' */
if (!(wrapped_str = (char*)malloc(wrapped_str_len)))
line_ticker_src_len = strlen(line_ticker->src_str);
/* 10 bytes use for inserting '\n' */
wrapped_str_len = line_ticker_src_len + 1 + 10;
if (!(wrapped_str = (char*)malloc(wrapped_str_len)))
goto end;
wrapped_str[0] = '\0';
@ -2056,6 +2063,7 @@ bool gfx_animation_line_ticker_smooth(gfx_animation_ctx_line_ticker_smooth_t *li
wrapped_str,
wrapped_str_len,
line_ticker->src_str,
line_ticker_src_len,
(int)line_len,
wideglyph_width, 0);

View File

@ -298,10 +298,9 @@ void gfx_widgets_msg_queue_push(
title_length,
1);
msg_widget->text_height = p_dispwidget->gfx_widget_fonts.msg_queue.line_height;
msg_len = title_length + 1 + 1; /* 1 byte uses for inserting '\n' */
msg = (char *)malloc(msg_len);
if (!msg)
/* 1 byte uses for inserting '\n' */
msg_len = title_length + 1 + 1;
if (!(msg = (char *)malloc(msg_len)))
return;
msg[0] = '\0';
@ -314,7 +313,8 @@ void gfx_widgets_msg_queue_push(
if ((text_width - (text_width >> 2)) < width)
width = text_width - (text_width >> 2);
word_wrap(msg, msg_len, title, (title_length * width) / text_width,
word_wrap(msg, msg_len, title, title_length,
(title_length * width) / text_width,
100, 2);
msg_widget->text_height *= 2;

View File

@ -254,7 +254,9 @@ static void logiqx_dat_sanitise_element_data(
if (strstr(sanitised_data, find_string))
{
char *tmp = string_replace_substring(
sanitised_data, find_string, replace_string);
sanitised_data,
find_string, strlen(find_string),
replace_string, strlen(replace_string));
if (!string_is_empty(tmp))
strlcpy(sanitised_data, tmp, sizeof(sanitised_data));

View File

@ -159,8 +159,9 @@ char *string_to_lower(char *s);
char *string_ucwords(char *s);
char *string_replace_substring(const char *in, const char *pattern,
const char *by);
char *string_replace_substring(const char *in,
const char *pattern, size_t pattern_len,
const char *replacement, size_t replacement_len);
/**
* string_trim_whitespace_left:
@ -188,6 +189,7 @@ char *string_trim_whitespace(char *const s);
* @dst : pointer to destination buffer.
* @dst_size : size of destination buffer.
* @src : pointer to input string.
* @src_len : length of @src
* @line_width : max number of characters per line.
* @wideglyph_width : not used, but is necessary to keep
* compatibility with word_wrap_wideglyph().
@ -202,7 +204,7 @@ char *string_trim_whitespace(char *const s);
* correctly any text containing so-called 'wide' Unicode
* characters (e.g. CJK languages, emojis, etc.).
**/
void word_wrap(char *dst, size_t dst_size, const char *src,
void word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);
/**
@ -210,6 +212,7 @@ void word_wrap(char *dst, size_t dst_size, const char *src,
* @dst : pointer to destination buffer.
* @dst_size : size of destination buffer.
* @src : pointer to input string.
* @src_len : length of @src
* @line_width : max number of characters per line.
* @wideglyph_width : effective width of 'wide' Unicode glyphs.
* the value here is normalised relative to the
@ -238,8 +241,11 @@ void word_wrap(char *dst, size_t dst_size, const char *src,
* on-screen pixel width deviates greatly from the set
* @wideglyph_width value.
**/
void word_wrap_wideglyph(char *dst, size_t dst_size, const char *src,
int line_width, int wideglyph_width, unsigned max_lines);
void word_wrap_wideglyph(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width,
unsigned max_lines);
/**
* string_tokenize:

View File

@ -89,9 +89,11 @@ char *string_ucwords(char *s)
}
char *string_replace_substring(const char *in,
const char *pattern, const char *replacement)
const char *pattern, size_t pattern_len,
const char *replacement, size_t replacement_len)
{
size_t numhits, pattern_len, replacement_len, outlen;
size_t outlen;
size_t numhits = 0;
const char *inat = NULL;
const char *inprev = NULL;
char *out = NULL;
@ -102,9 +104,6 @@ char *string_replace_substring(const char *in,
if (!pattern || !replacement)
return strdup(in);
pattern_len = strlen(pattern);
replacement_len = strlen(replacement);
numhits = 0;
inat = in;
while ((inat = strstr(inat, pattern)))
@ -128,7 +127,7 @@ char *string_replace_substring(const char *in,
outat += inat-inprev;
memcpy(outat, replacement, replacement_len);
outat += replacement_len;
inat += pattern_len;
inat += pattern_len;
inprev = inat;
}
strcpy(outat, inprev);
@ -217,13 +216,14 @@ char *string_trim_whitespace(char *const s)
* correctly any text containing so-called 'wide' Unicode
* characters (e.g. CJK languages, emojis, etc.).
**/
void word_wrap(char *dst, size_t dst_size, const char *src,
int line_width, int wideglyph_width, unsigned max_lines)
void word_wrap(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines)
{
char *lastspace = NULL;
unsigned counter = 0;
unsigned lines = 1;
size_t src_len = strlen(src);
const char *src_end = src + src_len;
/* Prevent buffer overflow */
@ -327,12 +327,12 @@ void word_wrap(char *dst, size_t dst_size, const char *src,
* @wideglyph_width value.
**/
void word_wrap_wideglyph(char *dst, size_t dst_size,
const char *src, int line_width,
const char *src, size_t src_len, int line_width,
int wideglyph_width, unsigned max_lines)
{
char *lastspace = NULL;
char *lastwideglyph = NULL;
const char *src_end = src + strlen(src);
const char *src_end = src + src_len;
unsigned lines = 1;
/* 'line_width' means max numbers of characters per line,
* but this metric is only meaningful when dealing with

View File

@ -196,7 +196,7 @@ START_TEST (test_word_wrap)
char output[1024];
word_wrap(output, sizeof(output), testtxt, 40, 100, 10);
word_wrap(output, sizeof(output), testtxt, strlen(testtxt), 40, 100, 10);
ck_assert(!strcmp(output, expected));
}
END_TEST

View File

@ -568,8 +568,10 @@ typedef struct materialui_handle
materialui_font_data_t hint; /* ptr alignment */
} font_data;
void (*word_wrap)(char *dst, size_t dst_size, const char *src,
int line_width, int wideglyph_width, unsigned max_lines);
void (*word_wrap)(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);
/* Thumbnail helpers */
gfx_thumbnail_path_data_t *thumbnail_path_data;
@ -2634,7 +2636,8 @@ static void materialui_render_messagebox(
/* Split message into lines */
(mui->word_wrap)(
wrapped_message, sizeof(wrapped_message), message,
wrapped_message, sizeof(wrapped_message),
message, strlen(message),
usable_width / (int)mui->font_data.list.glyph_width,
mui->font_data.list.wideglyph_width, 0);
@ -2661,9 +2664,8 @@ static void materialui_render_messagebox(
if (!string_is_empty(line))
{
int width = font_driver_get_message_width(
int width = font_driver_get_message_width(
mui->font_data.list.font, line, (unsigned)strlen(line), 1);
longest_width = (width > longest_width) ?
width : longest_width;
}
@ -2797,7 +2799,8 @@ static unsigned materialui_count_sublabel_lines(
(has_icon ? (int)mui->icon_size : 0);
(mui->word_wrap)(
wrapped_sublabel_str, sizeof(wrapped_sublabel_str), entry.sublabel,
wrapped_sublabel_str, sizeof(wrapped_sublabel_str),
entry.sublabel, strlen(entry.sublabel),
sublabel_width_max / (int)mui->font_data.hint.glyph_width,
mui->font_data.hint.wideglyph_width, 0);
@ -4113,8 +4116,10 @@ static void materialui_render_menu_entry_default(
sublabel_y = entry_y + vertical_margin + mui->font_data.list.line_height + (int)mui->sublabel_gap + mui->font_data.hint.line_ascender;
/* Wrap sublabel string */
(mui->word_wrap)(wrapped_sublabel, sizeof(wrapped_sublabel), entry->sublabel,
(int)((usable_width - (int)mui->sublabel_padding) / mui->font_data.hint.glyph_width),
(mui->word_wrap)(wrapped_sublabel, sizeof(wrapped_sublabel),
entry->sublabel, strlen(entry->sublabel),
(int)((usable_width - (int)mui->sublabel_padding)
/ mui->font_data.hint.glyph_width),
mui->font_data.hint.wideglyph_width, 0);
/* Draw sublabel string
@ -4457,8 +4462,10 @@ static void materialui_render_menu_entry_playlist_list(
sublabel_y = entry_y + vertical_margin + mui->font_data.list.line_height + (int)mui->sublabel_gap + mui->font_data.hint.line_ascender;
/* Wrap sublabel string */
(mui->word_wrap)(wrapped_sublabel, sizeof(wrapped_sublabel), entry->sublabel,
(int)((usable_width - (int)mui->sublabel_padding) / mui->font_data.hint.glyph_width),
(mui->word_wrap)(wrapped_sublabel, sizeof(wrapped_sublabel),
entry->sublabel, strlen(entry->sublabel),
(int)((usable_width - (int)mui->sublabel_padding)
/ mui->font_data.hint.glyph_width),
mui->font_data.hint.wideglyph_width, 0);
/* Draw sublabel string

View File

@ -427,8 +427,10 @@ struct ozone_handle
ozone_font_data_t sidebar;
} fonts;
void (*word_wrap)(char *dst, size_t dst_size, const char *src,
int line_width, int wideglyph_width, unsigned max_lines);
void (*word_wrap)(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);
struct
{
@ -3855,14 +3857,16 @@ static void ozone_update_content_metadata(ozone_handle_t *ozone)
/* Word wrap core name string, if required */
if (!scroll_content_metadata)
{
char tmpstr[sizeof(ozone->selection_core_name)];
char tmpstr[256];
unsigned metadata_len =
(ozone->dimensions.thumbnail_bar_width - ((ozone->dimensions.sidebar_entry_icon_padding * 2) * 2)) /
ozone->fonts.footer.glyph_width;
strlcpy(tmpstr, ozone->selection_core_name, sizeof(tmpstr));
(ozone->word_wrap)(ozone->selection_core_name, sizeof(ozone->selection_core_name),
tmpstr, metadata_len, ozone->fonts.footer.wideglyph_width, 0);
(ozone->word_wrap)(ozone->selection_core_name,
sizeof(ozone->selection_core_name),
tmpstr, strlen(tmpstr),
metadata_len, ozone->fonts.footer.wideglyph_width, 0);
ozone->selection_core_name_lines = ozone_count_lines(ozone->selection_core_name);
}
else
@ -3909,10 +3913,11 @@ static void ozone_update_content_metadata(ozone_handle_t *ozone)
* formats. Last played strings are well defined, however
* (unlike core names), so this should never overflow the
* side bar */
char tmpstr[sizeof(ozone->selection_lastplayed)];
char tmpstr[256];
strlcpy(tmpstr, ozone->selection_lastplayed, sizeof(tmpstr));
(ozone->word_wrap)(ozone->selection_lastplayed, sizeof(ozone->selection_lastplayed), tmpstr, 30, 100, 0);
(ozone->word_wrap)(ozone->selection_lastplayed,
sizeof(ozone->selection_lastplayed), tmpstr, strlen(tmpstr), 30, 100, 0);
ozone->selection_lastplayed_lines = ozone_count_lines(ozone->selection_lastplayed);
}
else
@ -4971,7 +4976,10 @@ static void ozone_compute_entries_position(
if (ozone->show_thumbnail_bar)
sublabel_max_width -= ozone->dimensions.thumbnail_bar_width - entry_padding * 2;
(ozone->word_wrap)(wrapped_sublabel_str, sizeof(wrapped_sublabel_str), entry.sublabel,
(ozone->word_wrap)(wrapped_sublabel_str,
sizeof(wrapped_sublabel_str),
entry.sublabel,
strlen(entry.sublabel),
sublabel_max_width /
ozone->fonts.entries_sublabel.glyph_width,
ozone->fonts.entries_sublabel.wideglyph_width, 0);
@ -5317,8 +5325,12 @@ border_iterate:
}
wrapped_sublabel_str[0] = '\0';
(ozone->word_wrap)(wrapped_sublabel_str, sizeof(wrapped_sublabel_str),
sublabel_str, sublabel_max_width / ozone->fonts.entries_sublabel.glyph_width,
(ozone->word_wrap)(wrapped_sublabel_str,
sizeof(wrapped_sublabel_str),
sublabel_str,
strlen(sublabel_str),
sublabel_max_width
/ ozone->fonts.entries_sublabel.glyph_width,
ozone->fonts.entries_sublabel.wideglyph_width, 0);
sublabel_str = wrapped_sublabel_str;
}
@ -6241,8 +6253,9 @@ static void ozone_draw_osk(ozone_handle_t *ozone,
text_color = ozone_theme_light.text_sublabel_rgba;
}
(ozone->word_wrap)(message, sizeof(message), text,
(video_width - margin*2 - padding*2) / ozone->fonts.entries_label.glyph_width,
(ozone->word_wrap)(message, sizeof(message), text, strlen(text),
(video_width - margin*2 - padding*2) /
ozone->fonts.entries_label.glyph_width,
ozone->fonts.entries_label.wideglyph_width, 0);
string_list_initialize(&list);
@ -6358,7 +6371,8 @@ static void ozone_draw_messagebox(
/* Split message into lines */
(ozone->word_wrap)(
wrapped_message, sizeof(wrapped_message), message,
wrapped_message, sizeof(wrapped_message),
message, strlen(message),
usable_width / (int)ozone->fonts.footer.glyph_width,
ozone->fonts.footer.wideglyph_width, 0);

View File

@ -4266,7 +4266,8 @@ static void rgui_render_messagebox(rgui_t *rgui, const char *message,
/* Split message into lines */
word_wrap(
wrapped_message, sizeof(wrapped_message), message,
wrapped_message, sizeof(wrapped_message),
message, strlen(message),
rgui->term_layout.width,
100, 0);

View File

@ -317,8 +317,10 @@ typedef struct xmb_handle
video_font_raster_block_t raster_block;
video_font_raster_block_t raster_block2;
void (*word_wrap)(char *dst, size_t dst_size, const char *src,
int line_width, int wideglyph_width, unsigned max_lines);
void (*word_wrap)(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);
menu_screensaver_t *screensaver;
@ -1002,7 +1004,8 @@ static void xmb_render_messagebox_internal(
/* Split message into lines */
(xmb->word_wrap)(
wrapped_message, sizeof(wrapped_message), message,
wrapped_message, sizeof(wrapped_message),
message, strlen(message),
usable_width / (xmb->font_size * 0.6f),
xmb->wideglyph_width, 0);
@ -1091,7 +1094,7 @@ static char* xmb_path_dynamic_wallpaper(xmb_handle_t *xmb)
{
char path[PATH_MAX_LENGTH];
size_t len = 0;
char *tmp = string_replace_substring(xmb->title_name, "/", " ");
char *tmp = string_replace_substring(xmb->title_name, "/", STRLEN_CONST("/"), " ", STRLEN_CONST(" "));
settings_t *settings = config_get_ptr();
const char *dir_dynamic_wallpapers = settings->paths.directory_dynamic_wallpapers;
unsigned depth = (unsigned)xmb_list_get_size(xmb, MENU_LIST_PLAIN);

View File

@ -1187,14 +1187,15 @@ void CoreOptionsDialog::buildLayout()
if (!string_is_empty(option->info))
{
char *new_info;
size_t new_info_len = strlen(option->info) + 1;
size_t option_info_len = strlen(option->info);
size_t new_info_len = option_info_len + 1;
new_info = (char *)malloc(new_info_len);
if (!new_info)
if (!(new_info = (char *)malloc(new_info_len)))
return;
new_info[0] = '\0';
word_wrap(new_info, new_info_len, option->info, 50, 100, 0);
word_wrap(new_info, new_info_len, option->info,
option_info_len, 50, 100, 0);
descLabel->setToolTip(new_info);
combo_box->setToolTip(new_info);
free(new_info);