From 6d894b634ed772525e9c43bf3a955c3b89ada9e7 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Tue, 4 Sep 2018 13:21:02 +0000 Subject: [PATCH] NULL pointer check for memcpy stub structure default initialization for pointer check --- src/stubser/stubser.c | 52 +++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index 9f60c79..6935636 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -183,10 +183,10 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf aFunction->name, aParameterIndex); fprintf(aFile, ", strlen("STUBINIT_PARAM_PARAMETER_S1"));"NEWLINES, aParameterIndex); fprintf(aFile, "\t}" NEWLINES); - // injection only for non constant parameter + // injection only for non constant and non NULL parameter if (NULL == strstr(aParameter->dataType, "const")) { - fprintf(aFile, "\telse" NEWLINES "\t{" NEWLINES); + fprintf(aFile, "\telse if (NULL != " STUBINIT_PARAM_PARAMETER_S1 ")" NEWLINES "\t{" NEWLINES, aParameterIndex); fprintf(aFile, "\t\tmemcpy(" STUBINIT_PARAM_PARAMETER_S1 ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1", " // STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size",// aParameterIndex, aFunction->name, aFunction->name, aParameterIndex, aFunction->name, aFunction->name, aParameterIndex); @@ -208,10 +208,10 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf aFunction->name, aParameterIndex); fprintf(aFile, ");" NEWLINES); fprintf(aFile, "\t}" NEWLINES); - // injection only for non constant parameter + // injection only for non constant and non NULL parameter if (NULL == strstr(aParameter->dataType, "const")) { - fprintf(aFile, "\telse" NEWLINES "\t{" NEWLINES); + fprintf(aFile, "\telse if (NULL != " STUBINIT_PARAM_PARAMETER_S1 ")" NEWLINES "\t{" NEWLINES, aParameterIndex); fprintf(aFile, "\t\tmemcpy(" STUBINIT_PARAM_PARAMETER_S1 ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1", " // STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size",// aParameterIndex, aFunction->name, aFunction->name, aParameterIndex, aFunction->name, aFunction->name, aParameterIndex); @@ -420,24 +420,25 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a { if (CPARAMETER_TYPE_FUNCPTR == parameter->type) { - fprintf(aFile, "\t%s (*"STUBPARAMETER_PARAM_S1")%s; /*!< @brief %s */" NEWLINES, NCHECK(parameter->dataType), parameterIndex++, + fprintf(aFile, "\t%s (*"STUBPARAMETER_PARAM_S1")%s; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex++, NCHECK(parameter->array), NCHECK(parameter->name)); } else if (parameter->array) { - fprintf(aFile, "\t%s *"STUBPARAMETER_PARAM_S1"; /*!< @brief %s */" NEWLINES, NCHECK(parameter->dataType), parameterIndex, NCHECK(parameter->name)); - fprintf(aFile, "\tuint32_t "STUBPARAMETER_PARAM_S1"_size; /*!< @brief %s size to copy in case of parameter injection */" NEWLINES, parameterIndex++, - NCHECK(parameter->name)); + fprintf(aFile, "\t%s *"STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex, NCHECK(parameter->name)); + fprintf(aFile, "\tuint32_t "STUBPARAMETER_PARAM_S1"_size; /*!< @brief \"%s\" size to copy in case of parameter injection */" NEWLINES, + parameterIndex++, NCHECK(parameter->name)); } else if (NULL != strstr(parameter->dataType, "*")) { - fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief %s */" NEWLINES, NCHECK(parameter->dataType), parameterIndex, NCHECK(parameter->name)); - fprintf(aFile, "\tuint32_t "STUBPARAMETER_PARAM_S1"_size; /*!< @brief %s size to copy in case of parameter injection */" NEWLINES, parameterIndex++, - NCHECK(parameter->name)); + fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex, NCHECK(parameter->name)); + fprintf(aFile, "\tuint32_t "STUBPARAMETER_PARAM_S1"_size; /*!< @brief \"%s\" size to copy in case of parameter injection */" NEWLINES, + parameterIndex++, NCHECK(parameter->name)); } else if (CPARAMETER_TYPE_REGULAR == parameter->type) { - fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief %s */" NEWLINES, NCHECK(parameter->dataType), parameterIndex++, NCHECK(parameter->name)); + fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex++, + NCHECK(parameter->name)); } parameter = parameter->next; } @@ -452,8 +453,31 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a // expected parameter fprintf(aFile, "// Expected parameter and return value arrays for in-stub tests" NEWLINES); - fprintf(aFile, STUBPARAMETER_TYPEDEF_S1 " " STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_AMOUNT_S1"];" NEWLINES, aFunction->name, aFunction->name, - aFunction->name); + fprintf(aFile, STUBPARAMETER_TYPEDEF_S1 " " STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_AMOUNT_S1"]", aFunction->name, aFunction->name, aFunction->name); + if (pointerAvailable) + { + fprintf(aFile, " = {" NEWLINES); + // pointer address check by default + + parameter = aFunction->parameter.head; + parameterIndex = 'a'; + while (parameter) + { + // initialize _size parameter to 1 (default to check addresses) + if (CPARAMETER_TYPE_FUNCPTR != parameter->type && (parameter->array || NULL != strstr(parameter->dataType, "*"))) + { + fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1" = (%s) 0x55," NEWLINES, STUB_INSTANCES_AMOUNT - 1, parameterIndex, parameter->dataType); + fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1"_size = 1," NEWLINES, STUB_INSTANCES_AMOUNT - 1, parameterIndex); + } + ++parameterIndex; + parameter = parameter->next; + } + fprintf(aFile, "};" NEWLINES); + } + else + { + fprintf(aFile, ";" NEWLINES); + } NEWLINE(aFile); // stub function init header; function comment