File fix-mmap-return-value-check.patch of Package jack

From cc8576a7ab62caab10c7ddd7309367cf797c5e69 Mon Sep 17 00:00:00 2001
From: falkTX <falktx@gmail.com>
Date: Thu, 5 Apr 2018 10:04:18 +0200
Subject: [PATCH] Check mmap() against MAP_FAILED Fixes #338

---
 linux/JackLinuxFutex.cpp | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/linux/JackLinuxFutex.cpp b/linux/JackLinuxFutex.cpp
index b724f191..415a8b95 100644
--- a/linux/JackLinuxFutex.cpp
+++ b/linux/JackLinuxFutex.cpp
@@ -147,7 +147,9 @@ bool JackLinuxFutex::Allocate(const char* name, const char* server_name, int val
         return false;
     }
 
-    if ((fFutex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0)) == NULL) {
+    FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0);
+
+    if (futex == NULL || futex == MAP_FAILED) {
         jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
         close(fSharedMem);
         fSharedMem = -1;
@@ -157,11 +159,12 @@ bool JackLinuxFutex::Allocate(const char* name, const char* server_name, int val
 
     fPrivate = internal;
 
-    fFutex->futex = value;
-    fFutex->internal = internal;
-    fFutex->wasInternal = internal;
-    fFutex->needsChange = false;
-    fFutex->externalCount = 0;
+    futex->futex = value;
+    futex->internal = internal;
+    futex->wasInternal = internal;
+    futex->needsChange = false;
+    futex->externalCount = 0;
+    fFutex = futex;
     return true;
 }
 
@@ -182,24 +185,27 @@ bool JackLinuxFutex::Connect(const char* name, const char* server_name)
         return false;
     }
 
-    if ((fFutex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0)) == NULL) {
+    FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0);
+
+    if (futex == NULL || futex == MAP_FAILED) {
         jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
         close(fSharedMem);
         fSharedMem = -1;
         return false;
     }
 
-    if (! fPrivate && fFutex->wasInternal)
+    if (! fPrivate && futex->wasInternal)
     {
         const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
 
-        if (externalSync != NULL && strstr(fName, externalSync) != NULL && ++fFutex->externalCount == 1)
+        if (externalSync != NULL && strstr(fName, externalSync) != NULL && ++futex->externalCount == 1)
         {
             jack_error("Note: client %s running as external client temporarily", fName);
-            fFutex->needsChange = true;
+            futex->needsChange = true;
         }
     }
 
+    fFutex = futex;
     return true;
 }