[PATCH] Assorted bug fixes

Jindřich Makovička <[email protected]> Sat, 20 May 2017 14:20:24 +0200
Newsgroups gmane.comp.gnome.apps.gkrellm
Message-ID <20170520142024.441d4549@holly>
--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,

I am attaching couple of trivial fixes for bugs reported by recent
Clang & GCC & AddressSanitizer.

Regards,
-- 
Jindrich Makovicka

--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=0001-Unify-integer-types.patch

From 1fdc32fc367868e74cbc1c8dc602d711f2dccb1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= <[email protected]>
Date: Sat, 20 May 2017 09:23:16 +0200
Subject: [PATCH 1/6] Unify integer types

---
 server/monitor.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/server/monitor.c b/server/monitor.c
index 3e3eec9..1a7fa45 100644
--- a/server/monitor.c
+++ b/server/monitor.c
@@ -2018,7 +2018,7 @@ init_sensors_monitor(void)
 /* ======================================================= */
 static time_t	base_uptime,
 				up_seconds;
-static gulong	up_minutes = -1;
+static glong	up_minutes = -1;
 
 void
 gkrellm_uptime_set_base_uptime(time_t base)
@@ -2029,16 +2029,16 @@ gkrellm_uptime_set_base_uptime(time_t base)
 static void
 update_uptime(GkrellmdMonitor *mon, gboolean first_update)
 	{
-	gint	prev_up;
+	glong	prev_up;
 
 	if (GK.ten_second_tick || up_minutes < 0 || first_update)
 		{
 		prev_up = up_minutes;
 		up_seconds = gkrellm_sys_uptime_read_uptime();
 		if (up_seconds > 0)
-			up_minutes = (gint) (up_seconds / 60);
+			up_minutes = (glong) (up_seconds / 60);
 		else
-			up_minutes = (gint)(time(0) - _GK.start_time + base_uptime) / 60;
+			up_minutes = (glong)(time(0) - _GK.start_time + base_uptime) / 60;
 		if (up_minutes != prev_up)
 			gkrellmd_need_serve(mon);
 		}
@@ -2050,7 +2050,7 @@ serve_uptime_data(GkrellmdMonitor *mon, gboolean first_serve)
 	gchar	buf[128];
 
 	gkrellmd_set_serve_name(mon, "uptime");
-	snprintf(buf, sizeof(buf), "%lu\n", (gulong) up_minutes);
+	snprintf(buf, sizeof(buf), "%ld\n", (glong) (up_minutes >= 0 ? up_minutes : 0));
 	gkrellmd_serve_data(mon, buf);
 	}
 
-- 
2.11.0


--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0002-Compute-floating-point-absolute-value.patch

From 80baf76746f0c24e953c2104b7a6ddaf8f1fb687 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= <[email protected]>
Date: Sat, 20 May 2017 09:23:46 +0200
Subject: [PATCH 2/6] Compute floating point absolute value

---
 src/battery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/battery.c b/src/battery.c
index bacd3cd..40d4198 100644
--- a/src/battery.c
+++ b/src/battery.c
@@ -382,7 +382,7 @@ estimate_battery_time_left(Battery *bat)
 			/* charging, use exponential: eta =~ 2.5 * time-constant (~=92%) */
 			eta = -2.5 * dt/60 / (log(1 - (gdouble)dp/(gdouble)(p+dp)));
 		else
-			eta = abs((gdouble)p / rate);	/* use linear */
+			eta = fabs((gdouble)p / rate);	/* use linear */
 
 #ifdef BAT_ESTIMATE_DEBUG
 		fprintf(stderr, "eta = %.2f\t", eta);
-- 
2.11.0


--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0003-Fix-uninitialized-variable-usage.patch

From 9e698600c984d20c5a33f31c33ecf2b143939a5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= <[email protected]>
Date: Sat, 20 May 2017 09:24:00 +0200
Subject: [PATCH 3/6] Fix uninitialized variable usage

---
 src/fs.c  | 2 +-
 src/mem.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/fs.c b/src/fs.c
index 626bc74..6c73f41 100644
--- a/src/fs.c
+++ b/src/fs.c
@@ -460,7 +460,7 @@ fs_draw_decal_text(FSmon *fs, gint value)
 	GkrellmDecal		*d;
 	GkrellmTextstyle	ts_save;
 	gchar				buf[128];
-	gint				x_off, w;
+	gint				x_off, w = 0;
 
 	if (value == 0)
 		{
diff --git a/src/mem.c b/src/mem.c
index 2f85935..c130aa4 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -299,7 +299,7 @@ draw_decal_label(MeminfoMeter *mm, gint draw_to_screen)
 	GkrellmDecal		*d;
 	GkrellmTextstyle	ts_save;
 	gchar				buf[128];
-	gint				x_off, w;
+	gint				x_off, w = 0;
 
 	d = mm->decal_label;
 	if (! mm->label_is_data)
-- 
2.11.0


--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0004-Fix-potential-buffer-overflow.patch

From fedbd9b433671f032c817136d3aaef4fb6cc2857 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= <[email protected]>
Date: Sat, 20 May 2017 10:23:10 +0200
Subject: [PATCH 4/6] Fix potential buffer overflow

---
 src/sysdeps/linux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/sysdeps/linux.c b/src/sysdeps/linux.c
index 7f7e37d..460a03d 100644
--- a/src/sysdeps/linux.c
+++ b/src/sysdeps/linux.c
@@ -1525,7 +1525,7 @@ gkrellm_sys_fs_get_mounts_list(void)
 	while (fgets(buf, sizeof(buf), f))
 		{
 		dev[0] = dir[0] = type[0] = '\0';
-		sscanf(buf, "%512s %512s %127s", dev, dir, type);
+		sscanf(buf, "%511s %511s %127s", dev, dir, type);
 		fix_fstab_name(dev);
 		fix_fstab_name(dir);
 		fix_fstab_name(type);
-- 
2.11.0


--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0005-Avoid-calling-g_strstr_len-with-NULL-haystack.patch

From 021238ddf894a6c4bf789e2aa9f468f61a933735 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= <[email protected]>
Date: Sat, 20 May 2017 10:34:52 +0200
Subject: [PATCH 5/6] Avoid calling g_strstr_len with NULL haystack

---
 src/sysdeps/linux.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/sysdeps/linux.c b/src/sysdeps/linux.c
index 460a03d..99e7261 100644
--- a/src/sysdeps/linux.c
+++ b/src/sysdeps/linux.c
@@ -3157,6 +3157,7 @@ sensors_nvidia_smi_read(gboolean setup)
 				    || !strcmp(id, ":")
 				   )
 					continue;
+				stmp = str;
 				if ((str = g_strstr_len(str, -1, "Temperature")) != NULL)
 					{
 					str += 11;
@@ -3185,6 +3186,10 @@ sensors_nvidia_smi_read(gboolean setup)
 					else if ((smi = nvidia_smi_lookup(id)) != NULL)
 						smi->temp = temp;
 					}
+				else
+					{
+					str = stmp;
+					}
 				}
 			}
 	if (output)
-- 
2.11.0


--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7
Content-Type: text/x-patch
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=0006-Check-that-the-privileges-were-successfully-dropped.patch

From 6d34de36e2d5957d3e98e129f60e699385c951c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= <[email protected]>
Date: Sat, 20 May 2017 13:23:30 +0200
Subject: [PATCH 6/6] Check that the privileges were successfully dropped

---
 server/main.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/server/main.c b/server/main.c
index 8851e13..1c1f22e 100644
--- a/server/main.c
+++ b/server/main.c
@@ -1124,18 +1124,28 @@ detach_from_terminal(void)
 #endif /* !defined(WIN32) */
 
 
-static void
+static int
 drop_privileges(void)
 	{
 #if !defined(WIN32)
+	int r;
 	if (drop_privs.gid > (uid_t)0)
 		{
-		(void) setgroups((size_t)0, (gid_t*)0);
-		(void) setgid(drop_privs.gid);
+		r = setgroups((size_t)0, (gid_t*)0);
+		if (r != 0)
+			return r;
+		r = setgid(drop_privs.gid);
+		if (r != 0)
+			return r;
 		}
 	if (drop_privs.uid > (uid_t)0)
-		(void) setuid(drop_privs.uid);
+		{
+		r = setuid(drop_privs.uid);
+		if (r != 0)
+			return r;
+		}
 #endif
+	return 0;
 	}
 
 
@@ -1201,7 +1211,12 @@ gkrellmd_run(gint argc, gchar **argv)
 
 	make_pidfile();
 	gkrellm_sys_main_init();
-	drop_privileges();
+	if (drop_privileges() != 0)
+		{
+		g_warning("Failed to drop privileges: %s\n", strerror(errno));
+		gkrellm_sys_main_cleanup();
+		return 1;
+		}
 
 	_GK.start_time = time(0);
 	if (_GK.update_HZ < 1 || _GK.update_HZ > 10)
-- 
2.11.0


--MP_/2O6JJ5Z_hj4UCwi4SRRS/i7--