From 7ee53e6bf4c1a5550ce50b3289b2ce2187ad070b Mon Sep 17 00:00:00 2001 From: mjg Date: Thu, 10 Jul 2014 21:46:57 +0000 Subject: [PATCH] Don't make a temporary copy of fixed sysctl strings. --- sys/kern/kern_sysctl.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 44078b1dff39..5e74d87ea01d 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1216,26 +1216,33 @@ int sysctl_handle_string(SYSCTL_HANDLER_ARGS) { size_t outlen; - int error = 0; + int error = 0, ro_string = 0; /* * A zero-length buffer indicates a fixed size read-only * string: */ - if (arg2 == 0) + if (arg2 == 0) { arg2 = strlen((char *)arg1) + 1; + ro_string = 1; + } if (req->oldptr != NULL) { char *tmparg; - /* try to make a coherent snapshot of the string */ - tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); - memcpy(tmparg, arg1, arg2); + if (ro_string) { + tmparg = arg1; + } else { + /* try to make a coherent snapshot of the string */ + tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); + memcpy(tmparg, arg1, arg2); + } outlen = strnlen(tmparg, arg2 - 1) + 1; error = SYSCTL_OUT(req, tmparg, outlen); - free(tmparg, M_SYSCTLTMP); + if (!ro_string) + free(tmparg, M_SYSCTLTMP); } else { outlen = strnlen((char *)arg1, arg2 - 1) + 1; error = SYSCTL_OUT(req, NULL, outlen);