Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/x86/x86 make this compute the needed size instead o...



details:   https://anonhg.NetBSD.org/src/rev/4c5c71c9c72e
branches:  trunk
changeset: 795259:4c5c71c9c72e
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Apr 05 18:43:09 2014 +0000

description:
make this compute the needed size instead of bailing.

diffstat:

 sys/arch/x86/x86/procfs_machdep.c |  99 ++++++++++++++++++++------------------
 1 files changed, 51 insertions(+), 48 deletions(-)

diffs (177 lines):

diff -r 2e18120bb096 -r 4c5c71c9c72e sys/arch/x86/x86/procfs_machdep.c
--- a/sys/arch/x86/x86/procfs_machdep.c Sat Apr 05 18:42:32 2014 +0000
+++ b/sys/arch/x86/x86/procfs_machdep.c Sat Apr 05 18:43:09 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: procfs_machdep.c,v 1.5 2014/03/27 18:22:56 christos Exp $ */
+/*     $NetBSD: procfs_machdep.c,v 1.6 2014/04/05 18:43:09 christos Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.5 2014/03/27 18:22:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: procfs_machdep.c,v 1.6 2014/04/05 18:43:09 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -102,7 +102,7 @@
 #endif
 };
 
-static int     procfs_getonecpu(int, struct cpu_info *, char *, int *);
+static int     procfs_getonecpu(int, struct cpu_info *, char *, size_t *);
 
 /*
  * Linux-style /proc/cpuinfo.
@@ -111,55 +111,54 @@
  * In the multiprocessor case, this should be a loop over all CPUs.
  */
 int
-procfs_getcpuinfstr(char *bf, int *len)
+procfs_getcpuinfstr(char *bf, size_t *len)
 {
        struct cpu_info *ci;
        CPU_INFO_ITERATOR cii;
-       int i = 0, used = *len, total = *len;
+       size_t i, total, size, used;
 
-       *len = 0;
+       i = total = 0;
+       used = size = *len;
+       
        for (CPU_INFO_FOREACH(cii, ci)) {
-               if (procfs_getonecpu(i++, ci, bf, &used) == 0) {
-                       *len += used;
-                       total = 0;
-                       break;
-               }
-               total -= used;
-               if (total > 0) {
+               procfs_getonecpu(i++, ci, bf, &used);
+               total += used + 1;
+               if (used + 1 < size) {
                        bf += used;
                        *bf++ = '\n';
-                       *len += used + 1;
-                       used = --total;
-                       if (used == 0)
-                               break;
-               } else {
-                       *len += used;
-                       break;
-               }
+                       size -= used + 1;
+                       used = size;
+               } else
+                       used = 0;
        }
-       return total == 0 ? -1 : 0;
+       size = *len;
+       *len = total;
+       return size < *len ? -1 : 0;
 }
 
 static int
-procfs_getonecpu(int xcpu, struct cpu_info *ci, char *bf, int *len)
+procfs_getonecpu(int xcpu, struct cpu_info *ci, char *bf, size_t *len)
 {
-       int left, l, i;
-       char featurebuf[256], *p;
+       size_t left, l, size;
+       char featurebuf[1024], *p;
 
        p = featurebuf;
        left = sizeof(featurebuf);
-       for (i = 0; i < 32; i++) {
+       size = *len;
+       for (size_t i = 0; i < 32; i++) {
                if ((ci->ci_feat_val[0] & (1 << i)) && x86_features[i]) {
                        l = snprintf(p, left, "%s ", x86_features[i]);
-                       if (l > left)
-                               return 0;
-                       left -= l;
-                       p += l;
+                       if (l < left) {
+                               left -= l;
+                               p += l;
+                       } else
+                               break;
                }
        }
 
        p = bf;
        left = *len;
+       size = 0;
        l = snprintf(p, left,
            "processor\t: %d\n"
            "vendor_id\t: %s\n"
@@ -173,21 +172,24 @@
            cpuid_level >= 0 ? ((ci->ci_signature >> 4) & 15) : 0,
            cpu_brand_string
        );
-
-       if (l > left)
-               return 0;
-       left -= l;
-       p += l;
+       size += l;
+       if (l < left) {
+               left -= l;
+               p += l;
+       } else
+               left = 0;
 
        if (cpuid_level >= 0)
                l = snprintf(p, left, "%d\n", ci->ci_signature & 15);
        else
                l = snprintf(p, left, "unknown\n");
 
-       if (l > left)
-               return 0;
-       left -= l;
-       p += l;
+       size += l;
+       if (l < left) {
+               left -= l;
+               p += l;
+       } else
+               left = 0;
 
        if (ci->ci_data.cpu_cc_freq != 0) {
                uint64_t freq, fraq;
@@ -199,10 +201,12 @@
        } else
                l = snprintf(p, left, "cpu MHz\t\t: unknown\n");
 
-       if (l > left)
-               return 0;
-       left -= l;
-       p += l;
+       size += l;
+       if (l < left) {
+               left -= l;
+               p += l;
+       } else
+               left = 0;
 
        l = snprintf(p, left,
            "fdiv_bug\t: %s\n"
@@ -217,12 +221,11 @@
            (rcr0() & CR0_WP) ? "yes" : "no",
            featurebuf
        );
+       size += l;
 
-       if (l > left)
-               return 0;
-       *len = (p + l) - bf;
-
-       return 1;
+       left = *len;
+       *len = size;
+       return left < *len ? -1 : 0;
 }
 
 #if defined(__HAVE_PROCFS_MACHDEP) && !defined(__x86_64__)



Home | Main Index | Thread Index | Old Index