Commit 5c7e5002 authored by Abhishek Pandey's avatar Abhishek Pandey Committed by Ashok Babu Channa

Allow empty array in OCRepPayloadSetIntArray

[IOT-2458] OCRepPayloadSetIntArray() did not allow empty
array.  Fixed by not returning false when dimTotal is 0
and instead letting the function to get executed when input
array is NULL & dimTotal is 0.

Also fixed the usage of API in easy setup enrollee code.
Now using OCRepPayloadSetIntArrayAsOwner() instead of
OCRepPayloadSetIntArray() as the later duplicates the
memory which can be avoided "by giving payload ownership
to stack".

Added unit test in cbortests to test new API behavior.

Change-Id: If5352b1faf5a9355dd0f8a22882a6af545df6d29
Signed-off-by: default avatarAbhishek Pandey <abhi.siso@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21591Tested-by: default avatarjenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: default avatarUze Choi <uzchoi@samsung.com>
Reviewed-by: default avatarTodd Malsbary <todd.malsbary@intel.com>
Reviewed-by: default avatarHarish Marappa <h.marappa@samsung.com>
Reviewed-by: default avatarAshok Babu Channa <ashok.channa@samsung.com>
parent 0d511009
......@@ -1039,21 +1039,14 @@ bool OC_CALL OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
size_t dimTotal = calcDimTotal(dimensions);
if (dimTotal == 0)
{
return false;
}
int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
if (!newArray)
if (newArray && array)
{
return false;
memcpy(newArray, array, dimTotal * sizeof(int64_t));
}
memcpy(newArray, array, dimTotal * sizeof(int64_t));
bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
if (!b)
{
......
......@@ -401,3 +401,35 @@ TEST(CborHeterogeneousArrayTest, ConvertParseTest)
OICFree(payload_cbor);
OCPayloadDestroy(payload_out);
}
TEST(CborEmptyArrayTest, EmptyArrayArraySetGetTest)
{
OCRepPayload* payload_in = OCRepPayloadCreate();
ASSERT_TRUE(payload_in != NULL);
OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
OCRepPayloadSetPropInt(payload_in, "scale", 4);
size_t dimensions_in[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
EXPECT_EQ(true, OCRepPayloadSetIntArray(payload_in, "quakedata",
NULL, dimensions_in));
int64_t* quakedata_out = NULL;
size_t dimensions_out[MAX_REP_ARRAY_DEPTH] = {0};
///TODO: Change Assert to true after fixing OCRepPayloadGetIntArray behaviour
ASSERT_EQ(false, OCRepPayloadGetIntArray(payload_in, "quakedata",
&quakedata_out, dimensions_out));
ASSERT_TRUE(quakedata_out == NULL);
for(size_t i = 0; i < MAX_REP_ARRAY_DEPTH; i++)
{
EXPECT_EQ(0, dimensions_out[i]);
}
// Cleanup
OCRepPayloadDestroy(payload_in);
}
......@@ -998,7 +998,7 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
(ehRequest->query && !strcmp(ehRequest->query, "")) ||
(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
{
OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Default interface)");
OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
......@@ -1023,13 +1023,19 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
{
connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
}
OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
bool b = OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
if (!b)
{
OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
OICFree(connectRequest);
}
}
else
{
OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
}
if (gWriteUserdataCb)
......@@ -1047,7 +1053,7 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
{
OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Batch Interface)");
OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
OCRepPayload* repPayload = NULL;
......@@ -1102,13 +1108,19 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
{
connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
}
OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
bool b = OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
if (!b)
{
OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
OICFree(connectRequest);
}
}
else
{
OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
}
if (gWriteUserdataCb)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment