Use sbuf_printf() for sysctl strings instead of stack buffers and snprintf().

This commit is contained in:
Ian Lepore 2015-03-14 23:16:12 +00:00
parent 6ec9891291
commit 91d9eda200
2 changed files with 22 additions and 18 deletions

View File

@ -29,6 +29,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/queue.h>
@ -238,22 +239,24 @@ et_free(struct eventtimer *et)
static int
sysctl_kern_eventtimer_choice(SYSCTL_HANDLER_ARGS)
{
char buf[512], *spc;
struct sbuf sb;
struct eventtimer *et;
int error, off;
int error;
if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
return (error);
sbuf_new_for_sysctl(&sb, NULL, 0, req);
spc = "";
error = 0;
buf[0] = 0;
off = 0;
ET_LOCK();
SLIST_FOREACH(et, &eventtimers, et_all) {
off += snprintf(buf + off, sizeof(buf) - off, "%s%s(%d)",
spc, et->et_name, et->et_quality);
spc = " ";
if (et != SLIST_FIRST(&eventtimers))
sbuf_putc(&sb, ' ');
sbuf_printf(&sb, "%s(%d)", et->et_name, et->et_quality);
}
ET_UNLOCK();
error = SYSCTL_OUT(req, buf, strlen(buf));
error = sbuf_finish(&sb);
sbuf_delete(&sb);
return (error);
}
SYSCTL_PROC(_kern_eventtimer, OID_AUTO, choice,

View File

@ -25,6 +25,7 @@ __FBSDID("$FreeBSD$");
#include <sys/limits.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/systm.h>
@ -1445,18 +1446,18 @@ SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
static int
sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
{
char buf[32], *spc;
struct sbuf sb;
struct timecounter *tc;
int error;
spc = "";
error = 0;
for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
sprintf(buf, "%s%s(%d)",
spc, tc->tc_name, tc->tc_quality);
error = SYSCTL_OUT(req, buf, strlen(buf));
spc = " ";
sbuf_new_for_sysctl(&sb, NULL, 0, req);
for (tc = timecounters; tc != NULL; tc = tc->tc_next) {
if (tc != timecounters)
sbuf_putc(&sb, ' ');
sbuf_printf(&sb, "%s(%d)", tc->tc_name, tc->tc_quality);
}
error = sbuf_finish(&sb);
sbuf_delete(&sb);
return (error);
}