Skip to content

Commit

Permalink
Merge pull request eclipse-wakaama#392 from eclipse/fix_378
Browse files Browse the repository at this point in the history
Fix JSON decoding of resource instances.
  • Loading branch information
dnav committed Oct 10, 2018
2 parents 25ea074 + 852e313 commit eed11b5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 29 deletions.
43 changes: 23 additions & 20 deletions core/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ static int prv_parseItem(uint8_t * buffer,
if (recordP->ids[0] != LWM2M_MAX_ID) return -1;

// Check for " around URI
if (valueLen < 3
if (valueLen < 2
|| buffer[index+valueStart] != '"'
|| buffer[index+valueStart+valueLen-1] != '"')
{
Expand All @@ -339,27 +339,30 @@ static int prv_parseItem(uint8_t * buffer,
}
i = 0;
j = 0;
do {
uint32_t readId;
if (valueLen > 1)
{
do {
uint32_t readId;

readId = 0;
i++;
while (i < valueLen-1 && buffer[index+valueStart+i] != '/')
{
if (buffer[index+valueStart+i] < '0'
|| buffer[index+valueStart+i] > '9')
readId = 0;
i++;
while (i < valueLen-1 && buffer[index+valueStart+i] != '/')
{
return -1;
if (buffer[index+valueStart+i] < '0'
|| buffer[index+valueStart+i] > '9')
{
return -1;
}
readId *= 10;
readId += buffer[index+valueStart+i] - '0';
if (readId > LWM2M_MAX_ID) return -1;
i++;
}
readId *= 10;
readId += buffer[index+valueStart+i] - '0';
if (readId > LWM2M_MAX_ID) return -1;
i++;
}
recordP->ids[j] = readId;
j++;
} while (i < valueLen-1 && j < 4 && buffer[index+valueStart+i] == '/');
if (i < valueLen-1 ) return -1;
recordP->ids[j] = readId;
j++;
} while (i < valueLen-1 && j < 4 && buffer[index+valueStart+i] == '/');
if (i < valueLen-1 ) return -1;
}
}
break;

Expand Down Expand Up @@ -598,7 +601,7 @@ static int prv_convertRecord(lwm2m_uri_t * uriP,
parentP->value.asChildren.array = lwm2m_data_new(1);
if (NULL == parentP->value.asChildren.array) goto error;
parentP = parentP->value.asChildren.array;
parentP->type = LWM2M_TYPE_UNDEFINED;
parentP->type = LWM2M_TYPE_MULTIPLE_RESOURCE;
parentP->id = uriP->resourceId;
rootLevel = URI_DEPTH_RESOURCE_INSTANCE;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/client)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lightclient)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/server)

project(wakaama)
project(wakaama C)
4 changes: 2 additions & 2 deletions examples/bootstrap_server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.0)

project (bootstrap_server)
project (bootstrap_server C)

if(DTLS)
message(FATAL_ERROR "DTLS option is not supported." )
Expand Down Expand Up @@ -28,4 +28,4 @@ add_executable(${PROJECT_NAME} ${SOURCES} ${AUXILIARY_FILES} ${WAKAAMA_SOURCES}
# Add WITH_LOGS to debug variant
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:WITH_LOGS>)

SOURCE_GROUP(wakaama FILES ${WAKAAMA_SOURCES})
SOURCE_GROUP(wakaama FILES ${WAKAAMA_SOURCES})
4 changes: 2 additions & 2 deletions examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.0)

project (lwm2mclient)
project (lwm2mclient C)

option(DTLS "Enable DTLS" OFF)

Expand Down Expand Up @@ -32,4 +32,4 @@ add_executable(${PROJECT_NAME} ${SOURCES} ${WAKAAMA_SOURCES} ${SHARED_SOURCES})
# Add WITH_LOGS to debug variant
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:WITH_LOGS>)

SOURCE_GROUP(wakaama FILES ${WAKAAMA_SOURCES})
SOURCE_GROUP(wakaama FILES ${WAKAAMA_SOURCES})
4 changes: 2 additions & 2 deletions examples/lightclient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.0)

project (lightclient)
project (lightclient C)

if(DTLS)
message(FATAL_ERROR "DTLS option is not supported." )
Expand All @@ -27,4 +27,4 @@ add_executable(${PROJECT_NAME} ${SOURCES} ${WAKAAMA_SOURCES} ${SHARED_SOURCES})
# Add WITH_LOGS to debug variant
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:WITH_LOGS>)

SOURCE_GROUP(wakaama FILES ${WAKAAMA_SOURCES})
SOURCE_GROUP(wakaama FILES ${WAKAAMA_SOURCES})
2 changes: 1 addition & 1 deletion examples/server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 2.8)

project (lwm2mserver)
project (lwm2mserver C)

if(DTLS)
message(FATAL_ERROR "DTLS option is not supported." )
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.0)

project (lwm2munittests)
project (lwm2munittests C)

include(${CMAKE_CURRENT_LIST_DIR}/../core/wakaama.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../examples/shared/shared.cmake)
Expand Down
34 changes: 34 additions & 0 deletions tests/tlv_json_lwm2m_data_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ static void test_9(void)

test_raw(NULL, (uint8_t *)buffer, strlen(buffer), LWM2M_CONTENT_JSON, "9");
}

static void test_10(void)
{
lwm2m_data_t * data1 = lwm2m_data_new(17);
Expand Down Expand Up @@ -384,6 +385,36 @@ static void test_10(void)
test_data("/12/0", LWM2M_CONTENT_JSON, data1, 17, "10b");
}

static void test_11(void)
{
const char * buffer = "{\"bn\":\"/5/0/1\", \
\"e\":[ \
{\"n\":\"/1\",\"sv\":\"http\"}] \
}";

test_raw(NULL, (uint8_t *)buffer, strlen(buffer), LWM2M_CONTENT_JSON, "11");
}

static void test_12(void)
{
const char * buffer = "{\"bn\":\"/5/0\", \
\"e\":[ \
{\"n\":\"/1\",\"sv\":\"http\"}] \
}";

test_raw(NULL, (uint8_t *)buffer, strlen(buffer), LWM2M_CONTENT_JSON, "12");
}

static void test_13(void)
{
const char * buffer = "{\"bn\":\"/5/0/1\", \
\"e\":[ \
{\"n\":\"\",\"sv\":\"http\"}] \
}";

test_raw(NULL, (uint8_t *)buffer, strlen(buffer), LWM2M_CONTENT_JSON, "13");
}

static struct TestTable table[] = {
{ "test of test_1()", test_1 },
{ "test of test_2()", test_2 },
Expand All @@ -395,6 +426,9 @@ static struct TestTable table[] = {
{ "test of test_8()", test_8 },
{ "test of test_9()", test_9 },
{ "test of test_10()", test_10 },
{ "test of test_11()", test_11 },
{ "test of test_12()", test_12 },
{ "test of test_13()", test_13 },
{ NULL, NULL },
};

Expand Down

0 comments on commit eed11b5

Please sign in to comment.