iio.c: fix memory leak and out of bound array access issue
Fix memory leak in mraa_iio_update_channels() and mraa_iio_get_channel_data(). In mraa_iio_update_channels(), we add checking for out of bound array access. Signed-off-by: Lay, Kuan Loon <kuan.loon.lay@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
4ffb094063
commit
d5233adbf9
4
src/iio/iio.c
Normal file → Executable file
4
src/iio/iio.c
Normal file → Executable file
@@ -154,6 +154,7 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
||||||
syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
|
syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
|
||||||
|
free(str);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
||||||
@@ -577,6 +578,7 @@ mraa_iio_update_channels(mraa_iio_context dev)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
chan_num = ((int) strtol(readbuf, NULL, 10));
|
chan_num = ((int) strtol(readbuf, NULL, 10));
|
||||||
|
if (chan_num >= 0 && chan_num < dev->chan_num) {
|
||||||
chan = &dev->channels[chan_num];
|
chan = &dev->channels[chan_num];
|
||||||
chan->index = chan_num;
|
chan->index = chan_num;
|
||||||
close(fd);
|
close(fd);
|
||||||
@@ -589,6 +591,7 @@ mraa_iio_update_channels(mraa_iio_context dev)
|
|||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
||||||
syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
|
syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
|
||||||
|
free(str);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
||||||
@@ -604,6 +607,7 @@ mraa_iio_update_channels(mraa_iio_context dev)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user