Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Passing narrow strings string arguments to wide string functions or wide strings string arguments to narrow string functions can lead to unexpected and undefined behavior 151. Scaling problems are likely (see because of the difference in size between wide and narrow characters. (See ARR39-C. Do not add or subtract a scaled integer to a pointer) because of the difference in size between wide and narrow characters..) Because wide strings are terminated by a null wide character and can contain null bytes, determining the length is also problematic.

Because wchar_t and char are distinct types, many compilers will produce a warning diagnostic if the an inappropriate function is used. (see See MSC00-C. Compile cleanly at high warning levels.).

Noncompliant Code Example (Wide Strings with Narrow String Functions) 

This noncompliant code example incorrectly uses the strncpy() function in an attempt to copy up to 10 wide characters. However, because wide characters can contain null bytes, the copy operation may end earlier than anticipated, resulting in the truncation of the wide string.

Code Block
bgColor#ffcccc
langc
#include <stddef.h>
#include <string.h>
 
void func(void) {
  wchar_t wide_str1[]  = L"0123456789";
  wchar_t wide_str2[] =  L"0000000000";

  strncpy(wide_str2, wide_str1, 10);
}

...

Code Block
bgColor#ccccff
langc
#include <stddef.h>
#include <string.h>
#include <wchar.h>
 
void func(void) {
  wchar_t wide_str1[] = L"0123456789";
  wchar_t wide_str2[] = L"0000000000";
  /* Use of proper-width function */ 
  wcsncpy(wide_str2, wide_str1, 10);

  char narrow_str1[] = "0123456789";
  char narrow_str2[] = "0000000000";
  /* Use of proper-width function */ 
  strncpy(narrow_str2, narrow_str1, 10);
}

...

Code Block
bgColor#FFcccc
langc
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
 
void func(void) {
  wchar_t wide_str1[] = L"0123456789";
  wchar_t *wide_str2 = (wchar_t*)malloc(strlen(wide_str1) + 1);
  if (wide_str2 == NULL) {
    /* Handle error */
  }

  /* ... */
  free(wide_str2);
  wide_str2 = NULL;
}

...

This compliant solution correctly calculates the number of bytes required to contain a copy of the wide string, including the terminating null wide character:

Code Block
bgColor#ccccff
langc
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>
 
void func(void) {
  wchar_t wide_str1[] = L"0123456789";
  wchar_t *wide_str2 = (wchar_t *)malloc(
    (wcslen(wide_str1) + 1) * sizeof(wchar_t));
  if (wide_str2 == NULL) {
    /* Handle error */
  }
  /* ... */

  free(wide_str2);
  wide_str2 = NULL;
}

...

Confusing narrow and wide character strings can result in buffer overflows, data truncation, and other defects.

Rule

Severity

Likelihood

Remediation Cost

Detectable

Repairable

Priority

Level

STR38-C

High

Likely

Yes

Low

No

P27

P18

L1

Automated Detection

Modern compilers recognize the difference between a char * and a wchar_t *, so compiling code that violates this rule will generate warnings. It is feasible to have automated software that recognizes functions of improper width and replaces them with functions of proper width (that is, software that uses wcsncpy() when it recognizes that the parameters are of type wchar_t *).

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
wide-narrow-string-cast
wide-narrow-string-cast-implicit
Partially checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-STR38Fully implemented
Clang
Include Page
Clang_V
Clang_V
-Wincompatible-pointer-types
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.MEM.BO
LANG.MEM.TBA

Buffer Overrun
Tainted Buffer Access

Coverity
Include Page
Coverity_V
Coverity_V

PW

Implemented
Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-str38-c
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0432

C++0403 


Klocwork
Include Page
Klocwork_V
Klocwork_V

CXX.DIFF.WIDTH.STR_AND_FUNC


Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-STR38-a
Do not confuse narrow and wide character strings and functions
PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

2454, 2480, 2481

Partially supported: reports illegal conversions involving pointers to char or wchar_t as well as byte/wide-oriented stream inconsistencies

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule STR38-C

Checks for misuse of narrow or wide character string (rule fully covered)

RuleChecker

Include Page
RuleChecker_V
RuleChecker_V

wide-narrow-string-cast
wide-narrow-string-cast-implicit
Partially checked
Security Reviewer - Static Reviewer

Include Page
Security Reviewer - Static Reviewer_V
Security Reviewer - Static Reviewer_V

UNSAFE_02Fully implemented
TrustInSoft Analyzer

Include Page
TrustInSoft Analyzer_V
TrustInSoft Analyzer_V

pointer arithmeticPartially verified.


Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Bibliography

[ISO/IEC 9899:2024]7.26.2.5, "The strncpy Function"
7.31.4.2.2, "The wcsncpy Function"


...

Image Modified Image Modified Image Modified