Skip to contentSkip to conversation

tools: include: add proper strscpy() declaration

PATCH In review Lucas Poupeau 4 participants 5 messages May 6 – May 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: include: add proper strscpy() declaration

Remove the macro hack and add a proper implementation in tools/lib/string.c. This avoids unsafe strcpy usage when strscpy is expected and fixes potential compilation warnings regarding str_error_r.

Fixes 9e3d665 ("memblock test: fix implicit declaration of function 'strscpy'")
---
 tools/include/linux/string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/linux/string.h +1 -1
index 51ad3cf4fa82..d6e53109e786 100644
@@ -12,7 +12,7 @@ void argv_free(char **argv);
 
 int strtobool(const char *s, bool *res);
 
-#define strscpy strcpy
+ssize_t strscpy(char *dest, const char *src, size_t count);
 
 /*
  * glibc based builds needs the extern while uClibc doesn't.
Signature · 1 line
2.54.0
#1 lore raw
Quoted 27 lines · Lucas Poupeau

Remove the macro hack and add a proper implementation in tools/lib/string.c. This avoids unsafe strcpy usage when strscpy is expected and fixes potential compilation warnings regarding str_error_r.

Fixes 9e3d665 ("memblock test: fix implicit declaration of function 'strscpy'")
---
 tools/include/linux/string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/linux/string.h +1 -2
index 51ad3cf4fa82..d6e53109e786 100644
@@ -12,7 +12,7 @@ void argv_free(char **argv);
 
 int strtobool(const char *s, bool *res);
 
-#define strscpy strcpy
+ssize_t strscpy(char *dest, const char *src, size_t count);
 
 /*
  * glibc based builds needs the extern while uClibc doesn't.
--
Quoted 2 lines

2.54.0

Should be v2; please remark and add changelog.

best regards,
max

#2 lore raw
In reply to Lucas Poupeau
Quoted 5 lines · Lucas Poupeau

Remove the macro hack and add a proper implementation in tools/lib/string.c. This avoids unsafe strcpy usage when strscpy is expected and fixes potential compilation warnings regarding str_error_r.

Fixes 9e3d665 ("memblock test: fix implicit declaration of function 'strscpy'")

The Fixes tag seems not necessary.

Quoted 2 lines

Hmm... I didn't see your change before, so I can't reviewed yet.

It is not proper to add RB by yourself.

Quoted 10 lines

---
tools/include/linux/string.h | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/linux/string.h +0 -0
index 51ad3cf4fa82..d6e53109e786 100644

tools/include/linux/string.h void argv_free(char **argv); · line 16

Quoted 6 lines
tools/include/linux/string.h +1 -1
@@ -12,7 +12,7 @@ void argv_free(char **argv);
 
 int strtobool(const char *s, bool *res);
 
-#define strscpy strcpy
+ssize_t strscpy(char *dest, const char *src, size_t count);
 

You mean add proper implementation, but this is just a declaration. Where is the definition in tools/lib/string.c? Something is missed?

After applying your patch, I got this when build tools/testing/memblock.

memblock.c: In function ‘reserved_mem_add’:
memblock.c:2464:9: error: too few arguments to function ‘strscpy’

 2464 |         strscpy(map->name, name);
      |         ^~~~~~~
In file included from ./linux/kernel.h:11,
                 from memblock.c:9:
../../include/linux/string.h:16:9: note: declared here
   16 | ssize_t strscpy(char *dest, const char *src, size_t count);
      |         ^~~~~~~

make: *** [<builtin>: memblock.o] Error 1

Quoted 4 lines
tools/include/linux/string.h +0 -0
 /*
 * glibc based builds needs the extern while uClibc doesn't.
Signature · 1 line
2.54.0
Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
Quoted 10 lines · Wei Yang
Quoted 5 lines · Lucas Poupeau

Remove the macro hack and add a proper implementation in tools/lib/string.c. This avoids unsafe strcpy usage when strscpy is expected and fixes potential compilation warnings regarding str_error_r.

Fixes 9e3d665 ("memblock test: fix implicit declaration of function 'strscpy'")

The Fixes tag seems not necessary.

Perhaps...let's get rid of that.

Quoted 7 lines
Quoted 3 lines

Hmm... I didn't see your change before, so I can't reviewed yet.

It is not proper to add RB by yourself.

No clue how this got added. Lucas, please remove this tag, as Wei didn't give an RB.

Quoted 3 lines
Quoted 2 lines

Lucas, make sure you add the changes Wei asked for as well.

Quoted 25 lines
Quoted 10 lines

---
tools/include/linux/string.h | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/linux/string.h +0 -0
index 51ad3cf4fa82..d6e53109e786 100644

tools/include/linux/string.h void argv_free(char **argv); · line 16

Quoted 6 lines
tools/include/linux/string.h +1 -1
@@ -12,7 +12,7 @@ void argv_free(char **argv);
 
 int strtobool(const char *s, bool *res);
 
-#define strscpy strcpy
+ssize_t strscpy(char *dest, const char *src, size_t count);
 

You mean add proper implementation, but this is just a declaration. Where is the definition in tools/lib/string.c? Something is missed?

After applying your patch, I got this when build tools/testing/memblock.

memblock.c: In function ‘reserved_mem_add’:
memblock.c:2464:9: error: too few arguments to function ‘strscpy’

Quoted 9 lines
 2464 |         strscpy(map->name, name);
      |         ^~~~~~~
In file included from ./linux/kernel.h:11,
                 from memblock.c:9:
../../include/linux/string.h:16:9: note: declared here
   16 | ssize_t strscpy(char *dest, const char *src, size_t count);
      |         ^~~~~~~

make: *** [<builtin>: memblock.o] Error 1

Ah...seems like a signature mismatch. Too bad testing/memblock is reliant on this crap macro now... Lucas, make sure when you're testing this that you build the ENTIRE kernel, since there may be hidden dependencies like this.

best regards,
max

Quoted 8 lines
Quoted 4 lines
tools/include/linux/string.h +0 -1
 /*
 * glibc based builds needs the extern while uClibc doesn't.
--

2.54.0

--
Wei Yang
Help you, Help me

#4 lore raw
In reply to Lucas Poupeau
kernel test robot noticed the following build errors:

Hi Lucas,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v7.1-rc3 next-20260508]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Lucas-Poupeau/tools-include-add-proper-strscpy-declaration/20260511-103301
base:   linus/master
patch link:    https://lore.kernel.org/r/20260506163452.53067-1-lucasp.linux%40gmail.com
patch subject: [PATCH] tools: include: add proper strscpy() declaration
config: x86_64-rhel-9.4 (https://download.01.org/0day-ci/archive/20260511/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260511/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from exec-cmd.c:3:

>> tools/include/linux/string.h:15:1: error: unknown type name 'ssize_t'; did you mean 'size_t'?
      15 | ssize_t strscpy(char *dest, const char *src, size_t count);
         | ^~~~~~~
         | size_t
   In file included from parse-options.c:3:
>> tools/include/linux/string.h:15:1: error: unknown type name 'ssize_t'; did you mean 'size_t'?
      15 | ssize_t strscpy(char *dest, const char *src, size_t count);
         | ^~~~~~~
         | size_t

make[6]: *** [tools/build/Makefile.build:96: tools/objtool/libsubcmd/exec-cmd.o] Error 1 make[6]: *** [tools/build/Makefile.build:95: tools/objtool/libsubcmd/parse-options.o] Error 1 make[6]: Target '__build' not remade because of errors.
make[5]: *** [Makefile:78: tools/objtool/libsubcmd/libsubcmd-in.o] Error 2 make[5]: Target 'tools/objtool/libsubcmd/libsubcmd.a' not remade because of errors. make[4]: *** [Makefile:135: tools/objtool/libsubcmd/libsubcmd.a] Error 2 make[4]: Target 'all' not remade because of errors.
make[3]: *** [Makefile:74: objtool] Error 2
make[2]: *** [Makefile:1557: tools/objtool] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +15 tools/include/linux/string.h

    14	
  > 15	ssize_t strscpy(char *dest, const char *src, size_t count);
    16	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki