Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 1 | From ed58146d3aeecdb9920fdc017f85c18b5b10f2db Mon Sep 17 00:00:00 2001 |
| 2 | From: Nate Karstens <nate.karstens@garmin.com> |
| 3 | Date: Thu, 10 Aug 2017 08:27:32 -0500 |
| 4 | Subject: [PATCH 8/8] Handle errors from socket calls |
| 5 | |
| 6 | Adds handling for socket() or read() returning a |
| 7 | negative value (indicating an error has occurred). |
| 8 | |
| 9 | Upstream-Status: Submitted [dts@apple.com] |
| 10 | |
| 11 | Signed-off-by: Nate Karstens <nate.karstens@garmin.com> |
| 12 | Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com> |
| 13 | --- |
| 14 | mDNSPosix/mDNSPosix.c | 12 +++++++++--- |
| 15 | 1 file changed, 9 insertions(+), 3 deletions(-) |
| 16 | |
| 17 | diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c |
| 18 | index 77d982f4c50b..cab512160afb 100644 |
| 19 | --- a/mDNSPosix/mDNSPosix.c |
| 20 | +++ b/mDNSPosix/mDNSPosix.c |
| 21 | @@ -1677,7 +1677,7 @@ mDNSlocal void ProcessRoutingNotification(int sd, GenLinkedList *change |
| 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 ProcessRoutingNotification(int sd, GenLinkedList *change |
| 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 ProcessRoutingNotification(int sd, GenLinkedList *change |
| 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_CanReceiveUnicast(void) |
| 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 | -- |
| 62 | 2.35.1 |
| 63 | |