1From dae89c4e97faf408394961c0f4b1577a7d5976cc Mon Sep 17 00:00:00 2001
2Message-ID: <dae89c4e97faf408394961c0f4b1577a7d5976cc.1687508149.git.stefan@agner.ch>
3In-Reply-To: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
4References: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
5From: Nate Karstens <[email protected]>
6Date: Thu, 10 Aug 2017 08:27:32 -0500
7Subject: [PATCH] Handle errors from socket calls
8
9Adds handling for socket() or read() returning a
10negative value (indicating an error has occurred).
11
12Upstream-Status: Submitted [[email protected]]
13
14Signed-off-by: Nate Karstens <[email protected]>
15Signed-off-by: Alex Kiernan <[email protected]>
16---
17 mDNSPosix/mDNSPosix.c | 12 +++++++++---
18 1 file changed, 9 insertions(+), 3 deletions(-)
19
20diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
21index 010f266..89e108f 100644
22--- a/mDNSPosix/mDNSPosix.c
23+++ b/mDNSPosix/mDNSPosix.c
24@@ -1677,7 +1677,7 @@ mDNSlocal void          ProcessRoutingNotification(int sd, GenLinkedList *change
25 // Read through the messages on sd and if any indicate that any interface records should
26 // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0.
27 {
28-    ssize_t readCount;
29+    ssize_t readVal, readCount;
30     char buff[4096];
31     struct nlmsghdr         *pNLMsg = (struct nlmsghdr*) buff;
32
33@@ -1686,7 +1686,10 @@ mDNSlocal void          ProcessRoutingNotification(int sd, GenLinkedList *change
34     // enough to hold all pending data and so avoid message fragmentation.
35     // (Note that FIONREAD is not supported on AF_NETLINK.)
36
37-    readCount = read(sd, buff, sizeof buff);
38+    readVal = read(sd, buff, sizeof buff);
39+    if (readVal < 0) return;
40+    readCount = readVal;
41+
42     while (1)
43     {
44         // Make sure we've got an entire nlmsghdr in the buffer, and payload, too.
45@@ -1702,7 +1705,9 @@ mDNSlocal void          ProcessRoutingNotification(int sd, GenLinkedList *change
46                 pNLMsg = (struct nlmsghdr*) buff;
47
48                 // read more data
49-                readCount += read(sd, buff + readCount, sizeof buff - readCount);
50+                readVal = read(sd, buff + readCount, sizeof buff - readCount);
51+                if (readVal < 0) return;
52+                readCount += readVal;
53                 continue;                   // spin around and revalidate with new readCount
54             }
55             else
56@@ -2017,6 +2022,7 @@ mDNSlocal mDNSBool mDNSPlatformInit_CanReceiveUnicast(void)
57     int err;
58     int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
59     struct sockaddr_in s5353;
60+    if (s < 0) return mDNSfalse;
61     s5353.sin_family      = AF_INET;
62     s5353.sin_port        = MulticastDNSPort.NotAnInteger;
63     s5353.sin_addr.s_addr = 0;
64--
652.41.0
66
67