MISRA Discussion Forums
Rule 4-5-3 about using relational operators to determine uint8_t - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.4 Standard conversions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=133)
+---- Thread: Rule 4-5-3 about using relational operators to determine uint8_t (/showthread.php?tid=1639)



Rule 4-5-3 about using relational operators to determine uint8_t - zhaohui - 08-12-2022

Quote:Exceptionally, the following operators may be used if the associated restriction is observed:
• The binary + operator may be used to add an integral value in the range 0 to 9 to ‘0’;
• The binary – operator may be used to subtract character '0'.
• The relational operators <, <=, >, >= may be used to determine if a character (or wide
character) represents a digit.
According to exceptions and cases of Rule 4-5-3, it seems that Exp1 apples to uint8_t and Exp2, Exp3 apply to character (wide character). And the rule title mainly cares about plain char and wchar_t, so, 
1. What about using uint8_t as operands of relational operators?
2. Which kind of cases does uint8_t violate this rule? Does this rule only check binary operator "+" only for uint8_t?
Code:
void f(void)
{
    char ch = 't';
    if (( ch >= '0') && ( ch <= '9')) // Compliant by exception
    {
        v = ch – '0';                 // Compliant by exception
    }

    unsigned char uc;
    if (( uc >= '0') && ( uc <= '9'))  // compliant or non-compliant?
    {
    }
}



RE: Rule 4-5-3 about using relational operators to determine uint8_t - misra cpp - 06-11-2023

The intent of the rule is to enforce the interpretation that plain chars are used for characters, whereas signed/unsigned chars are meant to represent numeric values.
   
The exceptions exist because, at some point there is likely to be a requirement to input a string and interpret it as an integer (hence the allowed comparison with characters '0'..'9' and subtraction of '0'). Similarly, during output, converting an integer between 0..9 to a char requires adding '0'.
   
This rule does not apply to uint8_t, as there is no ambiguity in its range - unlike plain char which may be 0..255 or -128..127


RE: Rule 4-5-3 about using relational operators to determine uint8_t - chgros - 05-11-2025

Thanks for this, but we'd like an answer to the question: is the code compliant or not? I believe it's not compliant, because 
Code:
uc <= '9'
compares a character expression ('9') to something that's not a character, and that's not among the exceptions. Our customer claims it's compliant because of the exception to check for a digit.


RE: Rule 4-5-3 about using relational operators to determine uint8_t - misra cpp - 14-11-2025

We agree with your interpretation, its non-compliant.

The exception in 4.5.3 only allows the use of plain char "to determine if a character (or wide character) represents a digit".

Rule 5-0-11: the plain char type shall only be used for the storage and use of character values
Rule 5-0-12: signed char and unsigned char type shall only be used for the storage and use of numeric values

If neither of these rules has been deviated, 'uc' should not represent a character and so the exception doesn't apply and the code is non-compliant