From c39ec1e3090bab637234aa8c9ecbfa0427ac814c Mon Sep 17 00:00:00 2001
From: Fawzi Mohamed <fawzi.mohamed@digia.com>
Date: Fri, 4 Oct 2013 01:28:47 +0200
Subject: [PATCH] ios: fix compilation with osx 10.6

Change-Id: Id0b28c0aab46237b60756f12be05e3d05df9c9e7
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@digia.com>
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
---
 src/plugins/ios/iostoolhandler.cpp | 21 +++++++++++++++++----
 src/tools/iostool/main.cpp         | 22 ++++++++++++++++++----
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/src/plugins/ios/iostoolhandler.cpp b/src/plugins/ios/iostoolhandler.cpp
index e91276b6c03..71891b85d16 100644
--- a/src/plugins/ios/iostoolhandler.cpp
+++ b/src/plugins/ios/iostoolhandler.cpp
@@ -39,6 +39,7 @@
 #include <QDebug>
 #include <QCoreApplication>
 #include <QList>
+#include <QScopedArrayPointer>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -404,6 +405,17 @@ void IosToolHandlerPrivate::subprocessFinished(int exitCode, QProcess::ExitStatu
     }
 }
 
+#ifndef CMSG_SPACE
+size_t CMSG_SPACE(size_t len) {
+        msghdr msg;
+        cmsghdr cmsg;
+        msg.msg_control = &cmsg;
+        msg.msg_controllen =  ~socklen_t(0); /* To maximize the chance that CMSG_NXTHDR won't return NULL */
+        cmsg.cmsg_len = CMSG_LEN(len);
+        return reinterpret_cast<unsigned char *>(CMSG_NXTHDR(&msg, &cmsg)) - reinterpret_cast<unsigned char *>(&cmsg);
+}
+#endif
+
 int recv_fd(int socket)
 {
     int sent_fd;
@@ -421,10 +433,11 @@ int recv_fd(int socket)
     socket_message.msg_iovlen = 1;
 
     /* provide space for the ancillary data */
-    char ancillary_element_buffer[CMSG_SPACE(sizeof(int))];
-    memset(ancillary_element_buffer, 0, CMSG_SPACE(sizeof(int)));
-    socket_message.msg_control = ancillary_element_buffer;
-    socket_message.msg_controllen = CMSG_SPACE(sizeof(int));
+    size_t dimAncillaryEl = CMSG_SPACE(sizeof(int));
+    QScopedArrayPointer<char> ancillary_element_buffer(new char[dimAncillaryEl]);
+    memset(ancillary_element_buffer.data(), 0, dimAncillaryEl);
+    socket_message.msg_control = ancillary_element_buffer.data();
+    socket_message.msg_controllen = dimAncillaryEl;
 
     int flags = 0;
 #ifdef MSG_CMSG_CLOEXEC
diff --git a/src/tools/iostool/main.cpp b/src/tools/iostool/main.cpp
index 1cfab9d581b..9030e103f01 100644
--- a/src/tools/iostool/main.cpp
+++ b/src/tools/iostool/main.cpp
@@ -37,6 +37,8 @@
 #include <QXmlStreamWriter>
 #include <QFile>
 #include <QMapIterator>
+#include <QScopedArrayPointer>
+
 #include "iosdevicemanager.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -226,10 +228,22 @@ void IosTool::isTransferringApp(const QString &bundlePath, const QString &device
     outFile.flush();
 }
 
+#ifndef CMSG_SPACE
+size_t CMSG_SPACE(size_t len) {
+        msghdr msg;
+        cmsghdr cmsg;
+        msg.msg_control = &cmsg;
+        msg.msg_controllen =  ~socklen_t(0); /* To maximize the chance that CMSG_NXTHDR won't return NULL */
+        cmsg.cmsg_len = CMSG_LEN(len);
+        return reinterpret_cast<unsigned char *>(CMSG_NXTHDR(&msg, &cmsg)) - reinterpret_cast<unsigned char *>(&cmsg);
+}
+#endif
+
 int send_fd(int socket, int fd_to_send)
 {
     /* storage space needed for an ancillary element with a paylod of length is CMSG_SPACE(sizeof(length)) */
-    char ancillary_element_buffer[CMSG_SPACE(sizeof(int))];
+    size_t dimAncillaryBuffer = CMSG_SPACE(sizeof(int));
+    QScopedArrayPointer<char> ancillary_element_buffer(new char[dimAncillaryBuffer]);
     int available_ancillary_element_buffer_space;
 
     /* at least one vector of one byte must be sent */
@@ -247,9 +261,9 @@ int send_fd(int socket, int fd_to_send)
     socket_message.msg_iovlen = 1;
 
     /* provide space for the ancillary data */
-    available_ancillary_element_buffer_space = CMSG_SPACE(sizeof(int));
-    memset(ancillary_element_buffer, 0, available_ancillary_element_buffer_space);
-    socket_message.msg_control = ancillary_element_buffer;
+    available_ancillary_element_buffer_space = dimAncillaryBuffer;
+    memset(ancillary_element_buffer.data(), 0, available_ancillary_element_buffer_space);
+    socket_message.msg_control = ancillary_element_buffer.data();
     socket_message.msg_controllen = available_ancillary_element_buffer_space;
 
     /* initialize a single ancillary data element for fd passing */
-- 
GitLab