Use strlcpy() instead of strncpy() to copy NUL terminated strings

for safety and consistency.
This commit is contained in:
Robert Drehmel 2002-10-17 20:03:38 +00:00
parent 34c763eff1
commit e80fb43467
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=105354
11 changed files with 27 additions and 28 deletions

View File

@ -1108,10 +1108,10 @@ __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize)
psinfo->pr_version = PRPSINFO_VERSION;
psinfo->pr_psinfosz = sizeof(prpsinfo_t);
strncpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname) - 1);
strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
/* XXX - We don't fill in the command line arguments properly yet. */
strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
strlcpy(psinfo->pr_psargs, p->p_comm, sizeof(psinfo->pr_psargs));
/* Fill in the header. */
bzero(hdr, hdrsize);

View File

@ -575,8 +575,7 @@ start_init(void *dummy)
p->p_vmspace->vm_ssize = 1;
if ((var = getenv("init_path")) != NULL) {
strncpy(init_path, var, sizeof init_path);
init_path[sizeof init_path - 1] = 0;
strlcpy(init_path, var, sizeof(init_path));
freeenv(var);
}
if ((var = getenv("kern.fallback_elf_brand")) != NULL) {

View File

@ -372,9 +372,8 @@ getenv_string(const char *name, char *data, int size)
tmp = getenv(name);
if (tmp != NULL) {
strncpy(data, tmp, size);
strlcpy(data, tmp, size);
freeenv(tmp);
data[size - 1] = 0;
return (1);
} else
return (0);

View File

@ -124,7 +124,8 @@ ithread_update(struct ithd *ithd)
return;
p = td->td_proc;
strncpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name));
strlcpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name));
ih = TAILQ_FIRST(&ithd->it_handlers);
if (ih == NULL) {
mtx_lock_spin(&sched_lock);

View File

@ -249,10 +249,9 @@ getcredhostname(cred, buf, size)
if (jailed(cred)) {
mtx_lock(&cred->cr_prison->pr_mtx);
strncpy(buf, cred->cr_prison->pr_host, size);
strlcpy(buf, cred->cr_prison->pr_host, size);
mtx_unlock(&cred->cr_prison->pr_mtx);
}
else
strncpy(buf, hostname, size);
buf[size - 1] = '\0';
strlcpy(buf, hostname, size);
}

View File

@ -921,13 +921,13 @@ fill_kinfo_proc(p, kp)
}
if (!(p->p_flag & P_KSES)) {
if (td->td_wmesg != NULL) {
strncpy(kp->ki_wmesg, td->td_wmesg,
sizeof(kp->ki_wmesg) - 1);
strlcpy(kp->ki_wmesg, td->td_wmesg,
sizeof(kp->ki_wmesg));
}
if (TD_ON_LOCK(td)) {
kp->ki_kiflag |= KI_LOCKBLOCK;
strncpy(kp->ki_lockname, td->td_lockname,
sizeof(kp->ki_lockname) - 1);
strlcpy(kp->ki_lockname, td->td_lockname,
sizeof(kp->ki_lockname));
}
}
@ -1002,8 +1002,8 @@ fill_kinfo_proc(p, kp)
if (sp != NULL) {
kp->ki_sid = sp->s_sid;
SESS_LOCK(sp);
strncpy(kp->ki_login, sp->s_login,
sizeof(kp->ki_login) - 1);
strlcpy(kp->ki_login, sp->s_login,
sizeof(kp->ki_login));
if (sp->s_ttyvp)
kp->ki_kiflag |= KI_CTTY;
if (SESS_LEADER(p))
@ -1020,8 +1020,8 @@ fill_kinfo_proc(p, kp)
} else
kp->ki_tdev = NOUDEV;
if (p->p_comm[0] != '\0') {
strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1);
strncpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm) - 1);
strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
}
kp->ki_siglist = p->p_siglist;
kp->ki_sigmask = p->p_sigmask;

View File

@ -822,11 +822,12 @@ sysctl_handle_string(SYSCTL_HANDLER_ARGS)
retry:
outlen = strlen((char *)arg1)+1;
tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
strncpy(tmparg, (char *)arg1, outlen);
if (tmparg[outlen-1] != '\0') {
if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
free(tmparg, M_SYSCTLTMP);
goto retry;
}
error = SYSCTL_OUT(req, tmparg, outlen);
free(tmparg, M_SYSCTLTMP);

View File

@ -447,8 +447,8 @@ sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
int error;
tc = timecounter;
strncpy(newname, tc->tc_name, sizeof(newname));
newname[sizeof(newname) - 1] = '\0';
strlcpy(newname, tc->tc_name, sizeof(newname));
error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
if (error != 0 || req->newptr == NULL ||
strcmp(newname, tc->tc_name) == 0)

View File

@ -122,8 +122,7 @@ devstat_add_entry(struct devstat *ds, const char *dev_name,
ds->device_number = devstat_current_devnumber++;
ds->unit_number = unit_number;
strncpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
ds->device_name[DEVSTAT_NAME_LEN - 1] = '\0';
strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
ds->block_size = block_size;
ds->flags = flags;
ds->device_type = device_type;

View File

@ -100,9 +100,9 @@ clone_label(lp)
*lp1 = *lp;
lp = NULL;
if (lp1->d_typename[0] == '\0')
strncpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
strlcpy(lp1->d_typename, "amnesiac", sizeof(lp1->d_typename));
if (lp1->d_packname[0] == '\0')
strncpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
strlcpy(lp1->d_packname, "fictitious", sizeof(lp1->d_packname));
if (lp1->d_nsectors == 0)
lp1->d_nsectors = 32;
if (lp1->d_ntracks == 0)

View File

@ -363,9 +363,10 @@ cn_devopen(struct cn_device *cnd, struct thread *td, int forceopen)
cnd->cnd_vp = NULL;
vn_close(vp, openflag, td->td_ucred, td);
}
if (cnd->cnd_name[0] == '\0')
strncpy(cnd->cnd_name, devtoname(cnd->cnd_cn->cn_dev),
if (cnd->cnd_name[0] == '\0') {
strlcpy(cnd->cnd_name, devtoname(cnd->cnd_cn->cn_dev),
sizeof(cnd->cnd_name));
}
snprintf(path, sizeof(path), "/dev/%s", cnd->cnd_name);
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
error = vn_open(&nd, &openflag, 0);