1// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
2// RUN: -- -std=c++11 -I %S/Inputs/not-null-terminated-result
3
4#include "not-null-terminated-result-cxx.h"
5
6#define __STDC_LIB_EXT1__ 1
7#define __STDC_WANT_LIB_EXT1__ 1
8
9//===----------------------------------------------------------------------===//
10// wmemcpy() - destination array tests
11//===----------------------------------------------------------------------===//
12
13void bad_wmemcpy_known_dest(const wchar_t *src) {
14 wchar_t dest01[13];
15 wmemcpy(dest01, src, wcslen(src));
16 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy' is not null-terminated [bugprone-not-null-terminated-result]
17 // CHECK-FIXES: wchar_t dest01[14];
18 // CHECK-FIXES-NEXT: wcscpy_s(dest01, src);
19}
20
21void good_wmemcpy_known_dest(const wchar_t *src) {
22 wchar_t dst01[14];
23 wcscpy_s(dst01, src);
24}
25
26//===----------------------------------------------------------------------===//
27// wmemcpy() - length tests
28//===----------------------------------------------------------------------===//
29
30void bad_wmemcpy_full_source_length(const wchar_t *src) {
31 wchar_t dest20[13];
32 wmemcpy(dest20, src, wcslen(src));
33 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy' is not null-terminated [bugprone-not-null-terminated-result]
34 // CHECK-FIXES: wchar_t dest20[14];
35 // CHECK-FIXES-NEXT: wcscpy_s(dest20, src);
36}
37
38void good_wmemcpy_full_source_length(const wchar_t *src) {
39 wchar_t dst20[14];
40 wcscpy_s(dst20, src);
41}
42
43void bad_wmemcpy_partial_source_length(const wchar_t *src) {
44 wchar_t dest21[13];
45 wmemcpy(dest21, src, wcslen(src) - 1);
46 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy' is not null-terminated [bugprone-not-null-terminated-result]
47 // CHECK-FIXES: wchar_t dest21[14];
48 // CHECK-FIXES-NEXT: wcsncpy_s(dest21, src, wcslen(src) - 1);
49}
50
51void good_wmemcpy_partial_source_length(const wchar_t *src) {
52 wchar_t dst21[14];
53 wcsncpy_s(dst21, src, wcslen(src) - 1);
54}
55
56//===----------------------------------------------------------------------===//
57// wmemcpy_s() - destination array tests
58//===----------------------------------------------------------------------===//
59
60void bad_wmemcpy_s_unknown_dest(wchar_t *dest40, const wchar_t *src) {
61 wmemcpy_s(dest40, 13, src, wcslen(src));
62 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
63 // CHECK-FIXES: wcscpy_s(dest40, 13, src);
64}
65
66void good_wmemcpy_s_unknown_dest(wchar_t *dst40, const wchar_t *src) {
67 wcscpy_s(dst40, 13, src);
68}
69
70void bad_wmemcpy_s_known_dest(const wchar_t *src) {
71 wchar_t dest41[13];
72 wmemcpy_s(dest41, 13, src, wcslen(src));
73 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
74 // CHECK-FIXES: wchar_t dest41[14];
75 // CHECK-FIXES-NEXT: wcscpy_s(dest41, src);
76}
77
78void good_wmemcpy_s_known_dest(const wchar_t *src) {
79 wchar_t dst41[13];
80 wcscpy_s(dst41, src);
81}
82
83//===----------------------------------------------------------------------===//
84// wmemcpy_s() - length tests
85//===----------------------------------------------------------------------===//
86
87void bad_wmemcpy_s_full_source_length(const wchar_t *src) {
88 wchar_t dest60[13];
89 wmemcpy_s(dest60, 13, src, wcslen(src));
90 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
91 // CHECK-FIXES: wchar_t dest60[14];
92 // CHECK-FIXES-NEXT: wcscpy_s(dest60, src);
93}
94
95void good_wmemcpy_s_full_source_length(const wchar_t *src) {
96 wchar_t dst60[13];
97 wcscpy_s(dst60, src);
98}
99
100void bad_wmemcpy_s_partial_source_length(const wchar_t *src) {
101 wchar_t dest61[13];
102 wmemcpy_s(dest61, 13, src, wcslen(src) - 1);
103 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
104 // CHECK-FIXES: wchar_t dest61[14];
105 // CHECK-FIXES-NEXT: wcsncpy_s(dest61, src, wcslen(src) - 1);
106}
107
108void good_wmemcpy_s_partial_source_length(const wchar_t *src) {
109 wchar_t dst61[13];
110 wcsncpy_s(dst61, src, wcslen(src) - 1);
111}
112

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/not-null-terminated-result-wmemcpy-safe-cxx.cpp