From d8cf30cb0abd5fa8f6282b490618204d683b625c Mon Sep 17 00:00:00 2001
From: Hauke Mehrtens <hauke@hauke-m.de>
Date: Mon, 3 Oct 2016 21:00:28 +0200
Subject: [PATCH 1/3] csdk: move OCEntityHandlerResponse from stack to heap

OCEntityHandlerResponse is over 50KByte and I got a stack overflow on MIPS
running on LEDE without this patch. Instead of storing
OCEntityHandlerResponse on the program stack, allocate some memory on
the heap and free it afterwards again.

This fixes one part of this issue for me:
https://jira.iotivity.org/browse/IOT-1374

Change-Id: I365a5c7a34dce2dfb0897a20b57a13ba566748ec
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 resource/csdk/stack/src/ocresource.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

--- a/resource/csdk/stack/src/ocresource.c
+++ b/resource/csdk/stack/src/ocresource.c
@@ -774,15 +774,25 @@ static bool includeThisResourceInRespons
 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
                                 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
 {
-    OCEntityHandlerResponse response = {0};
+    OCEntityHandlerResponse *response = NULL;
+    OCStackResult result = OC_STACK_ERROR;
 
-    response.ehResult = ehResult;
-    response.payload = discoveryPayload;
-    response.persistentBufferFlag = 0;
-    response.requestHandle = (OCRequestHandle) request->requestId;
-    response.resourceHandle = (OCResourceHandle) resource;
+    response = (OCEntityHandlerResponse *)OICCalloc(1, sizeof(*response));
+    VERIFY_PARAM_NON_NULL(TAG, response, "Failed allocating OCEntityHandlerResponse");
 
-    return OCDoResponse(&response);
+    response->ehResult = ehResult;
+    response->payload = discoveryPayload;
+    response->persistentBufferFlag = 0;
+    response->requestHandle = (OCRequestHandle) request->requestId;
+    response->resourceHandle = (OCResourceHandle) resource;
+
+    result = OCDoResponse(response);
+
+    OICFree(response);
+    return result;
+
+exit:
+    return OC_STACK_NO_MEMORY;
 }
 
 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
