Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/schedctl Fix illegal snprintf usage noted in PR 479...



details:   https://anonhg.NetBSD.org/src/rev/6b019fa34ba0
branches:  trunk
changeset: 797785:6b019fa34ba0
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Jul 27 04:46:48 2014 +0000

description:
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.

diffstat:

 usr.sbin/schedctl/schedctl.c |  49 +++++++++++++++++++------------------------
 1 files changed, 22 insertions(+), 27 deletions(-)

diffs (98 lines):

diff -r bb4e2b4e1ce2 -r 6b019fa34ba0 usr.sbin/schedctl/schedctl.c
--- a/usr.sbin/schedctl/schedctl.c      Sun Jul 27 04:38:03 2014 +0000
+++ b/usr.sbin/schedctl/schedctl.c      Sun Jul 27 04:46:48 2014 +0000
@@ -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>
@@ -33,9 +33,10 @@
 #include <sys/cdefs.h>
 
 #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 <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -60,7 +61,7 @@
 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 @@
 {
        struct sched_param sp;
        cpuset_t *cpuset;
-       char *cpus;
        int error, policy;
 
        cpuset = cpuset_create();
@@ -224,9 +224,9 @@
        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 @@
        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, "<none>", 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("<none>");
+       }
 }
 
 static void



Home | Main Index | Thread Index | Old Index