diff mbox series

i2c-tools: Consistently use snprintf instead of sprintf

Message ID 20181205165913.703a505b@endymion
State Awaiting Upstream
Headers show
Series i2c-tools: Consistently use snprintf instead of sprintf | expand

Commit Message

Jean Delvare Dec. 5, 2018, 3:59 p.m. UTC
There was a last instance of sprintf which somehow was not converted.
I know it can't fail, but for consistency, let's use snprintf and
check for truncation as we already do in gather_i2c_busses().

Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
 tools/i2cbusses.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
diff mbox series

Patch

--- i2c-tools.orig/tools/i2cbusses.c	2018-11-22 14:30:31.117726899 +0100
+++ i2c-tools/tools/i2cbusses.c	2018-12-05 16:46:30.110315579 +0100
@@ -406,14 +406,21 @@  int parse_i2c_address(const char *addres
 
 int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)
 {
-	int file;
+	int file, len;
 
-	snprintf(filename, size, "/dev/i2c/%d", i2cbus);
-	filename[size - 1] = '\0';
+	len = snprintf(filename, size, "/dev/i2c/%d", i2cbus);
+	if (len >= (int)size) {
+		fprintf(stderr, "%s: path truncated\n", filename);
+		return -EOVERFLOW;
+	}
 	file = open(filename, O_RDWR);
 
 	if (file < 0 && (errno == ENOENT || errno == ENOTDIR)) {
-		sprintf(filename, "/dev/i2c-%d", i2cbus);
+		len = snprintf(filename, size, "/dev/i2c-%d", i2cbus);
+		if (len >= (int)size) {
+			fprintf(stderr, "%s: path truncated\n", filename);
+			return -EOVERFLOW;
+		}
 		file = open(filename, O_RDWR);
 	}