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