From 52d944196e2c809db5eded2ae3ad0396a2b08c2d Mon Sep 17 00:00:00 2001
From: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
Date: Wed, 2 Aug 2017 15:32:33 -0400
Subject: [PATCH] contrib: add fix for restbed crash on Android

Restbed crashes on Android with the following backtrace:
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)
  native: pc 000000000001e598  /system/lib/libc.so (strlen+72)
  native: pc 000000000002728d  /system/lib/libc.so (strdup+4)
  native: pc 00000000006d4a07  /data/app-lib/cx.ring-1/libring.so (restbed::detail::HttpImpl::to_bytes(std::__ndk1::shared_ptr<restbed::Request> const&)+1578)

This patch attempts to fix the issue.

Change-Id: I9bd05c47ae2be4e991364f9fa3192c7b17858d1a
---
 contrib/src/restbed/locale-fix.patch | 79 ++++++++++++++++++++++++++++
 contrib/src/restbed/rules.mak        |  1 +
 2 files changed, 80 insertions(+)
 create mode 100644 contrib/src/restbed/locale-fix.patch

diff --git a/contrib/src/restbed/locale-fix.patch b/contrib/src/restbed/locale-fix.patch
new file mode 100644
index 0000000000..3cb30879e0
--- /dev/null
+++ b/contrib/src/restbed/locale-fix.patch
@@ -0,0 +1,79 @@
+From 4c8762f0cd993613c29f051fde19a3f8caeac401 Mon Sep 17 00:00:00 2001
+From: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
+Date: Mon, 31 Jul 2017 09:47:50 -0400
+Subject: [PATCH] http: check for locale before duplicating string
+
+---
+ source/corvusoft/restbed/detail/http_impl.cpp | 16 +++++++++++-----
+ source/corvusoft/restbed/http.cpp             | 16 +++++++++++-----
+ 2 files changed, 22 insertions(+), 10 deletions(-)
+
+diff --git a/source/corvusoft/restbed/detail/http_impl.cpp b/source/corvusoft/restbed/detail/http_impl.cpp
+index 54b7c15..f25d6eb 100644
+--- a/source/corvusoft/restbed/detail/http_impl.cpp
++++ b/source/corvusoft/restbed/detail/http_impl.cpp
+@@ -96,17 +96,23 @@ namespace restbed
+                 protocol = "HTTP";
+             }
+             
+-            char* locale = strdup( setlocale( LC_NUMERIC, nullptr ) );
+-            setlocale( LC_NUMERIC, "C" );
++            char* locale = nullptr;
++            if (auto current_locale = setlocale( LC_NUMERIC, nullptr ) )
++            {
++                locale = strdup(current_locale);
++                setlocale( LC_NUMERIC, "C" );
++            }
+             
+             auto data = String::format( "%s %s %s/%.1f\r\n",
+                                         request->get_method( ).data( ),
+                                         path.data( ),
+                                         protocol.data( ),
+                                         request->get_version( ) );
+-                                        
+-            setlocale( LC_NUMERIC, locale );
+-            free( locale );
++            
++            if (locale) {
++                setlocale( LC_NUMERIC, locale );
++                free( locale );
++            }
+             
+             auto headers = request->get_headers( );
+             
+diff --git a/source/corvusoft/restbed/http.cpp b/source/corvusoft/restbed/http.cpp
+index 7648501..9722f64 100644
+--- a/source/corvusoft/restbed/http.cpp
++++ b/source/corvusoft/restbed/http.cpp
+@@ -62,17 +62,23 @@ namespace restbed
+     
+     Bytes Http::to_bytes( const shared_ptr< Response >& value )
+     {
+-        char* locale = strdup( setlocale( LC_NUMERIC, nullptr ) );
+-        setlocale( LC_NUMERIC, "C" );
++        char* locale = nullptr;
++        if (auto current_locale = setlocale( LC_NUMERIC, nullptr ) )
++        {
++            locale = strdup(current_locale);
++            setlocale( LC_NUMERIC, "C" );
++        }
+         
+         auto data = String::format( "%s/%.1f %i %s\r\n",
+                                     value->get_protocol( ).data( ),
+                                     value->get_version( ),
+                                     value->get_status_code( ),
+                                     value->get_status_message( ).data( ) );
+-                                    
+-        setlocale( LC_NUMERIC, locale );
+-        free( locale );
++        
++        if (locale) {
++            setlocale( LC_NUMERIC, locale );
++            free( locale );
++        }
+         
+         auto headers = value->get_headers( );
+         
+-- 
+2.11.0
+
diff --git a/contrib/src/restbed/rules.mak b/contrib/src/restbed/rules.mak
index c9b2953cf8..a4bf160a0d 100644
--- a/contrib/src/restbed/rules.mak
+++ b/contrib/src/restbed/rules.mak
@@ -47,6 +47,7 @@ restbed: restbed-$(RESTBED_VERSION).tar.gz .sum-restbed
 	rm -r kashmir && mv kashmir-dependency-master kashmir)
 	$(APPLY) $(SRC)/restbed/findkashmir.patch
 	$(APPLY) $(SRC)/restbed/strand.patch
+	$(APPLY) $(SRC)/restbed/locale-fix.patch
 	$(MOVE)
 
 .restbed: restbed toolchain.cmake
-- 
GitLab