I performing the code analysis of my embedded C code with SonarQube and RATS (Rough Auditing Tool for Security).
Under Ubuntu Shell, I execute
rats --quiet --xml -w 1 . > ./rats_report.xml
to get the the report that will be imported into SonarQube.
I get some errors like this:
Extra care should be taken to ensure that character arrays that are allocated on the stack are used safely. They are prime targets for buffer overflow attacks.
This is a snippet code of the function that generates the error:
static char* GetQueryStringForValue( const char* valueLabel )
{
static char queryString[QUERY_LEN + 1];
memcpy( queryString, '\0', sizeof(queryString) );
snprintf( queryString, sizeof(queryString), "{'%s'", valueLabel );
return queryString;
}
I understand that the problem is related to the buffer allocated into the stack.
My question is: which is the best practice to prevent buffer overflow attacks?
Should I add particular controls?
Thanks for the help!
BR, Federico