diff options
author | robert <robert@FreeBSD.org> | 2002-10-17 20:03:38 +0000 |
---|---|---|
committer | robert <robert@FreeBSD.org> | 2002-10-17 20:03:38 +0000 |
commit | 1e0cdb534a593672b1178f980abd762bd316b57d (patch) | |
tree | 0a05cf96e39ba02857dd2294138be9f6e7f278ad | |
parent | 5e20feda93dab6d1a2a9c89e8f6a255504b3c3a3 (diff) | |
download | FreeBSD-src-1e0cdb534a593672b1178f980abd762bd316b57d.zip FreeBSD-src-1e0cdb534a593672b1178f980abd762bd316b57d.tar.gz |
Use strlcpy() instead of strncpy() to copy NUL terminated strings
for safety and consistency.
-rw-r--r-- | sys/kern/imgact_elf.c | 4 | ||||
-rw-r--r-- | sys/kern/init_main.c | 3 | ||||
-rw-r--r-- | sys/kern/kern_environment.c | 3 | ||||
-rw-r--r-- | sys/kern/kern_intr.c | 3 | ||||
-rw-r--r-- | sys/kern/kern_jail.c | 5 | ||||
-rw-r--r-- | sys/kern/kern_proc.c | 16 | ||||
-rw-r--r-- | sys/kern/kern_sysctl.c | 5 | ||||
-rw-r--r-- | sys/kern/kern_tc.c | 4 | ||||
-rw-r--r-- | sys/kern/subr_devstat.c | 3 | ||||
-rw-r--r-- | sys/kern/subr_diskslice.c | 4 | ||||
-rw-r--r-- | sys/kern/tty_cons.c | 5 |
11 files changed, 27 insertions, 28 deletions
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index 7ff8dd8..78b78a8 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -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); diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index e27958d..933c25c 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -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) { diff --git a/sys/kern/kern_environment.c b/sys/kern/kern_environment.c index a33b0c7..e4d308b 100644 --- a/sys/kern/kern_environment.c +++ b/sys/kern/kern_environment.c @@ -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); diff --git a/sys/kern/kern_intr.c b/sys/kern/kern_intr.c index 2d7b160..4bace31 100644 --- a/sys/kern/kern_intr.c +++ b/sys/kern/kern_intr.c @@ -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); diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index 1d76593..9451b85 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -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); } diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 345ee22..66b3c79 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -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; diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index cf84cd8..b494fe6 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -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); diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index 42c7134..445eab8 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -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) diff --git a/sys/kern/subr_devstat.c b/sys/kern/subr_devstat.c index dabdf9d..24f14ea 100644 --- a/sys/kern/subr_devstat.c +++ b/sys/kern/subr_devstat.c @@ -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; diff --git a/sys/kern/subr_diskslice.c b/sys/kern/subr_diskslice.c index 25b646a..0c3ae58 100644 --- a/sys/kern/subr_diskslice.c +++ b/sys/kern/subr_diskslice.c @@ -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) diff --git a/sys/kern/tty_cons.c b/sys/kern/tty_cons.c index 40285fc..bcd3389 100644 --- a/sys/kern/tty_cons.c +++ b/sys/kern/tty_cons.c @@ -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); |