summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_sysctl.c
diff options
context:
space:
mode:
authorguenther <guenther@openbsd.org>2011-09-18 01:54:41 +0000
committerguenther <guenther@openbsd.org>2011-09-18 01:54:41 +0000
commit668e63dc86ecd226a78ddd947acac493b84229ea (patch)
tree9b29b03cfee7b705d48528da829bbf0144566824 /sys/kern/kern_sysctl.c
parentAdd a MACRO OVERVIEW, listing all macros ordered by purpose, (diff)
downloadwireguard-openbsd-668e63dc86ecd226a78ddd947acac493b84229ea.tar.xz
wireguard-openbsd-668e63dc86ecd226a78ddd947acac493b84229ea.zip
Make sysctl__string() handle the truncated output case via two
copyout()s instead of altering the in-use variable, and to also return the full length via oldlenp in that case as documented. ok jsing@ (problem pointed out by matthew@)
Diffstat (limited to 'sys/kern/kern_sysctl.c')
-rw-r--r--sys/kern/kern_sysctl.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index a891d47b7b2..32be5a89893 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_sysctl.c,v 1.206 2011/07/05 04:48:02 guenther Exp $ */
+/* $OpenBSD: kern_sysctl.c,v 1.207 2011/09/18 01:54:41 guenther Exp $ */
/* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */
/*-
@@ -905,7 +905,6 @@ sysctl__string(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
char *str, int maxlen, int trunc)
{
int len, error = 0;
- char c;
len = strlen(str) + 1;
if (oldp && *oldlenp < len) {
@@ -916,16 +915,15 @@ sysctl__string(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
return (EINVAL);
if (oldp) {
if (trunc && *oldlenp < len) {
- /* save & zap NUL terminator while copying */
- c = str[*oldlenp-1];
- str[*oldlenp-1] = '\0';
- error = copyout(str, oldp, *oldlenp);
- str[*oldlenp-1] = c;
+ len = *oldlenp;
+ error = copyout(str, oldp, len - 1);
+ if (error == 0)
+ error = copyout("", (char *)oldp + len - 1, 1);
} else {
- *oldlenp = len;
error = copyout(str, oldp, len);
}
}
+ *oldlenp = len;
if (error == 0 && newp) {
error = copyin(newp, str, newlen);
str[newlen] = 0;