1From ed58146d3aeecdb9920fdc017f85c18b5b10f2db 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 8/8] 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>
12Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
13---
14 mDNSPosix/mDNSPosix.c | 12 +++++++++---
15 1 file changed, 9 insertions(+), 3 deletions(-)
16
17Index: mDNSResponder/mDNSPosix/mDNSPosix.c
18===================================================================
19--- mDNSResponder.orig/mDNSPosix/mDNSPosix.c
20+++ mDNSResponder/mDNSPosix/mDNSPosix.c
21@@ -1677,7 +1677,7 @@ mDNSlocal void          ProcessRoutingNo
22 // Read through the messages on sd and if any indicate that any interface records should
23 // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0.
24 {
25-    ssize_t readCount;
26+    ssize_t readVal, readCount;
27     char buff[4096];
28     struct nlmsghdr         *pNLMsg = (struct nlmsghdr*) buff;
29
30@@ -1686,7 +1686,10 @@ mDNSlocal void          ProcessRoutingNo
31     // enough to hold all pending data and so avoid message fragmentation.
32     // (Note that FIONREAD is not supported on AF_NETLINK.)
33
34-    readCount = read(sd, buff, sizeof buff);
35+    readVal = read(sd, buff, sizeof buff);
36+    if (readVal < 0) return;
37+    readCount = readVal;
38+
39     while (1)
40     {
41         // Make sure we've got an entire nlmsghdr in the buffer, and payload, too.
42@@ -1702,7 +1705,9 @@ mDNSlocal void          ProcessRoutingNo
43                 pNLMsg = (struct nlmsghdr*) buff;
44
45                 // read more data
46-                readCount += read(sd, buff + readCount, sizeof buff - readCount);
47+                readVal = read(sd, buff + readCount, sizeof buff - readCount);
48+                if (readVal < 0) return;
49+                readCount += readVal;
50                 continue;                   // spin around and revalidate with new readCount
51             }
52             else
53@@ -2017,6 +2022,7 @@ mDNSlocal mDNSBool mDNSPlatformInit_CanR
54     int err;
55     int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
56     struct sockaddr_in s5353;
57+    if (s < 0) return mDNSfalse;
58     s5353.sin_family      = AF_INET;
59     s5353.sin_port        = MulticastDNSPort.NotAnInteger;
60     s5353.sin_addr.s_addr = 0;
61