diff mbox series

[linux-next] um: use strscpy() is more robust and safer

Message ID 20220921092944.230413-1-ye.xingchen@zte.com.cn
State Changes Requested
Headers show
Series [linux-next] um: use strscpy() is more robust and safer | expand

Commit Message

CGEL Sept. 21, 2022, 9:29 a.m. UTC
From: ye xingchen <ye.xingchen@zte.com.cn>

The implementation of strscpy() is more robust and safer.

That's now the recommended way to copy NUL terminated strings.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: ye xingchen <ye.xingchen@zte.com.cn>
---
 arch/um/os-Linux/umid.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

kernel test robot Sept. 22, 2022, 2:34 a.m. UTC | #1
Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on next-20220920]

url:    https://github.com/intel-lab-lkp/linux/commits/cgel-zte-gmail-com/um-use-strscpy-is-more-robust-and-safer/20220921-173157
base:    ef08d387bbbc20df740ced8caee0ffac835869ac
config: um-i386_defconfig
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/e6fc2defb5c99c34a29cdc7f79df8d543e8d30a5
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review cgel-zte-gmail-com/um-use-strscpy-is-more-robust-and-safer/20220921-173157
        git checkout e6fc2defb5c99c34a29cdc7f79df8d543e8d30a5
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=um SUBARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   arch/um/os-Linux/umid.c: In function 'make_uml_dir':
>> arch/um/os-Linux/umid.c:43:17: error: implicit declaration of function 'strscpy'; did you mean 'strncpy'? [-Werror=implicit-function-declaration]
      43 |                 strscpy(dir, home, sizeof(dir));
         |                 ^~~~~~~
         |                 strncpy
   arch/um/os-Linux/umid.c: In function 'is_umdir_used':
   arch/um/os-Linux/umid.c:139:23: warning: variable 'err' set but not used [-Wunused-but-set-variable]
     139 |         int fd, p, n, err;
         |                       ^~~
   cc1: some warnings being treated as errors


vim +43 arch/um/os-Linux/umid.c

    27	
    28	static int __init make_uml_dir(void)
    29	{
    30		char dir[512] = { '\0' };
    31		int len, err;
    32	
    33		if (*uml_dir == '~') {
    34			char *home = getenv("HOME");
    35	
    36			err = -ENOENT;
    37			if (home == NULL) {
    38				printk(UM_KERN_ERR
    39					"%s: no value in environment for $HOME\n",
    40					__func__);
    41				goto err;
    42			}
  > 43			strscpy(dir, home, sizeof(dir));
    44			uml_dir++;
    45		}
    46		strlcat(dir, uml_dir, sizeof(dir));
    47		len = strlen(dir);
    48		if (len > 0 && dir[len - 1] != '/')
    49			strlcat(dir, "/", sizeof(dir));
    50	
    51		err = -ENOMEM;
    52		uml_dir = malloc(strlen(dir) + 1);
    53		if (uml_dir == NULL) {
    54			printk(UM_KERN_ERR "%s : malloc failed, errno = %d\n",
    55				__func__, errno);
    56			goto err;
    57		}
    58		strcpy(uml_dir, dir);
    59	
    60		if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
    61			printk(UM_KERN_ERR "Failed to mkdir '%s': %s\n",
    62				uml_dir, strerror(errno));
    63			err = -errno;
    64			goto err_free;
    65		}
    66		return 0;
    67	
    68	err_free:
    69		free(uml_dir);
    70	err:
    71		uml_dir = NULL;
    72		return err;
    73	}
    74
diff mbox series

Patch

diff --git a/arch/um/os-Linux/umid.c b/arch/um/os-Linux/umid.c
index 7a1abb829930..288c422bfa96 100644
--- a/arch/um/os-Linux/umid.c
+++ b/arch/um/os-Linux/umid.c
@@ -40,7 +40,7 @@  static int __init make_uml_dir(void)
 				__func__);
 			goto err;
 		}
-		strlcpy(dir, home, sizeof(dir));
+		strscpy(dir, home, sizeof(dir));
 		uml_dir++;
 	}
 	strlcat(dir, uml_dir, sizeof(dir));
@@ -243,7 +243,7 @@  int __init set_umid(char *name)
 	if (strlen(name) > UMID_LEN - 1)
 		return -E2BIG;
 
-	strlcpy(umid, name, sizeof(umid));
+	strscpy(umid, name, sizeof(umid));
 
 	return 0;
 }
@@ -262,7 +262,7 @@  static int __init make_umid(void)
 	make_uml_dir();
 
 	if (*umid == '\0') {
-		strlcpy(tmp, uml_dir, sizeof(tmp));
+		strscpy(tmp, uml_dir, sizeof(tmp));
 		strlcat(tmp, "XXXXXX", sizeof(tmp));
 		fd = mkstemp(tmp);
 		if (fd < 0) {