Module Name:src
Committed By: dholland
Date: Sun Jul 27 04:46:48 UTC 2014
Modified Files:
src/usr.sbin/schedctl: schedctl.c
Log Message:
Fix illegal snprintf usage noted in PR 47976 by simplifying it away:
instead of consing up a string and printing it to stdout, just print
to stdout. Not as compositionally tidy, but much simpler and perfectly
adequate here.
To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/usr.sbin/schedctl/schedctl.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.sbin/schedctl/schedctl.c
diff -u src/usr.sbin/schedctl/schedctl.c:1.15 src/usr.sbin/schedctl/schedctl.c:1.16
--- src/usr.sbin/schedctl/schedctl.c:1.15 Wed Aug 31 13:32:41 2011
+++ src/usr.sbin/schedctl/schedctl.c Sun Jul 27 04:46:48 2014
@@ -1,4 +1,4 @@
-/* $NetBSD: schedctl.c,v 1.15 2011/08/31 13:32:41 joerg Exp $ */
+/* $NetBSD: schedctl.c,v 1.16 2014/07/27 04:46:48 dholland Exp $ */
/*
* Copyright (c) 2008, Mindaugas Rasiukevicius
@@ -33,9 +33,10 @@
#include
#ifndef lint
-__RCSID("$NetBSD: schedctl.c,v 1.15 2011/08/31 13:32:41 joerg Exp $");
+__RCSID("$NetBSD: schedctl.c,v 1.16 2014/07/27 04:46:48 dholland Exp $");
#endif
+#include
#include
#include
#include
@@ -60,7 +61,7 @@ static const char *class_str[] = {
static void sched_set(pid_t, lwpid_t, int, struct sched_param *, cpuset_t *);
static void thread_info(pid_t, lwpid_t);
static cpuset_t *makecpuset(char *);
-static char *showcpuset(cpuset_t *);
+static void printcpuset(cpuset_t *);
__dead static void usage(void);
static u_int ncpu;
@@ -205,7 +206,6 @@ thread_info(pid_t pid, lwpid_t lid)
{
struct sched_param sp;
cpuset_t *cpuset;
- char *cpus;
int error, policy;
cpuset = cpuset_create();
@@ -224,9 +224,9 @@ thread_info(pid_t pid, lwpid_t lid)
printf(" Priority: %d\n", sp.sched_priority);
printf(" Class:%s\n", class_str[policy]);
- cpus = showcpuset(cpuset);
- printf(" Affinity (CPUs): %s\n", cpus);
- free(cpus);
+ printf(" Affinity (CPUs): ");
+ printcpuset(cpuset);
+ printf("\n");
cpuset_destroy(cpuset);
}
@@ -280,31 +280,26 @@ makecpuset(char *str)
return cpuset;
}
-static char *
-showcpuset(cpuset_t *cpuset)
+static void
+printcpuset(cpuset_t *cpuset)
{
- char *buf;
- size_t size;
unsigned int i;
+ bool seen;
- size = 3 * ncpu; /* XXX */
- buf = malloc(size + 1);
- if (buf == NULL)
- err(EXIT_FAILURE, "malloc");
- memset(buf, '\0', size + 1);
-
- for (i = 0; i < ncpu; i++)
- if (cpuset_isset(i, cpuset))
- snprintf(buf, size, "%s%d,", buf, i);
-
- i = strlen(buf);
- if (i != 0) {
- buf[i - 1] = '\0';
- } else {
- strncpy(buf, "", size);
+ seen = false;
+ for (i = 0; i < ncpu; i++) {
+ if (cpuset_isset(i, cpuset)) {
+ if (seen) {
+putchar(',');
+ }
+ printf("%d", i);
+ seen = true;
+ }
}
- return buf;
+ if (!seen) {
+ printf("");
+ }
}
static void