Module Name:    src
Committed By:   christos
Date:           Sat Apr  5 18:43:09 UTC 2014

Modified Files:
        src/sys/arch/x86/x86: procfs_machdep.c

Log Message:
make this compute the needed size instead of bailing.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/x86/x86/procfs_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/procfs_machdep.c
diff -u src/sys/arch/x86/x86/procfs_machdep.c:1.5 src/sys/arch/x86/x86/procfs_machdep.c:1.6
--- src/sys/arch/x86/x86/procfs_machdep.c:1.5	Thu Mar 27 14:22:56 2014
+++ src/sys/arch/x86/x86/procfs_machdep.c	Sat Apr  5 14:43:09 2014
@@ -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 @@ static const char * const x86_features[]
 #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 @@ static int	procfs_getonecpu(int, struct 
  * 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 @@ procfs_getonecpu(int xcpu, struct cpu_in
 	    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 @@ procfs_getonecpu(int xcpu, struct cpu_in
 	} 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 @@ procfs_getonecpu(int xcpu, struct cpu_in
 	    (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__)

Reply via email to