A modelling and serializer library for Microsoft Azure IoTHub client applications
Dependents: sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more
This library implements a serializer library to be used in projects involving Microsoft Azure IoT Hub connectivity. The code is replicated from https://github.com/Azure/azure-iot-sdks
Diff: commanddecoder.c
- Revision:
- 36:7d12a5386197
- Parent:
- 26:7c0e6f86d034
--- a/commanddecoder.c Thu Jul 12 18:10:30 2018 -0700 +++ b/commanddecoder.c Tue Sep 11 11:14:37 2018 -0700 @@ -103,7 +103,7 @@ else { memberNames[j] = propertyName; - + /* Codes_SRS_COMMAND_DECODER_01_014: [CommandDecoder shall use the MultiTree APIs to extract a specific element from the command JSON.] */ if (MultiTree_GetChildByName(node, memberNames[j], &memberNode) != MULTITREE_OK) { @@ -181,6 +181,12 @@ LogError("Invalid action name"); result = EXECUTE_COMMAND_ERROR; } + else if (strLength >= 128) + { + /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */ + LogError("Invalid action name length"); + result = EXECUTE_COMMAND_ERROR; + } else { /* Codes_SRS_COMMAND_DECODER_99_006:[ The action name shall be decoded from the element "Name" of the command JSON.] */ @@ -188,10 +194,7 @@ size_t argCount; MULTITREE_HANDLE parametersTreeNode; -#ifdef _MSC_VER -#pragma warning(suppress: 6324) /* We intentionally use here strncpy */ -#endif - if (strncpy(tempStr, actionName, strLength - 1) == NULL) + if (memcpy(tempStr, actionName, strLength-1) == NULL) { /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */ LogError("Invalid action name."); @@ -206,8 +209,7 @@ } else { - tempStr[strLength - 1] = 0; - + tempStr[strLength-1] = 0; /* Codes_SRS_COMMAND_DECODER_99_009:[ CommandDecoder shall call Schema_GetModelActionByName to obtain the information about a specific action.] */ if (((modelActionHandle = Schema_GetModelActionByName(modelHandle, tempStr)) == NULL) || (Schema_GetModelActionArgumentCount(modelActionHandle, &argCount) != SCHEMA_OK)) @@ -309,9 +311,9 @@ size_t argCount; #ifdef _MSC_VER -#pragma warning(suppress: 6324) /* We intentionally use here strncpy */ +#pragma warning(suppress: 6324) /* We intentionally use here strncpy */ #endif - + /*Codes_SRS_COMMAND_DECODER_02_020: [ CommandDecoder_ExecuteMethod shall verify that the model has a method called methodName. ]*/ if (((modelMethodHandle = Schema_GetModelMethodByName(modelHandle, methodName)) == NULL) || (Schema_GetModelMethodArgumentCount(modelMethodHandle, &argCount) != SCHEMA_OK)) @@ -323,7 +325,7 @@ else { /*Codes_SRS_COMMAND_DECODER_02_021: [ For every argument of methodName, CommandDecoder_ExecuteMethod shall build an AGENT_DATA_TYPE from the node with the same name from the MULTITREE_HANDLE. ]*/ - + if (argCount == 0) { /*no need for any parameters*/ @@ -393,7 +395,7 @@ } } - + } return result; } @@ -624,7 +626,7 @@ else { result = ScanMethodPathAndExecuteMethod(commandDecoderInstance, schemaHandle, fullMethodName, methodTree); - + } return result; } @@ -637,7 +639,7 @@ /*Codes_SRS_COMMAND_DECODER_01_010: [If either the buffer or the receiveCallbackContext argument is NULL, the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/ if ( (command == NULL) || - (commandDecoderInstance == NULL) + (commandDecoderInstance == NULL) ) { LogError("Invalid argument, COMMAND_DECODER_HANDLE handle=%p, const char* command=%p", handle, command); @@ -800,7 +802,7 @@ /*if the serialization does not contain mandatory items from the model, it fails*/ static bool validateModel_vs_Multitree(void* startAddress, SCHEMA_MODEL_TYPE_HANDLE modelHandle, MULTITREE_HANDLE desiredPropertiesTree, size_t offset) { - + bool result; size_t nChildren; size_t nProcessedChildren = 0; @@ -842,13 +844,13 @@ } case (SCHEMA_PROPERTY): { - LogError("cannot ingest name (WITH_DATA instead of WITH_DESIRED_PROPERTY): %s", STRING_c_str); + LogError("cannot ingest name (WITH_DATA instead of WITH_DESIRED_PROPERTY): %s", childName_str); i = nChildren; break; } case (SCHEMA_REPORTED_PROPERTY): { - LogError("cannot ingest name (WITH_REPORTED_PROPERTY instead of WITH_DESIRED_PROPERTY): %s", STRING_c_str); + LogError("cannot ingest name (WITH_REPORTED_PROPERTY instead of WITH_DESIRED_PROPERTY): %s", childName_str); i = nChildren; break; } @@ -856,7 +858,7 @@ { /*Codes_SRS_COMMAND_DECODER_02_007: [ If the child name corresponds to a desired property then an AGENT_DATA_TYPE shall be constructed from the MULTITREE node. ]*/ SCHEMA_DESIRED_PROPERTY_HANDLE desiredPropertyHandle = elementType.elementHandle.desiredPropertyHandle; - + const char* desiredPropertyType = Schema_GetModelDesiredPropertyType(desiredPropertyHandle); AGENT_DATA_TYPE output; if (DecodeValueFromNode(Schema_GetSchemaForModelType(modelHandle), &output, child, desiredPropertyType) != 0) @@ -884,13 +886,13 @@ } Destroy_AGENT_DATA_TYPE(&output); } - + break; } case(SCHEMA_MODEL_IN_MODEL): { SCHEMA_MODEL_TYPE_HANDLE modelModel = elementType.elementHandle.modelHandle; - + /*Codes_SRS_COMMAND_DECODER_02_009: [ If the child name corresponds to a model in model then the function shall call itself recursively. ]*/ if (!validateModel_vs_Multitree(startAddress, modelModel, child, offset + Schema_GetModelModelByName_Offset(modelHandle, childName_str))) { @@ -906,10 +908,10 @@ { onDesiredProperty((char*)startAddress + offset); } - + nProcessedChildren++; } - + break; } @@ -980,7 +982,7 @@ EXECUTE_COMMAND_RESULT CommandDecoder_IngestDesiredProperties(void* startAddress, COMMAND_DECODER_HANDLE handle, const char* jsonPayload, bool parseDesiredNode) { EXECUTE_COMMAND_RESULT result; - + /*Codes_SRS_COMMAND_DECODER_02_001: [ If startAddress is NULL then CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_ERROR. ]*/ /*Codes_SRS_COMMAND_DECODER_02_002: [ If handle is NULL then CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_ERROR. ]*/ /*Codes_SRS_COMMAND_DECODER_02_003: [ If jsonPayload is NULL then CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_ERROR. ]*/