Skip to content

Commit 60ab4ab

Browse files
committed
core-builtin: add and use built in shim wrapper for strrchr
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
1 parent 9a722bf commit 60ab4ab

6 files changed

Lines changed: 41 additions & 3 deletions

File tree

Makefile.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,6 +2644,7 @@ functions: \
26442644
BUILTIN_STRDUP \
26452645
BUILTIN_STRLEN \
26462646
BUILTIN_STRNCMP \
2647+
BUILTIN_STRRCHR \
26472648
BUILTIN_STRSTR \
26482649
BUILTIN_SUPPORTS \
26492650
BUILTIN_TAN \
@@ -3741,6 +3742,9 @@ BUILTIN_STRLEN:
37413742
BUILTIN_STRNCMP:
37423743
$(call check,test-builtin-strncmp,HAVE_BUILTIN_STRNCMP,__builtin_strncmp)
37433744

3745+
BUILTIN_STRRCHR:
3746+
$(call check,test-builtin-strrchr,HAVE_BUILTIN_STRRCHR,__builtin_strrchr)
3747+
37443748
BUILTIN_STRSTR:
37453749
$(call check,test-builtin-strstr,HAVE_BUILTIN_STRSTR,__builtin_strstr)
37463750

core-builtin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
#define shim_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
8686
#endif
8787

88+
#if defined(HAVE_BUILTIN_STRRCHR)
89+
#define shim_strrchr(str, c) __builtin_strrchr((str), (c))
90+
#else
91+
#define shim_strrchr(str, c) strrchr((str), (c))
92+
#endif
93+
8894
#if defined(HAVE_BUILTIN_STRSTR)
8995
#define shim_strstr(haystack, needle) __builtin_strstr((haystack), (needle))
9096
#else

core-cpu-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static int stress_cpu_cache_get_riscv(
327327
int cpu_num;
328328

329329
/* Parse CPU number */
330-
base = strrchr(cpu_path, '/');
330+
base = shim_strrchr(cpu_path, '/');
331331
if (!base)
332332
return 0;
333333
base++;

stress-gpu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
*/
2020
#include "stress-ng.h"
21+
#include "core-builtin.h"
2122
#include "core-out-of-memory.h"
2223
#include "core-pthread.h"
2324
#include "core-signal.h"
@@ -108,7 +109,7 @@ static void stress_get_gpu_freq_mhz(double *gpu_freq)
108109

109110
static void stress_gpu_trim_newline(char *str)
110111
{
111-
char *ptr = strrchr(str, '\n');
112+
char *ptr = shim_strrchr(str, '\n');
112113

113114
if (ptr)
114115
*ptr = '\0';

stress-inode-flags.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static int stress_inode_flags_stressor(
208208
#endif
209209

210210
/* Find basename */
211-
file_name = strrchr(data->file_name, '/');
211+
file_name = shim_strrchr(data->file_name, '/');
212212
if (file_name)
213213
file_name++;
214214

test/test-builtin-strrchr.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2026 Colin Ian King
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*
18+
*/
19+
#include <string.h>
20+
21+
int main(int argc, char **argv)
22+
{
23+
static char str[] = "test";
24+
25+
return (int)(__builtin_strrchr(str, 's') - str);
26+
}
27+

0 commit comments

Comments
 (0)