Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-9939

Compiler issues MISRA diagnostic 10.1 for assignment of UINT8_MAX to uint8_t

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Implement
    • Icon: Low Low
    • Code Generation Tools
    • CODEGEN-7925
    • ARM_20.2.2.LTS
    • default
    • No plans to address at this time.
    • MISRA 2004 checking has been deprecated. No further MISRA issues will be addressed.

      The user's test case assigns UINT8_MAX to an object of type uint8_t:

      uint8_t tmp = UINT8_MAX; /* gets MISRA diagnostic */
      tmp = 0xFFu; /* no diagnostic */

      The compiler emits a MISRA-C:2004 diagnostic on the first line, but not the second:

      % armcl --check_misra --strict_ansi --verbose_diagnostics try1.c
      "try1.c", line 9: warning: (MISRA-C:2004 10.1/R) The value of an expression of
      integer type shall not be implicitly converted to a different
      underlying type if it is not a conversion to a wider integer type of
      the same signedness
      uint8_t tmp = UINT8_MAX;

      This diagnostic is correct according to the rules for MISRA-C:2004.

      Somewhat confusingly, the type of UINT8_MAX is not uint8_t.

      C99 7.18. "Limits of specified-width integer types" says: "this expression shall have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions." Therefore this macro has a value of type "signed int," and one gets a MISRA-2004 warning for trying to assign it directly to a variable of type uint8_t.

      On the other hand, MISRA-2004 has a special case for constants. MISRA-2004 specifically allows the second case "tmp = 0xffu", because it is a literal constant with a value that fits into x without changing the value. MISRA-2004 does not make an exception for macros that have a value that fits, nor does it recognize UINT8_MAX as a special case, as it is a C99 feature.

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: