From 1e5fefa045cfc666b329f87efcdc03d171445b13 Mon Sep 17 00:00:00 2001 From: "Mark M. Hoffman" Date: Tue, 16 Aug 2005 01:16:59 +0000 Subject: [PATCH] Please use safer string handling in any code you're already touching. http://www.gratisoft.us/todd/papers/strlcpy.html git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@3057 7894878c-1315-0410-8ee3-d5d059ff63e0 --- lib/proc.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/proc.c b/lib/proc.c index d7446308..e04a12d8 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -142,19 +142,22 @@ int sensors_read_proc_chips(void) while (fgets(n, NAME_MAX, f)) { char *fstype = dirname; /* alias to keep the code readable */ - if (sscanf(n, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfs, fstype) == 2 - && !strcasecmp(fstype, "sysfs")) { + if (sscanf(n, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfsmount, fstype) + == 2 && !strcasecmp(fstype, "sysfs")) { foundsysfs++; break; } } fclose(f); - if (! foundsysfs) + if (!foundsysfs) { + memset(sysfsmount, '\0', sizeof(sysfsmount)); goto proc; + } /* Try /sys/class/hwmon first (Linux 2.6.14 and up) */ - strcpy(sysfsmount, sysfs); - strcat(sysfs, "/class/hwmon"); + strncpy(sysfs, sysfsmount, sizeof(sysfs) - 1); + sysfs[sizeof(sysfs) - 1] = '\0'; + strncat(sysfs, "/class/hwmon", sizeof(sysfs) - strlen(sysfs) - 1); dir = opendir(sysfs); if (! dir) @@ -200,8 +203,9 @@ int sensors_read_proc_chips(void) oldsys: /* Fall back to /sys/bus/i2c (Linux 2.5 to 2.6.13) */ - strcpy(sysfs, sysfsmount); - strcat(sysfs, "/bus/i2c/devices"); + strncpy(sysfs, sysfsmount, sizeof(sysfs) - 1); + sysfs[sizeof(sysfs) - 1] = '\0'; + strncat(sysfs, "/bus/i2c/devices", sizeof(sysfs) - strlen(sysfs) - 1); dir = opendir(sysfs); if (! dir)