From 1f89a9d1a502a7dfa37dbd0de18db35468314016 Mon Sep 17 00:00:00 2001 From: Michel Promonet Date: Thu, 2 Apr 2015 22:14:34 +0200 Subject: [PATCH] avoid stack overflow when no data is available --- inc/V4l2DeviceSource.h | 3 ++- src/V4l2DeviceSource.cpp | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/inc/V4l2DeviceSource.h b/inc/V4l2DeviceSource.h index d768d23..a8c05c0 100644 --- a/inc/V4l2DeviceSource.h +++ b/inc/V4l2DeviceSource.h @@ -76,7 +76,8 @@ class V4L2DeviceSource: public FramedSource void* thread(); static void deliverFrameStub(void* clientData) {((V4L2DeviceSource*) clientData)->deliverFrame();}; void deliverFrame(); - static void incomingPacketHandlerStub(void* clientData, int mask) { ((V4L2DeviceSource*) clientData)->getNextFrame(); }; + static void incomingPacketHandlerStub(void* clientData, int mask) { ((V4L2DeviceSource*) clientData)->incomingPacketHandler(); }; + void incomingPacketHandler(); int getNextFrame(); void processFrame(char * frame, int frameSize, const timeval &ref); void queueFrame(char * frame, int frameSize, const timeval &tv); diff --git a/src/V4l2DeviceSource.cpp b/src/V4l2DeviceSource.cpp index fbbc694..32b023f 100644 --- a/src/V4l2DeviceSource.cpp +++ b/src/V4l2DeviceSource.cpp @@ -175,12 +175,24 @@ void V4L2DeviceSource::deliverFrame() } pthread_mutex_unlock (&m_mutex); - // send Frame to the consumer - FramedSource::afterGetting(this); + if (fFrameSize > 0) + { + // send Frame to the consumer + FramedSource::afterGetting(this); + } } } // FrameSource callback on read event +void V4L2DeviceSource::incomingPacketHandler() +{ + if (this->getNextFrame() <= 0) + { + handleClosure(this); + } +} + +// read from device int V4L2DeviceSource::getNextFrame() { char buffer[m_device->getBufferSize()]; @@ -191,12 +203,10 @@ int V4L2DeviceSource::getNextFrame() if (frameSize < 0) { LOG(NOTICE) << "V4L2DeviceSource::getNextFrame errno:" << errno << " " << strerror(errno); - handleClosure(this); } else if (frameSize == 0) { LOG(NOTICE) << "V4L2DeviceSource::getNextFrame no data errno:" << errno << " " << strerror(errno); - handleClosure(this); } else {