CXX.DIFF.WIDTH.STR_AND_FUNC

Narrow and wide character strings used interchangeably in functions.

The CXX.DIFF.WIDTH.STR_AND_FUNC checker finds instances in which a narrow string argument is passed to a wide string function or where a wide string argument is passed to a narrow string function.

Vulnerability and risk

There is a risk of scaling problems because wide and narrow characters are different in size. Also, determining the length of some strings is problematic because wide strings are terminated by a null wide character and can contain null bytes.

Mitigation and prevention

To avoid this issue, use proper width functions and correct narrow and wide character strings when calling functions.

Vulnerable code example 1

1  #include <stddef.h>
2  #include <string.h>
3    
4  void func(void) {
5    wchar_t wide_str1[]  = L"0123456789";
6    wchar_t wide_str2[] =  L"0000000000";
7   
8    strncpy(wide_str2, wide_str1, 10);  //Non-compliant code
9  }

In this example, Klocwork reports a defect on line 8 in reference to the strncpy() function where a wide character string is passed to a function that is expecting a narrow character string.

Fixed code example 1

1  #include <string.h>
2  #include <wchar.h>
3    
4  void func(void) {
5    wchar_t wide_str1[] = L"0123456789";
6    wchar_t wide_str2[] = L"0000000000";
7    /* Use of proper-width function */
8    wcsncpy(wide_str2, wide_str1, 10);    /Compliant code
9   }

This fixed example uses the proper-width function, wcsncpy(), for copying wide character strings, which ensures that data is not truncated.

Vulnerable code example 2

1   #include <stdlib.h>
2   #include <string.h>
3     
4   void func(void) {
5     wchar_t wide_str1[] = L"0123456789";
6     wchar_t *wide_str2 = (wchar_t*)malloc(strlen(wide_str1) + 1); //Non-compliant
7     if (wide_str2 == NULL) {
8       /* Handle error */
9     }
10    /* ... */
11    free(wide_str2);
12    wide_str2 = NULL;
13  }

In this example, Klocwork reports a defect in reference to the code using the strlen() function to determine the size of a wide character string.

Fixed code example 2

1   #include <stdlib.h>
2   #include <wchar.h>
3     
4   void func(void) {
5     wchar_t wide_str1[] = L"0123456789";
6     wchar_t *wide_str2 = (wchar_t *)malloc(
7       (wcslen(wide_str1) + 1) * sizeof(wchar_t));
8     if (wide_str2 == NULL) {
9       /* Handle error */
10    }
11    /* ... */
12    free(wide_str2);
13    wide_str2 = NULL;
14  }

This fixed example correctly calculates the number of bytes required to contain a copy of the wide string, including the terminating null wide character.