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.
This commit is contained in:
dholland 2014-07-27 04:46:48 +00:00
parent d78881479c
commit e3566c1bef

View File

@ -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 <rmind at NetBSD org> * Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
@ -33,9 +33,10 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #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 #endif
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -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 sched_set(pid_t, lwpid_t, int, struct sched_param *, cpuset_t *);
static void thread_info(pid_t, lwpid_t); static void thread_info(pid_t, lwpid_t);
static cpuset_t *makecpuset(char *); static cpuset_t *makecpuset(char *);
static char *showcpuset(cpuset_t *); static void printcpuset(cpuset_t *);
__dead static void usage(void); __dead static void usage(void);
static u_int ncpu; static u_int ncpu;
@ -205,7 +206,6 @@ thread_info(pid_t pid, lwpid_t lid)
{ {
struct sched_param sp; struct sched_param sp;
cpuset_t *cpuset; cpuset_t *cpuset;
char *cpus;
int error, policy; int error, policy;
cpuset = cpuset_create(); cpuset = cpuset_create();
@ -224,9 +224,9 @@ thread_info(pid_t pid, lwpid_t lid)
printf(" Priority: %d\n", sp.sched_priority); printf(" Priority: %d\n", sp.sched_priority);
printf(" Class: %s\n", class_str[policy]); printf(" Class: %s\n", class_str[policy]);
cpus = showcpuset(cpuset); printf(" Affinity (CPUs): ");
printf(" Affinity (CPUs): %s\n", cpus); printcpuset(cpuset);
free(cpus); printf("\n");
cpuset_destroy(cpuset); cpuset_destroy(cpuset);
} }
@ -280,31 +280,26 @@ makecpuset(char *str)
return cpuset; return cpuset;
} }
static char * static void
showcpuset(cpuset_t *cpuset) printcpuset(cpuset_t *cpuset)
{ {
char *buf;
size_t size;
unsigned int i; unsigned int i;
bool seen;
size = 3 * ncpu; /* XXX */ seen = false;
buf = malloc(size + 1); for (i = 0; i < ncpu; i++) {
if (buf == NULL) if (cpuset_isset(i, cpuset)) {
err(EXIT_FAILURE, "malloc"); if (seen) {
memset(buf, '\0', size + 1); putchar(',');
}
for (i = 0; i < ncpu; i++) printf("%d", i);
if (cpuset_isset(i, cpuset)) seen = true;
snprintf(buf, size, "%s%d,", buf, i); }
i = strlen(buf);
if (i != 0) {
buf[i - 1] = '\0';
} else {
strncpy(buf, "<none>", size);
} }
return buf; if (!seen) {
printf("<none>");
}
} }
static void static void