Private
Public Access
2
0

mraa.c: Fix buffer overfow in mraa_find_i2c_bus

All files in sysfs are 4K minimum so allocate a 4k buffer to hold the result -
even if it'll never be that big

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2015-07-15 13:50:28 +01:00
parent 9c93eeb323
commit a5d13670f4

View File

@@ -453,7 +453,7 @@ mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW*
int
mraa_find_i2c_bus(const char* devname, int startfrom)
{
char path[64], value[64];
char path[64];
int fd;
int i = startfrom;
int ret = -1;
@@ -493,15 +493,23 @@ mraa_find_i2c_bus(const char* devname, int startfrom)
close(fd);
break;
}
char* value = malloc(size);
if (value == NULL) {
syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
close(fd);
break;
}
ssize_t r = read(fd, value, size);
if (r > 0) {
if (strcasestr(value, devname) != NULL) {
free(value);
close(fd);
return i;
}
} else {
syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
}
free(value);
close(fd);
}
} else {