@@ -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);
}
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(-)