ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Misra-C rule 10.6
    Programming/C&C++ 2014. 9. 15. 17:07

     얼마전 회사에서 담당했던 소스코드 정적검증 도중 검출된 문제와 관련하여 일주일 가량을 어떻게 리포팅 해야할까 고민하다 Misra-C rule 10.5 에서 이유를 찾았다.


     Rule 10.5의 내용은 다음과 같다.


    10.5 (required): If the bitwise operators ~ and << are applied to an operand of underlying type 'unsigned char' or 'unsigned short', the result shall be immediately cast to the underlying type of the operand.


     상세 내용을 보면 결론은 이거다.

    bitwise 연산자가 small integer types(unsigned char or unsigned short)에 사용될 경우 integral promotion이 발생된다.


    e.g)

    unsigned char port = 0x5aU;

    unsigned char result;


    result = (~port) >> 4;    /* not compliant */

    result = ((unsigned char)(~port)) >> 4;    /* compliant */


     사실 bitwise 연산자에도 integral promotion이 적용된다는 것은 모르고 있었다.


     C99에도 언급이 되어 있었던 부분인데 사실 이 부분은 사용하는 컴파일러마다 underlying type을 어떻게 처리할지는 다르기 때문에 유심히 살펴볼 필요가 있을 것 같다.

    The integer promotions are applied only: as part of the usual arithmetic conversions, to certain

    argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.


    'Programming > C&C++' 카테고리의 다른 글

    문자열 처리  (0) 2007.01.21
    xmalloc  (0) 2006.10.31
    [C]scanf의 오류  (1) 2006.04.27
    [퍼옴]전처리기  (0) 2006.03.09
    [C]hashing  (0) 2006.01.18
Designed by Tistory.