Skip to content

Commit 9d489a0

Browse files
committed
Get rid of more strtok_r calls
1 parent f0b8c5a commit 9d489a0

6 files changed

Lines changed: 82 additions & 59 deletions

File tree

core_option_manager.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,27 +1200,23 @@ core_option_manager_t *core_option_manager_new(
12001200
* the map */
12011201
const char *category_key = opt->opts[_len].category_key;
12021202
char address[256];
1203+
size_t __len = 0;
12031204

12041205
/* Address string is nominally:
12051206
* <category_key><delim><tag><option_key>
12061207
* ...where <tag> is prepended to the option
12071208
* key in order to avoid category/option key
12081209
* collisions */
12091210
if (string_is_empty(category_key))
1210-
{
1211-
size_t __len = 0;
12121211
address[ __len] = '#';
1213-
address[++__len] = '\0';
1214-
strlcpy(address + __len, option_def->key, sizeof(address) - __len);
1215-
}
12161212
else
12171213
{
1218-
size_t __len = strlcpy(address, category_key, sizeof(address) - 3);
1214+
__len = strlcpy(address, category_key, sizeof(address) - 3);
12191215
address[ __len] = ':';
12201216
address[++__len] = '#';
1221-
address[++__len] = '\0';
1222-
strlcpy(address + __len, option_def->key, sizeof(address) - __len);
12231217
}
1218+
address[++__len] = '\0';
1219+
strlcpy(address + __len, option_def->key, sizeof(address) - __len);
12241220

12251221
if (!nested_list_add_item(opt->option_map,
12261222
address, ":",

libretro-common/compat/compat_getopt.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,31 @@ static int parse_short(const char *optstring, char * const *argv)
124124
static int parse_long(const struct option *longopts, char * const *argv)
125125
{
126126
size_t i;
127-
char *save = NULL;
128-
char *argv0 = strdup(&argv[0][2]);
129-
char *token = strtok_r(argv0, "=", &save);
127+
const char *arg = &argv[0][2];
128+
const char *eq = strchr(arg, '=');
129+
size_t len = eq ? (size_t)(eq - arg) : strlen(arg);
130130
const struct option *opt = NULL;
131131

132132
for (i = 0; longopts[i].name; i++)
133133
{
134-
if (token && !strcmp(longopts[i].name, token))
134+
if (strlen(longopts[i].name) == len && !strncmp(longopts[i].name, arg, len))
135135
{
136136
opt = &longopts[i];
137137
break;
138138
}
139139
}
140140

141-
free(argv0);
142-
argv0 = NULL;
143-
144141
if (!opt)
145142
return '?';
146143

147144
/* Handle args with '=' instead of space */
148-
if (opt->has_arg)
145+
if (opt->has_arg && eq)
149146
{
150-
char *special_arg = strchr(argv[0], '=');
151-
if (special_arg)
152-
{
153-
optarg = ++special_arg;
154-
optind++;
155-
return opt->val;
156-
}
147+
optarg = (char *)(eq + 1);
148+
optind++;
149+
return opt->val;
157150
}
158151

159-
/* getopt_long has an "optional" arg, but we don't bother with that. */
160152
if (opt->has_arg && !argv[1])
161153
return '?';
162154

libretro-common/include/string/stdstring.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,15 @@ char *string_tokenize(char **str, const char *delim);
267267

268268
/**
269269
* string_remove_all_chars:
270-
* @str : input string (must be non-NULL, otherwise UB)
270+
* @s : input string (must be non-NULL, otherwise UB)
271271
*
272272
* Leaf function.
273273
*
274-
* Removes every instance of character @c from @str
274+
* Removes every instance of character @c from @s
275+
*
276+
* Returns the length of the resulting string.
275277
**/
276-
void string_remove_all_chars(char *str, char c);
278+
size_t string_remove_all_chars(char *s, char c);
277279

278280
/**
279281
* string_replace_all_chars:

libretro-common/string/stdstring.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,21 +513,22 @@ char* string_tokenize(char **str, const char *delim)
513513
* Leaf function.
514514
*
515515
* Removes every instance of character @c from @s
516+
*
517+
* Returns the length of the resulting string.
516518
**/
517-
void string_remove_all_chars(char *s, char c)
519+
size_t string_remove_all_chars(char *s, char c)
518520
{
519-
char *read_ptr = s;
520-
char *write_ptr = s;
521-
522-
while (*read_ptr != '\0')
521+
char *dst = s;
522+
char *src = s;
523+
while (*src)
523524
{
524525
/* Only write if the character is not the one to remove */
525-
if (*read_ptr != c)
526-
*write_ptr++ = *read_ptr;
527-
read_ptr++;
526+
if (*src != c)
527+
*dst++ = *src;
528+
src++;
528529
}
529-
530-
*write_ptr = '\0';
530+
*dst = '\0';
531+
return (size_t)(dst - s);
531532
}
532533

533534
/**

menu/drivers/ozone.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7050,7 +7050,6 @@ static void ozone_draw_osk(
70507050
0.00, 0.00, 0.00, 0.15,
70517051
0.00, 0.00, 0.00, 0.15,
70527052
};
7053-
char *tok, *save = NULL;
70547053
unsigned i = 0;
70557054
static retro_time_t last_time = 0;
70567055
unsigned list_size = 0;
@@ -7061,6 +7060,8 @@ static void ozone_draw_osk(
70617060
unsigned y_offset = 0;
70627061
bool draw_placeholder = string_is_empty(str);
70637062
retro_time_t current_time = menu_driver_get_current_time();
7063+
const char *line = NULL;
7064+
const char *next = NULL;
70647065

70657066
if (current_time - last_time >= INTERVAL_OSK_CURSOR)
70667067
{
@@ -7163,16 +7164,28 @@ static void ozone_draw_osk(
71637164
ozone->fonts.entries_label.wideglyph_width,
71647165
0);
71657166

7166-
tok = strtok_r(message, "\n", &save);
71677167
list_size = string_count_occurrences_single_character(message, '\n');
71687168

7169-
while (tok)
7169+
for (line = message; line; line = next ? next + 1 : NULL)
71707170
{
7171-
const char *msg = tok;
7171+
char line_buf[2048];
7172+
size_t _len;
7173+
7174+
next = strchr(line, '\n');
7175+
_len = next ? (size_t)(next - line) : strlen(line);
7176+
7177+
if (_len == 0 && !next)
7178+
break;
7179+
7180+
if (_len >= sizeof(line_buf))
7181+
_len = sizeof(line_buf) - 1;
7182+
7183+
memcpy(line_buf, line, _len);
7184+
line_buf[_len] = '\0';
71727185

71737186
gfx_display_draw_text(
71747187
ozone->fonts.entries_label.font,
7175-
msg,
7188+
line_buf,
71767189
margin + (padding * 2),
71777190
margin + padding + ozone->fonts.entries_label.line_height + y_offset,
71787191
video_width,
@@ -7191,7 +7204,7 @@ static void ozone_draw_osk(
71917204
{
71927205
unsigned cursor_x = draw_placeholder
71937206
? 0
7194-
: font_driver_get_message_width(ozone->fonts.entries_label.font, msg, strlen(msg), 1.0f);
7207+
: font_driver_get_message_width(ozone->fonts.entries_label.font, line_buf, _len, 1.0f);
71957208
gfx_display_draw_quad(
71967209
p_disp,
71977210
userdata,
@@ -7217,7 +7230,6 @@ static void ozone_draw_osk(
72177230
else
72187231
y_offset += 25 * scale_factor;
72197232

7220-
tok = strtok_r(NULL, "\n", &save);
72217233
i++;
72227234
}
72237235

tasks/task_overlay.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,18 @@ static void task_overlay_redefine_eightway_direction(
201201
char *str, input_bits_t *data)
202202
{
203203
unsigned bit;
204-
char *tok, *save = NULL;
205-
204+
char *cur = str;
205+
char *next;
206206
BIT256_CLEAR_ALL(*data);
207-
208-
for (tok = strtok_r(str, "|", &save); tok;
209-
tok = strtok_r(NULL, "|", &save))
207+
while (cur && *cur)
210208
{
211-
bit = input_config_translate_str_to_bind_id(tok);
209+
next = strchr(cur, '|');
210+
if (next)
211+
*next++ = '\0';
212+
bit = input_config_translate_str_to_bind_id(cur);
212213
if (bit < RARCH_CUSTOM_BIND_LIST_END)
213214
BIT256_SET(*data, bit);
215+
cur = next;
214216
}
215217
}
216218

@@ -330,7 +332,6 @@ static bool task_overlay_load_desc(
330332
char overlay[256];
331333
unsigned list_size = 0;
332334
char *elems[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
333-
char *tok, *save = NULL;
334335
float tmp_float = 0.0f;
335336
bool tmp_bool = false;
336337
bool by_pixel = false;
@@ -366,10 +367,23 @@ static bool task_overlay_load_desc(
366367

367368
/* Tokenize in-place — overlay[] is a local buffer,
368369
* no heap allocation needed */
369-
for (tok = strtok_r(overlay, ", ", &save);
370-
tok && list_size < 6;
371-
tok = strtok_r(NULL, ", ", &save))
372-
elems[list_size++] = tok;
370+
{
371+
char *p = overlay;
372+
while (*p && list_size < 6)
373+
{
374+
/* skip delimiters */
375+
while (*p == ',' || *p == ' ')
376+
p++;
377+
if (!*p)
378+
break;
379+
elems[list_size++] = p;
380+
/* advance to next delimiter or end */
381+
while (*p && *p != ',' && *p != ' ')
382+
p++;
383+
if (*p)
384+
*p++ = '\0';
385+
}
386+
}
373387

374388
if (list_size < 6)
375389
{
@@ -378,7 +392,7 @@ static bool task_overlay_load_desc(
378392
}
379393

380394
/* elems[0] (key) will be mutated by the button-parsing
381-
* strtok_r below, so read x/y/box from their own pointers
395+
* below, so read x/y/box from their own pointers
382396
* before that happens. They are separate tokens in the
383397
* buffer and won't be touched. */
384398
box = elems[3];
@@ -402,13 +416,19 @@ static bool task_overlay_load_desc(
402416
}
403417
else
404418
{
405-
char *save2 = NULL;
406-
const char *tmp = strtok_r(elems[0], "|", &save2);
419+
const char *tmp;
420+
char *p = elems[0];
407421

408422
desc->type = OVERLAY_TYPE_BUTTONS;
409423

410-
for (; tmp; tmp = strtok_r(NULL, "|", &save2))
424+
while (p)
411425
{
426+
char *delim = strchr(p, '|');
427+
if (delim)
428+
*delim = '\0';
429+
tmp = p;
430+
p = delim ? delim + 1 : NULL;
431+
412432
if (!string_is_equal(tmp, "nul"))
413433
{
414434
unsigned bind_id = input_config_translate_str_to_bind_id(tmp);

0 commit comments

Comments
 (0)