From: Andy Ross Date: Thu, 26 Sep 2013 19:31:26 +0000 (-0700) Subject: libsensors: correct read() usage X-Git-Tag: android-x86-6.0-r1~21 X-Git-Url: http://git.osdn.net/view?p=android-x86%2Fhardware-intel-libsensors.git;a=commitdiff_plain;h=17fd865ff6c4965cf37f0d7e76e0acc56f8061e9 libsensors: correct read() usage The readEvents() implementation would attempt to read buffer_len (two by default) IIO events from the device, but would only ever process one, leading to dropped events in the case of overflow. It was also not checking the return value from read and would push garbage data on I/O errors (probably would not happen in regular usage, but IIO devices are known to return -EBUSY or -EINVAL if there are other users or if the sysfs parameters are set incorrectly). Change-Id: I7f66f530c9fce0540f1596456b3ca191406bbd27 For: AXIA-3287 Signed-off-by: Andy Ross Reviewed-on: https://otc-android.intel.com/gerrit/22150 Reviewed-by: Daniel Leung Reviewed-by: Russell Webb Tested-by: jenkins autobuilder --- diff --git a/common/libsensors/SensorIIODev.cpp b/common/libsensors/SensorIIODev.cpp index 9259b14..faf7fdd 100644 --- a/common/libsensors/SensorIIODev.cpp +++ b/common/libsensors/SensorIIODev.cpp @@ -623,13 +623,16 @@ int SensorIIODev::readEvents(sensors_event_t *data, int count){ *data = mPendingEvent; return 1; } - read_size = read(mFd, raw_buffer, datum_size * buffer_len); + read_size = read(mFd, raw_buffer, datum_size); numEventReceived = 0; - if (processEvent(raw_buffer, datum_size) >= 0){ - mPendingEvent.timestamp = getTimestamp(); - mPendingEvent.timestamp = getTimestamp(); - *data = mPendingEvent; - numEventReceived++; + if(read_size != datum_size) { + ALOGE("read() error (or short count) from IIO device: %d\n", errno); + } else { + if (processEvent(raw_buffer, datum_size) >= 0) { + mPendingEvent.timestamp = getTimestamp(); + *data = mPendingEvent; + numEventReceived++; + } } return numEventReceived; }