Patchwork [06/12] lib: fwts_battery: use strncpy instead of strcpy

login
register
mail settings
Submitter Colin King
Date Nov. 4, 2012, 10:19 p.m.
Message ID <1352067590-11820-7-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/197104/
State Rejected
Headers show

Comments

Colin King - Nov. 4, 2012, 10:19 p.m.
From: Colin Ian King <colin.king@canonical.com>

Use the safer strncpy instead of strcpy.  This means we also
need to add the size of the name paramater in fwts_battery_get_name.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/acpi/battery/battery.c     |    2 +-
 src/lib/include/fwts_battery.h |    2 +-
 src/lib/src/fwts_battery.c     |   17 ++++++++++-------
 3 files changed, 12 insertions(+), 9 deletions(-)

Patch

diff --git a/src/acpi/battery/battery.c b/src/acpi/battery/battery.c
index 79e30e2..ab6dd33 100644
--- a/src/acpi/battery/battery.c
+++ b/src/acpi/battery/battery.c
@@ -235,7 +235,7 @@  static void do_battery_test(fwts_framework *fw, int index)
 
 	*state = '\0';
 
-	fwts_battery_get_name(fw, index, name);
+	fwts_battery_get_name(fw, index, name, sizeof(name));
 
 	fwts_log_info(fw, "Test battery '%s'.", name);
 
diff --git a/src/lib/include/fwts_battery.h b/src/lib/include/fwts_battery.h
index 1cdfdc1..3d74088 100644
--- a/src/lib/include/fwts_battery.h
+++ b/src/lib/include/fwts_battery.h
@@ -31,6 +31,6 @@  bool fwts_battery_check_trip_point_support(fwts_framework *fw, const int index);
 int fwts_battery_set_trip_point(fwts_framework *fw, const int index, const int trip_point);
 int fwts_battery_get_trip_point(fwts_framework *fw, const int index, int *trip_point);
 int fwts_battery_get_capacity(fwts_framework *fw, const int type, const int index, uint32_t *capacity_mAh, uint32_t *capacity_mWh);
-int fwts_battery_get_name(fwts_framework *fw, const int index, char *name);
+int fwts_battery_get_name(fwts_framework *fw, const int index, char *name, const size_t name_len);
 
 #endif
diff --git a/src/lib/src/fwts_battery.c b/src/lib/src/fwts_battery.c
index fc231c5..b5a728e 100644
--- a/src/lib/src/fwts_battery.c
+++ b/src/lib/src/fwts_battery.c
@@ -211,7 +211,8 @@  static int fwts_battery_get_count_proc_fs(DIR *dir, int *count)
 static int fwts_battery_get_name_sys_fs(
 	DIR *dir,
 	const int index,
-	char *name)
+	char *name,
+	const size_t name_len)
 {
 	struct dirent *entry;
 	char path[PATH_MAX];
@@ -237,7 +238,7 @@  static int fwts_battery_get_name_sys_fs(
 			if (!match)
 				continue;
 
-			strcpy(name, entry->d_name);
+			strncpy(name, entry->d_name, name_len);
 			return FWTS_OK;
 		}
 	} while (entry);
@@ -248,7 +249,8 @@  static int fwts_battery_get_name_sys_fs(
 static int fwts_battery_get_name_proc_fs(
 	DIR *dir,
 	const int index,
-	char *name)
+	char *name,
+	const size_t name_len)
 {
 	struct dirent *entry;
 	int i = 0;
@@ -261,7 +263,7 @@  static int fwts_battery_get_name_proc_fs(
 			if (!match)
 				continue;
 
-			strcpy(name, entry->d_name);
+			strncpy(name, entry->d_name, name_len);
 			return FWTS_OK;
 		}
 	} while (entry);
@@ -628,7 +630,8 @@  int fwts_battery_get_cycle_count(
 int fwts_battery_get_name(
 	fwts_framework *fw,
 	const int index,
-	char *name)
+	char *name,
+	const size_t name_len)
 {
 	int ret;
 	DIR *dir;
@@ -636,10 +639,10 @@  int fwts_battery_get_name(
 	FWTS_UNUSED(fw);
 
 	if ((dir = opendir(FWTS_SYS_CLASS_POWER_SUPPLY)) != NULL) {
-		ret = fwts_battery_get_name_sys_fs(dir, index, name);
+		ret = fwts_battery_get_name_sys_fs(dir, index, name, name_len);
 		closedir(dir);
 	} else if ((dir = opendir(FWTS_PROC_ACPI_BATTERY)) != NULL) {
-		ret = fwts_battery_get_name_proc_fs(dir, index, name);
+		ret = fwts_battery_get_name_proc_fs(dir, index, name, name_len);
 		closedir(dir);
 	} else {
 		return FWTS_ERROR;