JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


MISC.NEGCHAR : Negative Character Value

Summary

A negative (or potentially negative) value is passed to a function that expects an unsigned char value or EOF (as an int).

Properties

Class Name Negative Character Value
Significance security
Mnemonic MISC.NEGCHAR
Categories
MisraC2025 MisraC2025:21.13 Any value passed to a function in <ctype.h> shall be representable as an unsigned char or be the value EOF
  MisraC2025:22.7 The macro EOF shall only be compared with the unmodified return value from any Standard Library function capable of returning EOF
  MisraC2025:D.4.1 Run-time failures shall be minimized
MisraC2023 MisraC2023:21.13 Any value passed to a function in <ctype.h> shall be representable as an unsigned char or be the value EOF
  MisraC2023:22.7 The macro EOF shall only be compared with the unmodified return value from any Standard Library function capable of returning EOF
  MisraC2023:D.4.1 Run-time failures shall be minimized
Misra2012 Misra2012:21.13 Any value passed to a function in <ctype.h> shall be representable as an unsigned char or be the value EOF
  Misra2012:22.7 The macro EOF shall only be compared with the unmodified return value from any Standard Library function capable of returning EOF
  Misra2012:D.4.1 Run-time failures shall be minimized
AUTOSARC++14 AUTOSARC++14:A21-8-1 Arguments to character-handling functions shall be representable as an unsigned char.
CWE CWE:681 Incorrect Conversion between Numeric Types
  CWE:686 Function Call With Incorrect Argument Type
TS17961 TS17961:5.16-signconv Conversion of signed characters to wider integer types before a check for EOF
  TS17961:5.31-chrsgnext Passing arguments to character-handling functions that are not representable as unsigned char
CERT-C CERT-C:INT05-C Do not use input functions to convert character data if they cannot handle all possible inputs
  CERT-C:STR00-C Represent characters using an appropriate type
  CERT-C:STR34-C Cast characters to unsigned char before converting to larger integer sizes
  CERT-C:STR37-C Arguments to character-handling functions must be representable as an unsigned char
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Negative Character Value"

Example

#include <stdio.h>
#include <ctype.h>

void f(const char *instr, int len){
    int i;
    /* ... obtain instr and len */ 
    for (i=0; i<len; i++){
        if (isupper(instr[i])){    /* Negative Character Value warning issued here */
            printf("string has upper case character");
        }
    }

    /* do this instead */
    for (i=0; i<len; i++){
        if (isupper( (unsigned char) instr[i] )){   
            printf("string has upper case character");
        }
    }
}

Notes

The specifications for the character classification and conversion functions defined in ctype.h require that their argument be an int representable as an unsigned char or equal to EOF. At the very least, using a signed char argument will lead to undefined behavior for those cases where the corresponding int value is negative (and not EOF).

In a number of libc implementations these functions are implemented using lookup tables, as in the following example.

  #define islower(x) (_ctype_info[(int)(x) + 1] & _LOCALE)

With this definition, serious problems can arise when islower() is called with a signed character argument:

char c;
if (scanf("%c", &c)){
    if (islower(c)){ /*...*/

An attacker could provide a negative character value, such as '\156' ( = (char)-100, which is 'S' in extended ASCII). This would cause the table lookup to underflow the table buffer, revealing information about the memory location 99 bytes before the start of the table. The attacker could thus gradually deduce the entire contents of the 127-byte memory block immediately adjacent to the table, and this block could contain sensitive information.

Some libc implementations do not use table lookup, but negative char arguments are still a violation of the function specifications. If you need to pass a signed char value to one of these functions, cast it to unsigned char first.

Triggers

CodeSonar ships with library models that allow it to recognize functions such as libc tolower() that expect an argument representable as unsigned char. If one of these functions is called with an unsuitable value in the relevant parameter position, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Negative Character Value warnings.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.