Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cifs: don't cargo-cult strndup()

strndup(s, strlen(s)) is a highly unidiomatic way to spell strdup(s);
it's *NOT* safer in any way, since strlen() is just as sensitive to
NUL-termination as strdup() is.

strndup() is for situations when you need a copy of a known-sized
substring, not a magic security juju to drive the bad spirits away.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Steve French <stfrench@microsoft.com>

authored by

Al Viro and committed by
Steve French
8d767223 b9335f62

+17 -24
+1 -1
fs/cifs/cifs_dfs_ref.c
··· 270 270 char *mountdata; 271 271 char *devname; 272 272 273 - devname = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL); 273 + devname = kstrdup(fullpath, GFP_KERNEL); 274 274 if (!devname) 275 275 return ERR_PTR(-ENOMEM); 276 276
+3 -6
fs/cifs/connect.c
··· 1778 1778 * for the request. 1779 1779 */ 1780 1780 if (is_domain && ses->domainName) { 1781 - ctx->domainname = kstrndup(ses->domainName, 1782 - strlen(ses->domainName), 1783 - GFP_KERNEL); 1781 + ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL); 1784 1782 if (!ctx->domainname) { 1785 1783 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", 1786 1784 len); ··· 3409 3411 goto error; 3410 3412 } 3411 3413 /* Save mount options */ 3412 - mntdata = kstrndup(cifs_sb->ctx->mount_options, 3413 - strlen(cifs_sb->ctx->mount_options), GFP_KERNEL); 3414 + mntdata = kstrdup(cifs_sb->ctx->mount_options, GFP_KERNEL); 3414 3415 if (!mntdata) { 3415 3416 rc = -ENOMEM; 3416 3417 goto error; ··· 3482 3485 * links, the prefix path is included in both and may be changed during reconnect. See 3483 3486 * cifs_tree_connect(). 3484 3487 */ 3485 - cifs_sb->origin_fullpath = kstrndup(full_path, strlen(full_path), GFP_KERNEL); 3488 + cifs_sb->origin_fullpath = kstrdup(full_path, GFP_KERNEL); 3486 3489 if (!cifs_sb->origin_fullpath) { 3487 3490 rc = -ENOMEM; 3488 3491 goto error;
+9 -9
fs/cifs/dfs_cache.c
··· 89 89 if (*path == '\\') { 90 90 *npath = (char *)path; 91 91 } else { 92 - *npath = kstrndup(path, strlen(path), GFP_KERNEL); 92 + *npath = kstrdup(path, GFP_KERNEL); 93 93 if (!*npath) 94 94 return -ENOMEM; 95 95 convert_delimiter(*npath, '\\'); ··· 358 358 t = kmalloc(sizeof(*t), GFP_ATOMIC); 359 359 if (!t) 360 360 return ERR_PTR(-ENOMEM); 361 - t->name = kstrndup(name, strlen(name), GFP_ATOMIC); 361 + t->name = kstrdup(name, GFP_ATOMIC); 362 362 if (!t->name) { 363 363 kfree(t); 364 364 return ERR_PTR(-ENOMEM); ··· 419 419 if (!ce) 420 420 return ERR_PTR(-ENOMEM); 421 421 422 - ce->path = kstrndup(path, strlen(path), GFP_KERNEL); 422 + ce->path = kstrdup(path, GFP_KERNEL); 423 423 if (!ce->path) { 424 424 kmem_cache_free(cache_slab, ce); 425 425 return ERR_PTR(-ENOMEM); ··· 531 531 char *s, *e; 532 532 char sep; 533 533 534 - npath = kstrndup(path, strlen(path), GFP_KERNEL); 534 + npath = kstrdup(path, GFP_KERNEL); 535 535 if (!npath) 536 536 return ERR_PTR(-ENOMEM); 537 537 ··· 641 641 642 642 if (ce->tgthint) { 643 643 s = ce->tgthint->name; 644 - th = kstrndup(s, strlen(s), GFP_ATOMIC); 644 + th = kstrdup(s, GFP_ATOMIC); 645 645 if (!th) 646 646 return -ENOMEM; 647 647 } ··· 786 786 787 787 memset(ref, 0, sizeof(*ref)); 788 788 789 - ref->path_name = kstrndup(path, strlen(path), GFP_ATOMIC); 789 + ref->path_name = kstrdup(path, GFP_ATOMIC); 790 790 if (!ref->path_name) 791 791 return -ENOMEM; 792 792 793 - ref->node_name = kstrndup(target, strlen(target), GFP_ATOMIC); 793 + ref->node_name = kstrdup(target, GFP_ATOMIC); 794 794 if (!ref->node_name) { 795 795 rc = -ENOMEM; 796 796 goto err_free_path; ··· 828 828 goto err_free_it; 829 829 } 830 830 831 - it->it_name = kstrndup(t->name, strlen(t->name), GFP_ATOMIC); 831 + it->it_name = kstrdup(t->name, GFP_ATOMIC); 832 832 if (!it->it_name) { 833 833 kfree(it); 834 834 rc = -ENOMEM; ··· 1166 1166 if (!vi) 1167 1167 return -ENOMEM; 1168 1168 1169 - vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL); 1169 + vi->fullpath = kstrdup(fullpath, GFP_KERNEL); 1170 1170 if (!vi->fullpath) { 1171 1171 rc = -ENOMEM; 1172 1172 goto err_free_vi;
+1 -1
fs/cifs/fs_context.c
··· 430 430 if (nval == p) 431 431 continue; 432 432 *nval++ = 0; 433 - *val = kstrndup(nval, strlen(nval), GFP_KERNEL); 433 + *val = kstrdup(nval, GFP_KERNEL); 434 434 rc = !*val ? -ENOMEM : 0; 435 435 goto out; 436 436 }
+1 -1
fs/cifs/misc.c
··· 1180 1180 kfree(cifs_sb->prepath); 1181 1181 1182 1182 if (prefix && *prefix) { 1183 - cifs_sb->prepath = kstrndup(prefix, strlen(prefix), GFP_ATOMIC); 1183 + cifs_sb->prepath = kstrdup(prefix, GFP_ATOMIC); 1184 1184 if (!cifs_sb->prepath) { 1185 1185 rc = -ENOMEM; 1186 1186 goto out;
+1 -3
fs/cifs/smb1ops.c
··· 926 926 0); 927 927 928 928 if (!rc) { 929 - *symlinkinfo = kstrndup(referral.node_name, 930 - strlen(referral.node_name), 931 - GFP_KERNEL); 929 + *symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL); 932 930 free_dfs_info_param(&referral); 933 931 if (!*symlinkinfo) 934 932 rc = -ENOMEM;
+1 -3
fs/cifs/unc.c
··· 50 50 { 51 51 const char *src; 52 52 char *delim, *dst; 53 - int len; 54 53 55 54 /* skip double chars at the beginning */ 56 55 src = unc + 2; ··· 59 60 if (!delim) 60 61 return ERR_PTR(-EINVAL); 61 62 delim++; 62 - len = strlen(delim); 63 63 64 64 /* caller has to free the memory */ 65 - dst = kstrndup(delim, len, GFP_KERNEL); 65 + dst = kstrdup(delim, GFP_KERNEL); 66 66 if (!dst) 67 67 return ERR_PTR(-ENOMEM); 68 68