Don't try to read from closed connection socket 42/189542/2
authorPiotr Sawicki <p.sawicki2@partner.samsung.com>
Tue, 18 Sep 2018 10:58:31 +0000 (12:58 +0200)
committerPiotr Sawicki <p.sawicki2@partner.samsung.com>
Wed, 19 Sep 2018 06:35:48 +0000 (08:35 +0200)
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
src/ipc/client-channel.cpp

index 5336600..3cadd4a 100644 (file)
@@ -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::size_t>(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);
index a1b3cfb..4b5b4f2 100644 (file)
@@ -120,7 +120,7 @@ int ClientChannel::onReceive(int fd, std::vector<std::string> &&message) {
             // after receiving the response just close the connection
             closeConnection(fd);
 
-            break;
+            return -ENOTCONN;
         }
     default :
         ALOGE("Client received unknown message, command: " << command);