Versions Compared

Key

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

Passing narrow string arguments to wide string functions or wide string arguments to narrow string functions can lead to unexpected and undefined behavior 151. Scaling problems are likely 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 wide strings are terminated by a null wide character and can contain null bytes, determining the length is also problematic.

...

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

Rule

Severity

Likelihood

Detectable

Remediation CostRepairable

Priority

Level

STR38-C

High

Likely

Yes

LowNo

P27P18

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-cFully implemented
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.

...