[libcamera-devel,1/2] libcamera: Don't ignore the return value of read() and write()

Message ID 20190428231238.30547-1-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • [libcamera-devel,1/2] libcamera: Don't ignore the return value of read() and write()
Related show

Commit Message

Laurent Pinchart April 28, 2019, 11:12 p.m. UTC
The glibc read() and write() functions are defined with the
__warn_unused_result__ attribute when using FORTIFY_SOURCE. Don't ignore
their return value.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/event_dispatcher_poll.cpp |  8 ++++++--
 test/event.cpp                          | 13 +++++++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)

Comments

Jacopo Mondi April 28, 2019, 11:56 p.m. UTC | #1
HI Laurent,

On Mon, Apr 29, 2019 at 02:12:37AM +0300, Laurent Pinchart wrote:
> The glibc read() and write() functions are defined with the
> __warn_unused_result__ attribute when using FORTIFY_SOURCE. Don't ignore
> their return value.
>

Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/libcamera/event_dispatcher_poll.cpp |  8 ++++++--
>  test/event.cpp                          | 13 +++++++++++--
>  2 files changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp
> index 1f0f352a8e0a..2621b7d96b1e 100644
> --- a/src/libcamera/event_dispatcher_poll.cpp
> +++ b/src/libcamera/event_dispatcher_poll.cpp
> @@ -162,7 +162,9 @@ void EventDispatcherPoll::processEvents()
>  void EventDispatcherPoll::interrupt()
>  {
>  	uint64_t value = 1;
> -	write(eventfd_, &value, sizeof(value));
> +	ssize_t ret = write(eventfd_, &value, sizeof(value));
> +	if (ret < 0)
> +		LOG(Event, Error) << "Failed to interrupt event dispatcher";
>  }
>
>  short EventDispatcherPoll::EventNotifierSetPoll::events() const
> @@ -214,7 +216,9 @@ void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd)
>  		return;
>
>  	uint64_t value;
> -	read(eventfd_, &value, sizeof(value));
> +	ssize_t ret = read(eventfd_, &value, sizeof(value));
> +	if (ret < 0)
> +		LOG(Event, Error) << "Failed to process interrupt";
>  }
>
>  void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pollfds)
> diff --git a/test/event.cpp b/test/event.cpp
> index 52bc0c7e77f5..9bd876153a18 100644
> --- a/test/event.cpp
> +++ b/test/event.cpp
> @@ -38,6 +38,7 @@ protected:
>  		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
>  		std::string data("H2G2");
>  		Timer timeout;
> +		ssize_t ret;
>
>  		EventNotifier readNotifier(pipefd_[0], EventNotifier::Read);
>  		readNotifier.activated.connect(this, &EventTest::readReady);
> @@ -46,7 +47,11 @@ protected:
>  		memset(data_, 0, sizeof(data_));
>  		size_ = 0;
>
> -		write(pipefd_[1], data.data(), data.size());
> +		ret = write(pipefd_[1], data.data(), data.size());
> +		if (ret < 0) {
> +			cout << "Pipe write failed" << endl;
> +			return TestFail;
> +		}
>
>  		timeout.start(100);
>  		dispatcher->processEvents();
> @@ -73,7 +78,11 @@ protected:
>  		notified_ = false;
>  		readNotifier.setEnabled(false);
>
> -		write(pipefd_[1], data.data(), data.size());
> +		ret = write(pipefd_[1], data.data(), data.size());
> +		if (ret < 0) {
> +			cout << "Pipe write failed" << endl;
> +			return TestFail;
> +		}
>
>  		timeout.start(100);
>  		dispatcher->processEvents();
> --
> Regards,
>
> Laurent Pinchart
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Kieran Bingham April 29, 2019, 12:13 p.m. UTC | #2
Hi Laurent,

On 29/04/2019 00:12, Laurent Pinchart wrote:
> The glibc read() and write() functions are defined with the
> __warn_unused_result__ attribute when using FORTIFY_SOURCE. Don't ignore
> their return value.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/libcamera/event_dispatcher_poll.cpp |  8 ++++++--
>  test/event.cpp                          | 13 +++++++++++--
>  2 files changed, 17 insertions(+), 4 deletions(-)
> 
> diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp
> index 1f0f352a8e0a..2621b7d96b1e 100644
> --- a/src/libcamera/event_dispatcher_poll.cpp
> +++ b/src/libcamera/event_dispatcher_poll.cpp
> @@ -162,7 +162,9 @@ void EventDispatcherPoll::processEvents()
>  void EventDispatcherPoll::interrupt()
>  {
>  	uint64_t value = 1;
> -	write(eventfd_, &value, sizeof(value));
> +	ssize_t ret = write(eventfd_, &value, sizeof(value));> +	if (ret < 0)

Do we care if ret != sizeof(value)? (I think it must be highly unlikely
that the read/write call doesn't at least read the 64 bit value...)

> +		LOG(Event, Error) << "Failed to interrupt event dispatcher";

What about printing the strerror(errno), as if this ever actually
happens (even if unlikely) it would be useful...

>  }
>  
>  short EventDispatcherPoll::EventNotifierSetPoll::events() const
> @@ -214,7 +216,9 @@ void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd)
>  		return;
>  
>  	uint64_t value;
> -	read(eventfd_, &value, sizeof(value));
> +	ssize_t ret = read(eventfd_, &value, sizeof(value));
> +	if (ret < 0)
> +		LOG(Event, Error) << "Failed to process interrupt";


Same comments as above...

Could we ever expect ret == 0 on the read call (EOF?)? I presume as
we're checking with poll() in advance this should always have at least
one event to read.


I think the return values are probably fine, and the errno is optional
so with or without updates:


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>


>  }
>  
>  void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pollfds)
> diff --git a/test/event.cpp b/test/event.cpp
> index 52bc0c7e77f5..9bd876153a18 100644
> --- a/test/event.cpp
> +++ b/test/event.cpp
> @@ -38,6 +38,7 @@ protected:
>  		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
>  		std::string data("H2G2");
>  		Timer timeout;
> +		ssize_t ret;
>  
>  		EventNotifier readNotifier(pipefd_[0], EventNotifier::Read);
>  		readNotifier.activated.connect(this, &EventTest::readReady);
> @@ -46,7 +47,11 @@ protected:
>  		memset(data_, 0, sizeof(data_));
>  		size_ = 0;
>  
> -		write(pipefd_[1], data.data(), data.size());
> +		ret = write(pipefd_[1], data.data(), data.size());
> +		if (ret < 0) {
> +			cout << "Pipe write failed" << endl;
> +			return TestFail;
> +		}
>  
>  		timeout.start(100);
>  		dispatcher->processEvents();
> @@ -73,7 +78,11 @@ protected:
>  		notified_ = false;
>  		readNotifier.setEnabled(false);
>  
> -		write(pipefd_[1], data.data(), data.size());
> +		ret = write(pipefd_[1], data.data(), data.size());
> +		if (ret < 0) {
> +			cout << "Pipe write failed" << endl;
> +			return TestFail;
> +		}
>  
>  		timeout.start(100);
>  		dispatcher->processEvents();
>
Laurent Pinchart April 29, 2019, 12:25 p.m. UTC | #3
Hi Kieran,

On Mon, Apr 29, 2019 at 01:13:31PM +0100, Kieran Bingham wrote:
> On 29/04/2019 00:12, Laurent Pinchart wrote:
> > The glibc read() and write() functions are defined with the
> > __warn_unused_result__ attribute when using FORTIFY_SOURCE. Don't ignore
> > their return value.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/libcamera/event_dispatcher_poll.cpp |  8 ++++++--
> >  test/event.cpp                          | 13 +++++++++++--
> >  2 files changed, 17 insertions(+), 4 deletions(-)
> > 
> > diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp
> > index 1f0f352a8e0a..2621b7d96b1e 100644
> > --- a/src/libcamera/event_dispatcher_poll.cpp
> > +++ b/src/libcamera/event_dispatcher_poll.cpp
> > @@ -162,7 +162,9 @@ void EventDispatcherPoll::processEvents()
> >  void EventDispatcherPoll::interrupt()
> >  {
> >  	uint64_t value = 1;
> > -	write(eventfd_, &value, sizeof(value));
> > +	ssize_t ret = write(eventfd_, &value, sizeof(value));> +	if (ret < 0)
> 
> Do we care if ret != sizeof(value)? (I think it must be highly unlikely
> that the read/write call doesn't at least read the 64 bit value...)
> 
> > +		LOG(Event, Error) << "Failed to interrupt event dispatcher";
> 
> What about printing the strerror(errno), as if this ever actually
> happens (even if unlikely) it would be useful...

Good point. I'll fix this.

> >  }
> >  
> >  short EventDispatcherPoll::EventNotifierSetPoll::events() const
> > @@ -214,7 +216,9 @@ void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd)
> >  		return;
> >  
> >  	uint64_t value;
> > -	read(eventfd_, &value, sizeof(value));
> > +	ssize_t ret = read(eventfd_, &value, sizeof(value));
> > +	if (ret < 0)
> > +		LOG(Event, Error) << "Failed to process interrupt";
> 
> Same comments as above...
> 
> Could we ever expect ret == 0 on the read call (EOF?)? I presume as
> we're checking with poll() in advance this should always have at least
> one event to read.

Yes, there should also be data to read.

> I think the return values are probably fine, and the errno is optional
> so with or without updates:
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> >  }
> >  
> >  void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pollfds)
> > diff --git a/test/event.cpp b/test/event.cpp
> > index 52bc0c7e77f5..9bd876153a18 100644
> > --- a/test/event.cpp
> > +++ b/test/event.cpp
> > @@ -38,6 +38,7 @@ protected:
> >  		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
> >  		std::string data("H2G2");
> >  		Timer timeout;
> > +		ssize_t ret;
> >  
> >  		EventNotifier readNotifier(pipefd_[0], EventNotifier::Read);
> >  		readNotifier.activated.connect(this, &EventTest::readReady);
> > @@ -46,7 +47,11 @@ protected:
> >  		memset(data_, 0, sizeof(data_));
> >  		size_ = 0;
> >  
> > -		write(pipefd_[1], data.data(), data.size());
> > +		ret = write(pipefd_[1], data.data(), data.size());
> > +		if (ret < 0) {
> > +			cout << "Pipe write failed" << endl;
> > +			return TestFail;
> > +		}
> >  
> >  		timeout.start(100);
> >  		dispatcher->processEvents();
> > @@ -73,7 +78,11 @@ protected:
> >  		notified_ = false;
> >  		readNotifier.setEnabled(false);
> >  
> > -		write(pipefd_[1], data.data(), data.size());
> > +		ret = write(pipefd_[1], data.data(), data.size());
> > +		if (ret < 0) {
> > +			cout << "Pipe write failed" << endl;
> > +			return TestFail;
> > +		}
> >  
> >  		timeout.start(100);
> >  		dispatcher->processEvents();

Patch

diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp
index 1f0f352a8e0a..2621b7d96b1e 100644
--- a/src/libcamera/event_dispatcher_poll.cpp
+++ b/src/libcamera/event_dispatcher_poll.cpp
@@ -162,7 +162,9 @@  void EventDispatcherPoll::processEvents()
 void EventDispatcherPoll::interrupt()
 {
 	uint64_t value = 1;
-	write(eventfd_, &value, sizeof(value));
+	ssize_t ret = write(eventfd_, &value, sizeof(value));
+	if (ret < 0)
+		LOG(Event, Error) << "Failed to interrupt event dispatcher";
 }
 
 short EventDispatcherPoll::EventNotifierSetPoll::events() const
@@ -214,7 +216,9 @@  void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd)
 		return;
 
 	uint64_t value;
-	read(eventfd_, &value, sizeof(value));
+	ssize_t ret = read(eventfd_, &value, sizeof(value));
+	if (ret < 0)
+		LOG(Event, Error) << "Failed to process interrupt";
 }
 
 void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pollfds)
diff --git a/test/event.cpp b/test/event.cpp
index 52bc0c7e77f5..9bd876153a18 100644
--- a/test/event.cpp
+++ b/test/event.cpp
@@ -38,6 +38,7 @@  protected:
 		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
 		std::string data("H2G2");
 		Timer timeout;
+		ssize_t ret;
 
 		EventNotifier readNotifier(pipefd_[0], EventNotifier::Read);
 		readNotifier.activated.connect(this, &EventTest::readReady);
@@ -46,7 +47,11 @@  protected:
 		memset(data_, 0, sizeof(data_));
 		size_ = 0;
 
-		write(pipefd_[1], data.data(), data.size());
+		ret = write(pipefd_[1], data.data(), data.size());
+		if (ret < 0) {
+			cout << "Pipe write failed" << endl;
+			return TestFail;
+		}
 
 		timeout.start(100);
 		dispatcher->processEvents();
@@ -73,7 +78,11 @@  protected:
 		notified_ = false;
 		readNotifier.setEnabled(false);
 
-		write(pipefd_[1], data.data(), data.size());
+		ret = write(pipefd_[1], data.data(), data.size());
+		if (ret < 0) {
+			cout << "Pipe write failed" << endl;
+			return TestFail;
+		}
 
 		timeout.start(100);
 		dispatcher->processEvents();