Skip to main content

IAR Embedded Workbench for RISC-V 3.40

CERT-STR38-C

In this section:
Synopsis

Do not confuse narrow and wide character strings and functions.

Enabled by default

Yes

Severity/Certainty

Low/Low

lowlow.png
Full description

Passing narrow string arguments to wide string functions or wide string arguments to narrow string functions can lead to unexpected and undefined behavior. Scaling problems are likely because of the difference in size between wide and narrow characters.

Note: This check is not part of C-STAT® but detected by the IAR compiler.

Coding standards
CERT STR38-C

Do not use wide-char functions on narrow-char strings and vice versa

Code examples

The following code example fails the check and will give a warning:

#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);
}

The following code example passes the check and will not give a warning about this issue:

#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);
}