[PATCH] tools: include: add proper strscpy() declaration

Lucas Poupeau posted 1 patch 1 month, 1 week ago
tools/include/linux/string.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] tools: include: add proper strscpy() declaration
Posted by Lucas Poupeau 1 month, 1 week ago
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'")
Suggested-by: Maxwell Doose <m32285159@gmail.com>
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
Signed-off-by: Lucas Poupeau <lucasp.linux@gmail.com>
---
 tools/include/linux/string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h
index 51ad3cf4fa82..d6e53109e786 100644
--- a/tools/include/linux/string.h
+++ b/tools/include/linux/string.h
@@ -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.
-- 
2.54.0
Re: [PATCH] tools: include: add proper strscpy() declaration
Posted by kernel test robot 1 month ago
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/202605110823.9YvAiXAD-lkp@intel.com/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/202605110823.9YvAiXAD-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202605110823.9YvAiXAD-lkp@intel.com/

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
Re: [PATCH] tools: include: add proper strscpy() declaration
Posted by Wei Yang 1 month, 1 week ago
On Wed, May 06, 2026 at 06:34:52PM +0200, Lucas Poupeau wrote:
>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.

>Suggested-by: Maxwell Doose <m32285159@gmail.com>
>Reviewed-by: Wei Yang <richard.weiyang@gmail.com>

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

It is not proper to add RB by yourself.

>Signed-off-by: Lucas Poupeau <lucasp.linux@gmail.com>
>---
> tools/include/linux/string.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h
>index 51ad3cf4fa82..d6e53109e786 100644
>--- a/tools/include/linux/string.h
>+++ b/tools/include/linux/string.h
>@@ -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

> /*
>  * glibc based builds needs the extern while uClibc doesn't.
>-- 
>2.54.0

-- 
Wei Yang
Help you, Help me
Re: [PATCH] tools: include: add proper strscpy() declaration
Posted by Maxwell Doose 1 month, 1 week ago
On Wed, May 6, 2026 at 9:42 PM Wei Yang <richard.weiyang@gmail.com> wrote:
>
> On Wed, May 06, 2026 at 06:34:52PM +0200, Lucas Poupeau wrote:
> >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.

>
> >Suggested-by: Maxwell Doose <m32285159@gmail.com>
> >Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
>
> 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.

>
> >Signed-off-by: Lucas Poupeau <lucasp.linux@gmail.com>
>

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

>
> >---
> > tools/include/linux/string.h | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> >diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h
> >index 51ad3cf4fa82..d6e53109e786 100644
> >--- a/tools/include/linux/string.h
> >+++ b/tools/include/linux/string.h
> >@@ -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
>

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



> > /*
> >  * glibc based builds needs the extern while uClibc doesn't.
> >--
> >2.54.0
>
> --
> Wei Yang
> Help you, Help me
Re: [PATCH] tools: include: add proper strscpy() declaration
Posted by Maxwell Doose 1 month, 1 week ago
On Wed, May 6, 2026 at 11:34 AM Lucas Poupeau <lucasp.linux@gmail.com> wrote:
>
> 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'")
> Suggested-by: Maxwell Doose <m32285159@gmail.com>
> Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
> Signed-off-by: Lucas Poupeau <lucasp.linux@gmail.com>
> ---
>  tools/include/linux/string.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h
> index 51ad3cf4fa82..d6e53109e786 100644
> --- a/tools/include/linux/string.h
> +++ b/tools/include/linux/string.h
> @@ -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.
> --
> 2.54.0
>

Should be v2; please remark and add changelog.

best regards,
max