c - Both sides have side effects? -


i ran static code analysis misra 2004 , misra 2012 on following c code:

bool_type strings_are_equal(const char *s1, const char *s2) {   bool_type result = true;   const char *str1 = s1;   const char *str2 = s2;    if (null == s1 || null == s2)   {     result = false;   }   else if (strlen(s1) != strlen(s2))   {     result = false;   }   else   {     while (*str1 != 0)     {       if(tolower(*str1++) != tolower(*str2++))       {         result = false;         break;       }     }   }    return result; } 

and got following findings pc-lint reports: enter image description here

can please explain how code @ line 58 , 66 suffering side effects , how should correct it?

calling function invoke side effect, when using formal definition of c standard.

in specific case of strlen(s1) != strlen(s2), there nothing inside functions cause harm. wouldn't make sense implement them example internal static variables. if there such internal variables present, order of evaluation give different results depending on function call executed first. rationale behind warning.

in case of tolower(*str1++) != tolower(*str2++) there both 2 function call side effects , 2 variable assignment side effects ++ operators, total of 4 in single expression. though particular case safe, such code dangerous, depend on order of evaluation, or turn out unsequenced (like i=i++;) severe bug.

solve storing function results in temporary variables. , never mix ++ other operators, because that's both dangerous, pointless , banned misra rule:

misra-c:2004 rule 12.13

the increment (++) , decrement (--) operators should not mixed other operators in expression.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -