pkgsrc-WIP-changes archive

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

bsdfetch: bug fix.



Module Name:	pkgsrc-wip
Committed By:	Paolo Vincenzo Olivo <vms%retrobsd.ddns.net@localhost>
Pushed By:	vms
Date:		Mon Dec 5 10:06:34 2022 +0100
Changeset:	61f73b002ef71315a79f0393546781b63429305d

Modified Files:
	bsdfetch/distinfo
	bsdfetch/patches/patch-bsdfetch.c

Log Message:
bsdfetch: bug fix.

* Fix unintentionally introduced bug with snprintf() lacking size of
stored buffer as second argument.

* Finish replacing sprintf with snprintf.

* Increase size of buf[],tmp[] arrays to reflect changes (MAXCPU(S)) may be as
  high as `256' on certain architectures).

* Define size of buf[] and tmp[] in main(), so that we don't have do to
  it multiple times.

* Include required machine-dependent paramh/cpu.h.

* In get_loadavg(), initialize all lavg[] elements beforehand, in case
  getloadvg() returned fewer elements than requested.

To see a diff of this commit:
https://wip.pkgsrc.org/cgi-bin/gitweb.cgi?p=pkgsrc-wip.git;a=commitdiff;h=61f73b002ef71315a79f0393546781b63429305d

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

diffstat:
 bsdfetch/distinfo                 |   2 +-
 bsdfetch/patches/patch-bsdfetch.c | 116 ++++++++++++++++++++++++++++----------
 2 files changed, 88 insertions(+), 30 deletions(-)

diffs:
diff --git a/bsdfetch/distinfo b/bsdfetch/distinfo
index 78dd545022..c69aa3ee45 100644
--- a/bsdfetch/distinfo
+++ b/bsdfetch/distinfo
@@ -4,5 +4,5 @@ BLAKE2s (bsdfetch-0.9-9840c3153fcf49150ebcee94f527f4ebcc0b8c1b.tar.gz) = ee4bd56
 SHA512 (bsdfetch-0.9-9840c3153fcf49150ebcee94f527f4ebcc0b8c1b.tar.gz) = b62d956f25eacc2426743c09523e89ade002fadd44445aa5c2628425b10d45d0133354be765af17fac78a936436197a8de5b02efd9c670dcdcf9513f0b744bb3
 Size (bsdfetch-0.9-9840c3153fcf49150ebcee94f527f4ebcc0b8c1b.tar.gz) = 6794 bytes
 SHA1 (patch-Makefile) = 62812c29b42d05ad9a9b1b9cc263ec309a7a4771
-SHA1 (patch-bsdfetch.c) = b9e1d1759fd54708a3e7610e1d27b51c5b57158d
+SHA1 (patch-bsdfetch.c) = 702dae9a5509be615d36e76a2660cb0b0487dbf9
 SHA1 (patch-sysctlbyname.c) = d00d3466cc3be82c7cf01ecfb6b5b12099adcc44
diff --git a/bsdfetch/patches/patch-bsdfetch.c b/bsdfetch/patches/patch-bsdfetch.c
index 1dfe92a560..ab84d50710 100644
--- a/bsdfetch/patches/patch-bsdfetch.c
+++ b/bsdfetch/patches/patch-bsdfetch.c
@@ -4,7 +4,7 @@ Stability and portability fixes.
 
 --- bsdfetch.c.orig	2022-11-30 09:00:21.000000000 +0000
 +++ bsdfetch.c
-@@ -18,6 +18,8 @@
+@@ -18,11 +18,13 @@
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
@@ -13,7 +13,27 @@ Stability and portability fixes.
  #include <sys/types.h>
  #include <sys/sysctl.h>
  #include <sys/utsname.h>
-@@ -45,20 +47,11 @@
+ #include <dlfcn.h>
+-#if defined(__FreeBSD__) || defined(__DragonFly__)
++#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__MidnightBSD__)
+ #include <time.h>
+ #include <sys/vmmeter.h>
+ #include <vm/vm_param.h>
+@@ -32,6 +34,13 @@
+ #ifdef __OpenBSD__
+ #include "sysctlbyname.h"
+ #include <sys/sensors.h>
++#include <machine/cpu.h>
++#else
++#include <machine/param.h>
++#endif
++
++#ifndef MAXCPUS
++#define MAXCPUS MAXCPU
+ #endif
+ 
+ #define _PRG_NAME "bsdfetch"
+@@ -45,20 +54,13 @@
  
  #define _SILENT (void)
  
@@ -31,13 +51,15 @@ Stability and portability fixes.
 -
  typedef unsigned int uint;
  
-+char tmp[100] = {0};
-+char buf[100] = {0};
++char tmp[256] = {0};
++char buf[256] = {0};
++size_t tmp_size = 0;
++size_t buf_size = 0;
 +
  int color_flag = 1;
  
  static void die(int err_num, int line);
-@@ -95,55 +88,70 @@ static void show(const char *entry, cons
+@@ -95,57 +97,72 @@ static void show(const char *entry, cons
  }
  
  static void get_shell() {
@@ -87,12 +109,12 @@ Stability and portability fixes.
 +	uint ncpu_max = 0;
  	char cpu_type[200] = {0};
 -	char tmp[100] = {0};
- 
+-
 -	num_cpu_size = sizeof(num_cpu);
 -
 -	if(sysctlbyname("hw.ncpu", &num_cpu, &num_cpu_size, NULL, 0) == -1)
 -		die(errno, __LINE__);
--
+ 
 -#if defined(__NetBSD__)
 -	FILE *fc = NULL;
 -
@@ -121,7 +143,7 @@ Stability and portability fixes.
  	show("CPU", cpu_type);
  
 -	_SILENT sprintf(tmp, "%d", num_cpu);
-+	_SILENT sprintf(tmp, "%d of %d processors online", ncpu, ncpu_max);
++	_SILENT snprintf(tmp, MAXCPUS, "%d of %d processors online", ncpu, ncpu_max);
  
  	show("Cores", tmp);
  
@@ -132,16 +154,32 @@ Stability and portability fixes.
  		int temperature = 0;
  
 -		sprintf(buf, "dev.cpu.%d.temperature", i);
-+		snprintf(buf, "dev.cpu.%d.temperature", i);
- 
+-
  		temperature_size = sizeof(buf);
++		snprintf(buf, temperature_size, "dev.cpu.%d.temperature", i);
++
  		if(sysctlbyname(buf, &temperature, &temperature_size, NULL, 0) == -1)
-@@ -171,19 +179,16 @@ static void get_cpu() {
+ 			return;
+ 
+@@ -158,6 +175,7 @@ static void get_cpu() {
+ 	int mib[5];
+ 	char temp[10] = {0};
+ 	size_t size = 0;
++	size_t temp_size = 0;
+ 	struct sensor sensors;
+ 
+ 	mib[0] = CTL_HW;
+@@ -167,186 +185,104 @@ static void get_cpu() {
+ 	mib[4] = 0;
+ 
+ 	size = sizeof(sensors);
++	temp_size = sizeof(temp);
+ 
  	if(sysctl(mib, 5, &sensors, &size, NULL, 0) < 0)
  		return;
  
 -	_SILENT sprintf(temp, "%d °C", (int)((float)(sensors.value - 273150000) / 1E6));
-+	_SILENT snprintf(temp, "%d °C", (int)((float)(sensors.value - 273150000) / 1E6));
++	_SILENT snprintf(temp, temp_size, "%d °C", (int)((float)(sensors.value - 273150000) / 1E6));
  
  	show("CPU Temp", temp);
  #endif
@@ -150,23 +188,25 @@ Stability and portability fixes.
  static void get_loadavg() {
 -	char tmp[20] = {0};
 -	double *lavg = NULL;
-+	double lavg[3];
++	double lavg[3] = { 0.0 };
  
 -	lavg = malloc(sizeof(double) * 3);
--
--	(void)getloadavg(lavg, -1);
 +	(void)getloadavg(lavg, 3);
  
- 	_SILENT sprintf(tmp, "%.2lf %.2lf %.2lf", lavg[0], lavg[1], lavg[2]);
+-	(void)getloadavg(lavg, -1);
+-
+-	_SILENT sprintf(tmp, "%.2lf %.2lf %.2lf", lavg[0], lavg[1], lavg[2]);
++	_SILENT snprintf(tmp, tmp_size, "%.2lf %.2lf %.2lf", lavg[0], lavg[1], lavg[2]);
  
-@@ -191,120 +196,45 @@ static void get_loadavg() {
+ 	show("Loadavg", tmp);
  }
  
  static void get_packages() {
 -#if defined(__MidnightBSD__)
--	FILE *f = NULL;
+ 	FILE *f = NULL;
 -	char buf[10] = {0};
--
++	size_t npkg = 0;
+ 
 -	/*
 -	  It might be better to use the mport stats functionality long term, but this
 -	  avoids parsing.
@@ -223,10 +263,9 @@ Stability and portability fixes.
 -	sprintf(buf, "%d", numpkg);
 -	show("Packages", buf);
 -#elif defined(__OpenBSD__) || defined(__NetBSD__)
- 	FILE *f = NULL;
+-	FILE *f = NULL;
 -	char buf[10] = {0};
-+	size_t npkg = 0;
- 
+-
 -	/*
 -		This is a little hacky for the moment. I don't
 -		see another good solution to get the package
@@ -271,7 +310,7 @@ Stability and portability fixes.
 -	fgets(buf, sizeof(buf), fp);
 -	pclose(fp);
 -
-+	snprintf(buf, sizeof buf, "%zu", npkg);
++	snprintf(buf, buf_size, "%zu", npkg);
  	show("Packages", buf);
 -#endif
  }
@@ -299,7 +338,13 @@ Stability and portability fixes.
  	days = up / 86400;
  	up %= 86400;
  	hours = up / 3600;
-@@ -317,25 +247,20 @@ static void get_uptime() {
+ 	up %= 3600;
+ 	minutes = up / 60;
+ 
+-	_SILENT sprintf(buf, "%dd %dh %dm", days, hours, minutes);
++	_SILENT snprintf(buf, buf_size, "%dd %dh %dm", days, hours, minutes);
+ 
+ 	show("Uptime", buf);
  }
  
  static void get_memory() {
@@ -308,11 +353,11 @@ Stability and portability fixes.
  	unsigned long long mem = 0;
 -	char tmp[50] = {0};
 -	size_t buf_size = 0;
--
--	buf_size = sizeof(buf);
 +	const long pgsize = sysconf(_SC_PAGESIZE);
 +	const long pages = sysconf(_SC_PHYS_PAGES);
  
+-	buf_size = sizeof(buf);
+-
 -#if defined(__FreeBSD__) || defined(__MidnightBSD__)
 -	if(sysctlbyname("hw.realmem", &buf, &buf_size, NULL, 0) == -1)
 -		die(errno, __LINE__);
@@ -333,9 +378,11 @@ Stability and portability fixes.
 +	else
 +		mem = buff / 1048576;
  
- 	_SILENT sprintf(tmp, "%llu MB", mem);
+-	_SILENT sprintf(tmp, "%llu MB", mem);
++	_SILENT snprintf(tmp, tmp_size, "%llu MB", mem);
  
-@@ -344,9 +269,9 @@ static void get_memory() {
+ 	show("RAM", tmp);
+ }
  
  static void get_hostname() {
  	long host_size_max = 0;
@@ -347,7 +394,7 @@ Stability and portability fixes.
  	if(gethostname(hostname, host_size_max) == -1)
  		die(errno, __LINE__);
  
-@@ -354,20 +279,12 @@ static void get_hostname() {
+@@ -354,20 +290,12 @@ static void get_hostname() {
  }
  
  static void get_arch() {
@@ -371,3 +418,14 @@ Stability and portability fixes.
  }
  
  static void get_sysinfo() {
+@@ -400,6 +328,10 @@ static void usage() {
+ }
+ 
+ int main(int argc, char **argv) {
++	
++	tmp_size = sizeof(tmp);    
++	buf_size = sizeof(buf);
++
+ 	int is_a_tty = 0;
+ 
+ 	is_a_tty = isatty(1);


Home | Main Index | Thread Index | Old Index