Add streq(3), and use it to replace confusing uses of strncmp(3)
Subsystems (38)
cover Add streq(3), and use it to replace confusing uses of strncmp(3)
Hi!
This is a spin off from the patch set that adds strprefix()[1]. This patch set replaces some uses of strncmp(3), so that later the strprefix() patch set is easier to apply.
[1]: <https://lore.kernel.org/linux-hardening/[email protected]/T/>
This patch set doesn't replace strcmp(3) with streq(3), since there are thousands of those, and those are less important now. The strncmp(3) ones are the most important, because they can be easily confused with strprefix().
Testing
~~~~~~~
(I've indented the output, to better see the commands.)
$ git log --oneline --graph v7.4-rc1^..streq1 * 7994b5827030 (HEAD -> streq, tag: streq1) treewide: Use streq(3) instead of unnecessary uses of strncmp(3) * b93e0d005739 treewide: Use streq(3) instead of confusing uses of strncmp(3) * 1715e2c9c8ea tools/: Add streq() to nolibc/string * ad8500a6eb0a string: Add streq() * 6628e7d4c6b2 (tag: v7.4-rc1, master) Linux 7.4-rc1 $ cp ../kunitconfig .kunitconfig $ cat .kunitconfig CONFIG_KUNIT=y CONFIG_STRING_KUNIT_TEST=y $ mkdir -p ~/tmp/streq1 $ export TMPDIR=$HOME/tmp/streq1/ $ ./tools/testing/kunit/kunit.py run "string*" [18:45:10] Configuring KUnit Kernel ... Generating .config ... Populating config with: $ make ARCH=um O=.kunit olddefconfig [18:45:14] Building KUnit Kernel ... Populating config with: $ make ARCH=um O=.kunit olddefconfig Building with: $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=24 [18:45:30] Starting KUnit Kernel (1/1)... [18:45:30] ============================================================ Running tests with: $ .kunit/linux 'kunit.filter_glob=string*' kunit.enable=1 mem=1G console=tty kunit_shutdown=halt [18:45:30] ============= string-stream-test (12 subtests) ============= [18:45:30] [PASSED] string_stream_managed_init_test [18:45:30] [PASSED] string_stream_unmanaged_init_test [18:45:30] [PASSED] string_stream_managed_free_test [18:45:30] [PASSED] string_stream_resource_free_test [18:45:30] [PASSED] string_stream_line_add_test [18:45:30] [PASSED] string_stream_variable_length_line_test [18:45:30] [PASSED] string_stream_append_test [18:45:30] [PASSED] string_stream_append_auto_newline_test [18:45:30] [PASSED] string_stream_append_empty_string_test [18:45:30] [PASSED] string_stream_no_auto_newline_test [18:45:30] [PASSED] string_stream_auto_newline_test [18:45:30] [PASSED] string_stream_performance_test [18:45:30] =============== [PASSED] string-stream-test ================ [18:45:30] =================== string (29 subtests) =================== [18:45:30] [PASSED] string_test_memset16 [18:45:30] [PASSED] string_test_memset32 [18:45:30] [PASSED] string_test_memset64 [18:45:30] [PASSED] string_test_strlen [18:45:30] [PASSED] string_test_strnlen [18:45:30] [PASSED] string_test_strchr [18:45:30] [PASSED] string_test_strnchr [18:45:30] [PASSED] string_test_strrchr [18:45:30] [PASSED] string_test_strspn [18:45:30] [PASSED] string_test_streq [18:45:30] [PASSED] string_test_strcmp [18:45:30] [PASSED] string_test_strcmp_long_strings [18:45:30] [PASSED] string_test_strncmp [18:45:30] [PASSED] string_test_strncmp_long_strings [18:45:30] [PASSED] string_test_strcasecmp [18:45:30] [PASSED] string_test_strcasecmp_long_strings [18:45:30] [PASSED] string_test_strncasecmp [18:45:30] [PASSED] string_test_strncasecmp_long_strings [18:45:30] [PASSED] string_test_strscpy [18:45:30] [PASSED] string_test_strcat [18:45:30] [PASSED] string_test_strncat [18:45:30] [PASSED] string_test_strlcat [18:45:30] [PASSED] string_test_strtomem [18:45:30] [PASSED] string_test_memtostr [18:45:30] [PASSED] string_test_strends [18:45:30] [SKIPPED] string_bench_strlen (not enabled) [18:45:30] [SKIPPED] string_bench_strnlen (not enabled) [18:45:30] [SKIPPED] string_bench_strchr (not enabled) [18:45:30] [SKIPPED] string_bench_strrchr (not enabled) [18:45:30] ===================== [PASSED] string ====================== [18:45:30] =============== string_helpers (4 subtests) ================ [18:45:30] [PASSED] test_get_size [18:45:30] [PASSED] test_upper_lower [18:45:30] [PASSED] test_unescape [18:45:30] [PASSED] test_escape [18:45:30] ================= [PASSED] string_helpers ================== [18:45:30] ============================================================ [18:45:30] Testing complete. Ran 45 tests: passed: 41, skipped: 4 [18:45:30] Elapsed time: 19.736s total, 3.261s configuring, 16.309s building, 0.162s running
$ make allmodconfig HOSTCC scripts/basic/fixdep HOSTCC scripts/kconfig/conf.o HOSTCC scripts/kconfig/confdata.o HOSTCC scripts/kconfig/expr.o LEX scripts/kconfig/lexer.lex.c YACC scripts/kconfig/parser.tab.[ch] HOSTCC scripts/kconfig/lexer.lex.o HOSTCC scripts/kconfig/menu.o HOSTCC scripts/kconfig/parser.tab.o HOSTCC scripts/kconfig/preprocess.o HOSTCC scripts/kconfig/symbol.o HOSTCC scripts/kconfig/util.o HOSTLD scripts/kconfig/conf # # configuration written to .config # $ time make -j24 | ts -s 00:00:00 GEN arch/x86/include/generated/asm/orc_hash.h 00:00:00 UPD arch/x86/include/generated/uapi/asm/bpf_perf_event.h 00:00:00 UPD arch/x86/include/generated/uapi/asm/errno.h ... 00:20:03 LD [M] net/qrtr/qrtr-tun.ko 00:20:03 LD [M] net/qrtr/qrtr-mhi.ko 00:20:03 LD [M] virt/lib/irqbypass.ko
real 20m3.221s user 425m2.307s sys 25m40.226s
Have a lovely night!
Alex
Alejandro Colomar (4):
string: Add streq()
tools/: Add streq() to nolibc/string
treewide: Use streq(3) instead of confusing uses of strncmp(3)
treewide: Use streq(3) instead of unnecessary uses of strncmp(3)
arch/arm/mach-omap2/sr_device.c | 8 ++++---- arch/m68k/sun3/prom/console.c | 6 +++--- arch/mips/bcm47xx/time.c | 2 +- arch/powerpc/kernel/security.c | 2 +- drivers/clk/socfpga/clk-gate-a10.c | 2 -- drivers/clk/socfpga/clk.h | 1 - drivers/edac/amd64_edac.c | 2 +- drivers/edac/i10nm_base.c | 2 +- drivers/edac/igen6_edac.c | 2 +- drivers/edac/imh_base.c | 2 +- drivers/edac/pnd2_edac.c | 2 +- drivers/edac/sb_edac.c | 2 +- drivers/edac/skx_base.c | 2 +- drivers/gpu/drm/tilcdc/tilcdc_panel_legacy.c | 2 +- drivers/net/ethernet/intel/i40e/i40e_ddp.c | 2 +- fs/ocfs2/filecheck.c | 6 +++--- include/linux/remoteproc/pruss.h | 2 +- include/linux/string.h | 12 ++++++++++++ lib/tests/string_kunit.c | 13 +++++++++++++ net/netfilter/xt_set.c | 2 +- sound/hda/codecs/side-codecs/tas2781_hda_i2c.c | 2 +- tools/bpf/bpftool/btf.c | 4 ++-- tools/include/nolibc/string.h | 2 ++ tools/perf/util/callchain.c | 8 ++++---- tools/perf/util/evsel.c | 3 +-- tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 8 ++++---- tools/testing/selftests/kvm/steal_time.c | 2 +- tools/testing/selftests/kvm/x86/xen_shinfo_test.c | 4 ++-- tools/testing/selftests/proc/proc-subset-pid.c | 5 ----- tools/testing/selftests/proc/proc.h | 5 ----- 30 files changed, 65 insertions(+), 52 deletions(-)
Range-diff against v0:
-: ------------ > 1: ad8500a6eb0a string: Add streq() -: ------------ > 2: 1715e2c9c8ea tools/: Add streq() to nolibc/string -: ------------ > 3: b93e0d005739 treewide: Use streq(3) instead of confusing uses of strncmp(3) -: ------------ > 4: 7994b5827030 treewide: Use streq(3) instead of unnecessary uses of strncmp(3)
Signature · 1 line
2.55.0
PATCH 1/4 string: Add streq()
The return value of strcmp(3) being reversed makes it prone to mistakes. Since most of the time we're only interested in simple equality, add this API for those cases.
This removes some driver-specific definitions of streq(3).
--- drivers/clk/socfpga/clk-gate-a10.c | 2 -- drivers/clk/socfpga/clk.h | 1 - include/linux/string.h | 12 ++++++++++++ lib/tests/string_kunit.c | 13 +++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-)
drivers/clk/socfpga/clk-gate-a10.c +0 -2
@@ -11,8 +11,6 @@#include "clk.h" -#define streq (a, b) (strcmp((a), (b)) == 0) -#define to_socfpga_gate_clk (p) container_of(p, struct socfpga_gate_clk, hw.hw)/* SDMMC Group for System Manager defines */
drivers/clk/socfpga/clk.h +0 -1
@@ -19,7 +19,6 @@#define SOCFPGA_MAX_PARENTS 5 -#define streq (a, b) (strcmp((a), (b)) == 0)#define SYSMGR_SDMMC_CTRL_SET (smplsel, drvsel) \ ((((smplsel) & 0x7) << 3) | (((drvsel) & 0x7) << 0))
include/linux/string.h +12 -0
@@ -14,6 +14,18 @@#include <linux/stdarg.h> #include <uapi/linux/string.h> +/** +* streq - strings equal +* +* @s1: string to compare +* @s2: string to compare +* +* Compare two strings for equality. +* +* Return: true iff the strings are equal +*/ +#define streq (s1, s2) (strcmp(s1, s2) == 0) +extern char *strndup_user (const char __user *,long );extern void *memdup_user (const void __user *,size_t )__realloc_size (2 );extern void *vmemdup_user (const void __user *,size_t )__realloc_size (2 );
lib/tests/string_kunit.c +13 -0
@@ -287,6 +287,18 @@ static void string_test_strspn(struct kunit *test) } } +static void string_test_streq (struct kunit *test) +{ +KUNIT_ASSERT_TRUE (test,streq ("foo" ,"foo" )); +KUNIT_ASSERT_TRUE (test,streq ("" ,"" )); + +KUNIT_ASSERT_FALSE (test,streq ("foo" ,"foobar" )); +KUNIT_ASSERT_FALSE (test,streq ("foobar" ,"foo" )); +KUNIT_ASSERT_FALSE (test,streq ("foo" ,"bar" )); +KUNIT_ASSERT_FALSE (test,streq ("foo" ,"" )); +KUNIT_ASSERT_FALSE (test,streq ("" ,"bar" )); +} +static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN ];static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN ];
@@ -891,6 +903,7 @@ static struct kunit_case string_test_cases[] = {KUNIT_CASE (string_test_strnchr),KUNIT_CASE (string_test_strrchr),KUNIT_CASE (string_test_strspn), +KUNIT_CASE (string_test_streq),KUNIT_CASE (string_test_strcmp),KUNIT_CASE (string_test_strcmp_long_strings), KUNIT_CASE(string_test_strncmp),
Signature · 1 line
2.55.0
PATCH 2/4 tools/: Add streq() to nolibc/string
In reply to Alejandro ColomarIt's more readable than strcmp(3).
This de-duplicates the existing definitions of streq().
--- tools/include/nolibc/string.h | 2 ++ tools/testing/selftests/proc/proc-subset-pid.c | 5 ----- tools/testing/selftests/proc/proc.h | 5 ----- 3 files changed, 2 insertions(+), 10 deletions(-)
tools/include/nolibc/string.h +2 -0
@@ -130,6 +130,8 @@ int strcmp(const char *a, const char *b)return diff; } +#define streq (a, b) (strcmp(a, b) == 0) +static __attribute__((unused))char *strcpy (char *dst,const char *src) {
tools/testing/selftests/proc/proc-subset-pid.c +0 -5
@@ -32,11 +32,6 @@#include <unistd.h> #include <stdio.h> -static inline bool streq (const char *a,const char *b) -{ -return strcmp (a, b) ==0 ; -} -static void make_private_proc (void ) {if (unshare (CLONE_NEWNS ) ==-1 ) {
tools/testing/selftests/proc/proc.h +0 -5
@@ -19,11 +19,6 @@ static inline pid_t sys_gettid(void)return syscall (SYS_gettid); } -static inline bool streq (const char *s1,const char *s2) -{ -return strcmp (s1, s2) ==0 ; -} -static unsigned long long xstrtoull (const char *p,char **end) {if (*p =='0' ) {
Signature · 1 line
2.55.0
PATCH 3/4 treewide: Use streq(3) instead of confusing uses of strncmp(3)
In reply to Alejandro ColomarUses of strncmp(a, b, N) where N==strlen(b)+1 are really the same as strcmp(a,b). It's hard to distinguish whether the strcmp(3) behavior was intentional, or it strstarts() behavior was intended.
This code seems to have been confused, and probably thought using strncmp(3) was somehow safer than strcmp(3), but it is not.
Let's rewrite with streq(3), which makes it more readable. This patch is not changing the behavior of the existing code. If any of these was actually a bug and intended to write strstarts(), that should be fixed in a separate patch.
These cases were found with the following script:
$ seq 20 \
| while read n; do
git grep -E 'strncmp\([^,]*, "[^"]{'"$n"'}", '"$(expr 1 + $n)"'\)';
done;--- arch/arm/mach-omap2/sr_device.c | 8 ++++---- arch/mips/bcm47xx/time.c | 2 +- arch/powerpc/kernel/security.c | 2 +- drivers/net/ethernet/intel/i40e/i40e_ddp.c | 2 +- fs/ocfs2/filecheck.c | 6 +++--- net/netfilter/xt_set.c | 2 +- tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 8 ++++---- tools/testing/selftests/kvm/steal_time.c | 2 +- tools/testing/selftests/kvm/x86/xen_shinfo_test.c | 4 ++-- 9 files changed, 18 insertions(+), 18 deletions(-)
arch/arm/mach-omap2/sr_device.c +4 -4
@@ -92,12 +92,12 @@ static int __init sr_init_by_name(const char *name, const char *voltdm)struct omap_volt_data *volt_data;static int i; -if (!strncmp (name,"smartreflex_mpu_iva" ,20 ) || - !strncmp (name,"smartreflex_mpu" ,16 )) +if (streq (name,"smartreflex_mpu_iva" ) || +streq (name,"smartreflex_mpu" )) sr_data = &omap_sr_pdata[OMAP_SR_MPU ]; -else if (!strncmp (name,"smartreflex_core" ,17 )) +else if (streq (name,"smartreflex_core" )) sr_data = &omap_sr_pdata[OMAP_SR_CORE ]; -else if (!strncmp (name,"smartreflex_iva" ,16 )) +else if (streq (name,"smartreflex_iva" )) sr_data = &omap_sr_pdata[OMAP_SR_IVA ];if (!sr_data) {
arch/mips/bcm47xx/time.c +1 -1
@@ -60,7 +60,7 @@ void __init plat_time_init(void)if (chip_id ==0x5354 ) { len =bcm47xx_nvram_getenv ("clkfreq" , buf,sizeof (buf)); -if (len >=0 && !strncmp (buf,"200" ,4 )) +if (len >=0 &&streq (buf,"200" )) hz =100000000 ; }
arch/powerpc/kernel/security.c +1 -1
@@ -273,7 +273,7 @@ enum stf_barrier_type stf_barrier_type_get(void)/* This is the generic flag used by other architectures */ static int __init handle_ssbd (char *p) { -if (!p ||strncmp (p,"auto" ,5 ) ==0 ||strncmp (p,"on" ,2 ) ==0 ) { +if (!p ||streq (p,"auto" ) ||strncmp (p,"on" ,2 ) ==0 ) {/* Until firmware tells us, we have the barrier with auto */ return 0 ; }else if (strncmp (p,"off" ,3 ) ==0 ) {
drivers/net/ethernet/intel/i40e/i40e_ddp.c +1 -1
@@ -452,7 +452,7 @@ int i40e_ddp_flash(struct net_device *netdev, struct ethtool_flash *flash)/* If the user supplied "-" instead of file name rollback previously * stored profile. */ -if (strncmp (flash->data,"-" ,2 ) !=0 ) { +if (!streq (flash->data,"-" )) {struct i40e_ddp_old_profile_list *list_entry;char profile_name[sizeof (I40E_DDP_PROFILE_PATH ) +I40E_DDP_PROFILE_NAME_MAX ];
fs/ocfs2/filecheck.c +3 -3
@@ -258,11 +258,11 @@ ocfs2_filecheck_args_get_long(const char *buf, size_t count,static int ocfs2_filecheck_type_parse (const char *name,unsigned int *type) { -if (!strncmp (name,"fix" ,4 )) +if (streq (name,"fix" )) *type =OCFS2_FILECHECK_TYPE_FIX ; -else if (!strncmp (name,"check" ,6 )) +else if (streq (name,"check" )) *type =OCFS2_FILECHECK_TYPE_CHK ; -else if (!strncmp (name,"set" ,4 )) +else if (streq (name,"set" )) *type =OCFS2_FILECHECK_TYPE_SET ;else return 1 ;
net/netfilter/xt_set.c +1 -1
@@ -436,7 +436,7 @@ set_target_v3_check_hooks(const struct xt_tgchk_param *par)const struct xt_set_info_target_v3 *info = par->targinfo;if (info->map_set.index !=IPSET_INVALID_ID ) { -if (strncmp (par->table,"mangle" ,7 )) { +if (!streq (par->table,"mangle" )) {pr_info_ratelimited ("--map-set only usable from mangle table\n" );return -EINVAL ; }
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +4 -4
@@ -507,9 +507,9 @@ static int numer_of_heaps(void)int heaps =0 ;while ((dir =readdir (d))) { -if (!strncmp (dir->d_name,"." ,2 )) +if (streq (dir->d_name,"." ))continue ; -if (!strncmp (dir->d_name,".." ,3 )) +if (streq (dir->d_name,".." ))continue ; heaps++; }
@@ -533,9 +533,9 @@ int main(void)ksft_set_plan (12 *numer_of_heaps ());while ((dir =readdir (d))) { -if (!strncmp (dir->d_name,"." ,2 )) +if (streq (dir->d_name,"." ))continue ; -if (!strncmp (dir->d_name,".." ,3 )) +if (streq (dir->d_name,".." ))continue ;ksft_print_msg ("Testing heap: %s\n" , dir->d_name);
tools/testing/selftests/kvm/steal_time.c +1 -1
@@ -526,7 +526,7 @@ int main(int ac, char **av)bool verbose;int i, cpu; - verbose = ac >1 && (!strncmp (av[1 ],"-v" ,3 ) || !strncmp (av[1 ],"--verbose" ,10 )); + verbose = ac >1 && (streq (av[1 ],"-v" ) ||streq (av[1 ],"--verbose" ));/* Set CPU affinity so we can force preemption of the VCPU */ cpu =pin_self_to_any_cpu ();
tools/testing/selftests/kvm/x86/xen_shinfo_test.c +2 -2
@@ -438,8 +438,8 @@ int main(int argc, char *argv[])pthread_t thread;bool verbose; - verbose = argc >1 && (!strncmp (argv[1 ],"-v" ,3 ) || - !strncmp (argv[1 ],"--verbose" ,10 )); + verbose = argc >1 && (streq (argv[1 ],"-v" ) || +streq (argv[1 ],"--verbose" ));int xen_caps =kvm_check_cap (KVM_CAP_XEN_HVM );TEST_REQUIRE (xen_caps &KVM_XEN_HVM_CONFIG_SHARED_INFO );
Signature · 1 line
2.55.0
PATCH 4/4 treewide: Use streq(3) instead of unnecessary uses of strncmp(3)
In reply to Alejandro ColomarUses of strncmp(foo,"bar",sizeof("bar")) are really the same as strcmp(foo,"bar"). There's no good reason to use strncmp(3).
This code seems to have been confused, and probably thought using strncmp(3) was somehow safer than strcmp(3), but it is not.
Let's rewrite with streq(3), which makes it more readable. This patch is not changing the behavior of the existing code.
These cases were found with the following script:
$ git grep -E 'strncmp\([^,]*, [^,]*, sizeof.*[A-Z"]' \ | grep -v -- '- 1';
--- arch/m68k/sun3/prom/console.c | 6 +++--- drivers/edac/amd64_edac.c | 2 +- drivers/edac/i10nm_base.c | 2 +- drivers/edac/igen6_edac.c | 2 +- drivers/edac/imh_base.c | 2 +- drivers/edac/pnd2_edac.c | 2 +- drivers/edac/sb_edac.c | 2 +- drivers/edac/skx_base.c | 2 +- drivers/gpu/drm/tilcdc/tilcdc_panel_legacy.c | 2 +- include/linux/remoteproc/pruss.h | 2 +- sound/hda/codecs/side-codecs/tas2781_hda_i2c.c | 2 +- tools/bpf/bpftool/btf.c | 4 ++-- tools/perf/util/callchain.c | 8 ++++---- tools/perf/util/evsel.c | 3 +-- 14 files changed, 20 insertions(+), 21 deletions(-)
arch/m68k/sun3/prom/console.c +3 -3
@@ -92,7 +92,7 @@ prom_query_input_device()if (prom_node_has_property (st_p,"keyboard" ))return PROMDEV_IKBD ;prom_getproperty (st_p,"device_type" , propb,sizeof (propb)); -if (strncmp (propb,"serial" ,sizeof ("serial" ))) +if (!streq (propb,"serial" ))return PROMDEV_I_UNK ;prom_getproperty (prom_root_node,"stdin-path" , propb,sizeof (propb)); p = propb;
@@ -139,12 +139,12 @@ prom_query_output_device()local_irq_restore (flags); propl =prom_getproperty (st_p,"device_type" , propb,sizeof (propb));if (propl >=0 && propl ==sizeof ("display" ) && -strncmp ("display" , propb,sizeof ("display" )) ==0 ) +streq ("display" , propb)) {return PROMDEV_OSCREEN ; }if (prom_vers ==PROM_V3 ) { -if (strncmp ("serial" , propb,sizeof ("serial" ))) +if (!streq ("serial" , propb))return PROMDEV_O_UNK ;prom_getproperty (prom_root_node,"stdout-path" , propb,sizeof (propb)); p = propb;
drivers/edac/amd64_edac.c +1 -1
@@ -4137,7 +4137,7 @@ static int __init amd64_edac_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ;if (!x86_match_cpu (amd64_cpuids))
drivers/edac/i10nm_base.c +1 -1
@@ -938,7 +938,7 @@ static int __init i10nm_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ;if (cpu_feature_enabled (X86_FEATURE_HYPERVISOR ))
drivers/edac/igen6_edac.c +1 -1
@@ -1990,7 +1990,7 @@ static int __init igen6_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ; rc =pci_register_driver (&igen6_driver);
drivers/edac/imh_base.c +1 -1
@@ -543,7 +543,7 @@ static int __init imh_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ;if (cpu_feature_enabled (X86_FEATURE_HYPERVISOR ))
drivers/edac/pnd2_edac.c +1 -1
@@ -1528,7 +1528,7 @@ static int __init pnd2_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ;if (cpu_feature_enabled (X86_FEATURE_HYPERVISOR ))
drivers/edac/sb_edac.c +1 -1
@@ -3629,7 +3629,7 @@ static int __init sbridge_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ;if (cpu_feature_enabled (X86_FEATURE_HYPERVISOR ))
drivers/edac/skx_base.c +1 -1
@@ -612,7 +612,7 @@ static int __init skx_init(void)return -EBUSY ; owner =edac_get_owner (); -if (owner &&strncmp (owner,EDAC_MOD_STR ,sizeof (EDAC_MOD_STR ))) +if (owner && !streq (owner,EDAC_MOD_STR ))return -EBUSY ;if (cpu_feature_enabled (X86_FEATURE_HYPERVISOR ))
drivers/gpu/drm/tilcdc/tilcdc_panel_legacy.c +1 -1
@@ -74,7 +74,7 @@ static int __init tilcdc_panel_copy_props(struct device_node *old_panel,/* Copy all panel properties to the new panel node */ for_each_property_of_node (old_panel, prop) { -if (!strncmp (prop->name,"compatible" ,sizeof ("compatible" ))) +if (streq (prop->name,"compatible" ))continue ; ret = tilcdc_panel_update_prop(&ocs, new_panel, prop->name,
include/linux/remoteproc/pruss.h +1 -1
@@ -74,7 +74,7 @@ static inline bool is_pru_rproc(struct device *dev) {const char *drv_name =dev_driver_string (dev); -if (strncmp (drv_name,PRU_RPROC_DRVNAME ,sizeof (PRU_RPROC_DRVNAME ))) +if (!streq (drv_name,PRU_RPROC_DRVNAME ))return false;return true;
sound/hda/codecs/side-codecs/tas2781_hda_i2c.c +1 -1
@@ -109,7 +109,7 @@ static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid) sub =acpi_get_subsystem_id (ACPI_HANDLE (physdev));if (IS_ERR (sub)) {/* No subsys id in older tas2563 projects. */ -if (!strncmp (hid,"INT8866" ,sizeof ("INT8866" ))) { +if (streq (hid,"INT8866" )) { p->speaker_id =-1 ; goto end_2563; }
tools/bpf/bpftool/btf.c +2 -2
@@ -860,13 +860,13 @@ static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf)continue ; name =btf__name_by_offset (btf, t->name_off); -if (strncmp (name,KFUNC_DECL_TAG ,sizeof (KFUNC_DECL_TAG )) ==0 ) { +if (streq (name,KFUNC_DECL_TAG )) { err =ptr_array_push (ft, &kfuncs);if (err) goto out; } -if (strncmp (name,FASTCALL_DECL_TAG ,sizeof (FASTCALL_DECL_TAG )) ==0 ) { +if (streq (name,FASTCALL_DECL_TAG )) { err =ptr_array_push (ft, &fastcalls);if (err) goto out;
tools/perf/util/callchain.c +4 -4
@@ -277,7 +277,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param) do { /* Framepointer style */ - if (!strncmp(name, "fp", sizeof("fp"))) { + if (streq(name, "fp")) { ret = 0; param->record_mode = CALLCHAIN_FP;
@@ -285,7 +285,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)if (tok) {unsigned long size; -if (!strncmp (tok,"defer" ,sizeof ("defer" ))) { +if (streq (tok,"defer" )) { param->defer = true; }else { size =strtoul (tok, &name,0 );
@@ -296,7 +296,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)break ;/* Dwarf style */ - }else if (!strncmp (name,"dwarf" ,sizeof ("dwarf" ))) { + }else if (streq (name,"dwarf" )) {const unsigned long default_stack_dump_size =8192 ; ret =0 ;
@@ -311,7 +311,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param) ret =get_stack_size (tok, &size); param->dump_size = size; } - }else if (!strncmp (name,"lbr" ,sizeof ("lbr" ))) { + }else if (streq (name,"lbr" )) {if (!strtok_r (NULL ,"," , &saveptr)) { param->record_mode =CALLCHAIN_LBR ; ret =0 ;
tools/perf/util/evsel.c +1 -2
@@ -445,8 +445,7 @@ bool evsel__is_function_event(struct evsel *evsel) {#define FUNCTION_EVENT "ftrace:function" -return evsel->name && - !strncmp (FUNCTION_EVENT , evsel->name,sizeof (FUNCTION_EVENT )); +return evsel->name &&streq (FUNCTION_EVENT , evsel->name);#undef FUNCTION_EVENT }
Signature · 1 line
2.55.0
Re: [PATCH v1 4/4] treewide: Use streq(3) instead of unnecessary uses of strncmp(3)
Quoted 13 lines · Alejandro Colomar
Uses of strncmp(foo,"bar",sizeof("bar")) are really the same as
strcmp(foo,"bar"). There's no good reason to use strncmp(3).This code seems to have been confused, and probably thought using strncmp(3) was somehow safer than strcmp(3), but it is not.
Let's rewrite with streq(3), which makes it more readable. This patch is not changing the behavior of the existing code.
These cases were found with the following script:
$ git grep -E 'strncmp\([^,]*, [^,]*, sizeof.*[A-Z"]' \ | grep -v -- '- 1';
...
Quoted 1 line
arch/m68k/sun3/prom/console.c | 6 +++---
I hope you compile-tested this at least. IIRC PROM is early boot stage on m68k and PPC.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 4/4] treewide: Use streq(3) instead of unnecessary uses of strncmp(3)
Hi Andy,
Quoted 18 lines
Date: 2026-11-16 14:25:28+0200
From: Andy Shevchenko <[email protected]>Quoted 6 lines · Alejandro Colomar
Uses of strncmp(foo,"bar",sizeof("bar")) are really the same as
strcmp(foo,"bar"). There's no good reason to use strncmp(3).This code seems to have been confused, and probably thought using strncmp(3) was somehow safer than strcmp(3), but it is not.
Quoted 7 lines
Let's rewrite with streq(3), which makes it more readable. This patch is not changing the behavior of the existing code.
These cases were found with the following script:
$ git grep -E 'strncmp\([^,]*, [^,]*, sizeof.*[A-Z"]' \ | grep -v -- '- 1';
Quoted 6 lines
...
Quoted 1 line
arch/m68k/sun3/prom/console.c | 6 +++---I hope you compile-tested this at least. IIRC PROM is early boot stage on m68k and PPC.
Yes, but never trust my word for that, as I make mistakes. :) That's why I try to explicitly show the testing shell session in the cover letter or in a reply to it. In this case, the cover letter shows the full testing I did, including a full build with 'allmodconfig'.
Cheers,
Alex
Quoted 6 lines
Signature · 3 lines
-- With Best Regards, Andy Shevchenko
Signature · 1 line
<https://www.alejandro-colomar.es>
Hi!
Just a gentle ping on this patch. It didn't seem to get any feedback, which I take as something good (no important issues with it, I guess), since the patch is quite simple.
I've never done a tree-wide change like this, so I don't know if it's okay as is, or if I should split the tree-wide patch into small patches for each subsystem (and where are the boundaries). Any advice?
Have a lovely day!
Alex
Quoted 183 lines
Date: 2026-11-15 19:46:10+0100
From: Alejandro Colomar <[email protected]>Hi!
This is a spin off from the patch set that adds strprefix()[1]. This patch set replaces some uses of strncmp(3), so that later the
strprefix()patch set is easier to apply.[1]: <https://lore.kernel.org/linux-hardening/[email protected]/T/>
This patch set doesn't replace strcmp(3) with streq(3), since there are thousands of those, and those are less important now. The strncmp(3) ones are the most important, because they can be easily confused with
strprefix().Testing
~~~~~~~(I've indented the output, to better see the commands.)
$ git log --oneline --graph v7.4-rc1^..streq1 * 7994b5827030 (HEAD -> streq, tag: streq1) treewide: Use streq(3) instead of unnecessary uses of strncmp(3) * b93e0d005739 treewide: Use streq(3) instead of confusing uses of strncmp(3) * 1715e2c9c8ea tools/: Add streq() to nolibc/string * ad8500a6eb0a string: Add streq() * 6628e7d4c6b2 (tag: v7.4-rc1, master) Linux 7.4-rc1 $ cp ../kunitconfig .kunitconfig $ cat .kunitconfig CONFIG_KUNIT=y CONFIG_STRING_KUNIT_TEST=y $ mkdir -p ~/tmp/streq1 $ export TMPDIR=$HOME/tmp/streq1/ $ ./tools/testing/kunit/kunit.py run "string*" [18:45:10] Configuring KUnit Kernel ... Generating .config ... Populating config with: $ make ARCH=um O=.kunit olddefconfig [18:45:14] Building KUnit Kernel ... Populating config with: $ make ARCH=um O=.kunit olddefconfig Building with: $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=24 [18:45:30] Starting KUnit Kernel (1/1)... [18:45:30] ============================================================ Running tests with: $ .kunit/linux 'kunit.filter_glob=string*' kunit.enable=1 mem=1G console=tty kunit_shutdown=halt [18:45:30] ============= string-stream-test (12 subtests) ============= [18:45:30] [PASSED] string_stream_managed_init_test [18:45:30] [PASSED] string_stream_unmanaged_init_test [18:45:30] [PASSED] string_stream_managed_free_test [18:45:30] [PASSED] string_stream_resource_free_test [18:45:30] [PASSED] string_stream_line_add_test [18:45:30] [PASSED] string_stream_variable_length_line_test [18:45:30] [PASSED] string_stream_append_test [18:45:30] [PASSED] string_stream_append_auto_newline_test [18:45:30] [PASSED] string_stream_append_empty_string_test [18:45:30] [PASSED] string_stream_no_auto_newline_test [18:45:30] [PASSED] string_stream_auto_newline_test [18:45:30] [PASSED] string_stream_performance_test [18:45:30] =============== [PASSED] string-stream-test ================ [18:45:30] =================== string (29 subtests) =================== [18:45:30] [PASSED] string_test_memset16 [18:45:30] [PASSED] string_test_memset32 [18:45:30] [PASSED] string_test_memset64 [18:45:30] [PASSED] string_test_strlen [18:45:30] [PASSED] string_test_strnlen [18:45:30] [PASSED] string_test_strchr [18:45:30] [PASSED] string_test_strnchr [18:45:30] [PASSED] string_test_strrchr [18:45:30] [PASSED] string_test_strspn [18:45:30] [PASSED] string_test_streq [18:45:30] [PASSED] string_test_strcmp [18:45:30] [PASSED] string_test_strcmp_long_strings [18:45:30] [PASSED] string_test_strncmp [18:45:30] [PASSED] string_test_strncmp_long_strings [18:45:30] [PASSED] string_test_strcasecmp [18:45:30] [PASSED] string_test_strcasecmp_long_strings [18:45:30] [PASSED] string_test_strncasecmp [18:45:30] [PASSED] string_test_strncasecmp_long_strings [18:45:30] [PASSED] string_test_strscpy [18:45:30] [PASSED] string_test_strcat [18:45:30] [PASSED] string_test_strncat [18:45:30] [PASSED] string_test_strlcat [18:45:30] [PASSED] string_test_strtomem [18:45:30] [PASSED] string_test_memtostr [18:45:30] [PASSED] string_test_strends [18:45:30] [SKIPPED] string_bench_strlen (not enabled) [18:45:30] [SKIPPED] string_bench_strnlen (not enabled) [18:45:30] [SKIPPED] string_bench_strchr (not enabled) [18:45:30] [SKIPPED] string_bench_strrchr (not enabled) [18:45:30] ===================== [PASSED] string ====================== [18:45:30] =============== string_helpers (4 subtests) ================ [18:45:30] [PASSED] test_get_size [18:45:30] [PASSED] test_upper_lower [18:45:30] [PASSED] test_unescape [18:45:30] [PASSED] test_escape [18:45:30] ================= [PASSED] string_helpers ================== [18:45:30] ============================================================ [18:45:30] Testing complete. Ran 45 tests: passed: 41, skipped: 4 [18:45:30] Elapsed time: 19.736s total, 3.261s configuring, 16.309s building, 0.162s running$ make allmodconfig HOSTCC scripts/basic/fixdep HOSTCC scripts/kconfig/conf.o HOSTCC scripts/kconfig/confdata.o HOSTCC scripts/kconfig/expr.o LEX scripts/kconfig/lexer.lex.c YACC scripts/kconfig/parser.tab.[ch] HOSTCC scripts/kconfig/lexer.lex.o HOSTCC scripts/kconfig/menu.o HOSTCC scripts/kconfig/parser.tab.o HOSTCC scripts/kconfig/preprocess.o HOSTCC scripts/kconfig/symbol.o HOSTCC scripts/kconfig/util.o HOSTLD scripts/kconfig/conf # # configuration written to .config # $ time make -j24 | ts -s 00:00:00 GEN arch/x86/include/generated/asm/orc_hash.h 00:00:00 UPD arch/x86/include/generated/uapi/asm/bpf_perf_event.h 00:00:00 UPD arch/x86/include/generated/uapi/asm/errno.h ... 00:20:03 LD [M] net/qrtr/qrtr-tun.ko 00:20:03 LD [M] net/qrtr/qrtr-mhi.ko 00:20:03 LD [M] virt/lib/irqbypass.koreal 20m3.221s user 425m2.307s sys 25m40.226sHave a lovely night!
AlexAlejandro Colomar (4):
string: Addstreq()
tools/: Addstreq()to nolibc/string
treewide: Use streq(3) instead of confusing uses of strncmp(3)
treewide: Use streq(3) instead of unnecessary uses of strncmp(3)arch/arm/mach-omap2/sr_device.c | 8 ++++---- arch/m68k/sun3/prom/console.c | 6 +++--- arch/mips/bcm47xx/time.c | 2 +- arch/powerpc/kernel/security.c | 2 +- drivers/clk/socfpga/clk-gate-a10.c | 2 -- drivers/clk/socfpga/clk.h | 1 - drivers/edac/amd64_edac.c | 2 +- drivers/edac/i10nm_base.c | 2 +- drivers/edac/igen6_edac.c | 2 +- drivers/edac/imh_base.c | 2 +- drivers/edac/pnd2_edac.c | 2 +- drivers/edac/sb_edac.c | 2 +- drivers/edac/skx_base.c | 2 +- drivers/gpu/drm/tilcdc/tilcdc_panel_legacy.c | 2 +- drivers/net/ethernet/intel/i40e/i40e_ddp.c | 2 +- fs/ocfs2/filecheck.c | 6 +++--- include/linux/remoteproc/pruss.h | 2 +- include/linux/string.h | 12 ++++++++++++ lib/tests/string_kunit.c | 13 +++++++++++++ net/netfilter/xt_set.c | 2 +- sound/hda/codecs/side-codecs/tas2781_hda_i2c.c | 2 +- tools/bpf/bpftool/btf.c | 4 ++-- tools/include/nolibc/string.h | 2 ++ tools/perf/util/callchain.c | 8 ++++---- tools/perf/util/evsel.c | 3 +-- tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 8 ++++---- tools/testing/selftests/kvm/steal_time.c | 2 +- tools/testing/selftests/kvm/x86/xen_shinfo_test.c | 4 ++-- tools/testing/selftests/proc/proc-subset-pid.c | 5 ----- tools/testing/selftests/proc/proc.h | 5 ----- 30 files changed, 65 insertions(+), 52 deletions(-)Range-diff against v0:
-: ------------ > 1: ad8500a6eb0a string: Add streq() -: ------------ > 2: 1715e2c9c8ea tools/: Add streq() to nolibc/string -: ------------ > 3: b93e0d005739 treewide: Use streq(3) instead of confusing uses of strncmp(3) -: ------------ > 4: 7994b5827030 treewide: Use streq(3) instead of unnecessary uses of strncmp(3)Signature · 1 line
2.55.0
Signature · 1 line
<https://www.alejandro-colomar.es>