0008-Handle-errors-from-socket-calls.patch 2.5 KB

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