From a38c532bce977a22f89894a94fefac3981db9d28 Mon Sep 17 00:00:00 2001 From: Piotr Sawicki Date: Tue, 18 Sep 2018 12:58:31 +0200 Subject: [PATCH] Don't try to read from closed connection socket After closing a connection, we should immeadiately return from processing method, as the socket descriptor struct has already been destroyed. Change-Id: Ie8912941ad4b504f0e1cde0441fc1a3cdc14710e --- src/ipc/channel.cpp | 22 +++++++++++----------- src/ipc/client-channel.cpp | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ipc/channel.cpp b/src/ipc/channel.cpp index 5336600..3cadd4a 100644 --- a/src/ipc/channel.cpp +++ b/src/ipc/channel.cpp @@ -71,18 +71,15 @@ int Channel::process(int fd, int mask) { return -ENOTCONN; } - while (true) { - auto it = std::find(desc.input.begin(), desc.input.end(), '\n'); - - if (it == desc.input.end()) { - if (desc.input.size() > MAX_MESSAGE_LENGTH) { - closeConnection(fd); - ALOGE("Message too long, length: " << desc.input.size() << " file descriptor: " << fd); - return -ENOTCONN; - } - break; - } + auto it = std::find(desc.input.begin(), desc.input.end(), '\n'); + if (it == desc.input.end()) { + if (desc.input.size() > MAX_MESSAGE_LENGTH) { + closeConnection(fd); + ALOGE("Message too long, length: " << desc.input.size() << " file descriptor: " << fd); + return -ENOTCONN; + } + } else { std::size_t len = static_cast(std::distance(desc.input.begin(), it)) + 1; if (len < MIN_MESSAGE_LENGTH || len > MAX_MESSAGE_LENGTH) { closeConnection(fd); @@ -97,6 +94,9 @@ int Channel::process(int fd, int mask) { parse(strMessage, message); int status = onReceive(fd, std::move(message)); + if (status == -ENOTCONN) { + return status; + } if (status < 0) { closeConnection(fd); ALOGE("Problem while processing received message for file descriptor: " << fd); diff --git a/src/ipc/client-channel.cpp b/src/ipc/client-channel.cpp index a1b3cfb..4b5b4f2 100644 --- a/src/ipc/client-channel.cpp +++ b/src/ipc/client-channel.cpp @@ -120,7 +120,7 @@ int ClientChannel::onReceive(int fd, std::vector &&message) { // after receiving the response just close the connection closeConnection(fd); - break; + return -ENOTCONN; } default : ALOGE("Client received unknown message, command: " << command); -- 2.7.4