File: bugprone-not-null-terminated-result-wmemcpy-safe-cxx.cpp

package info (click to toggle)
llvm-toolchain-11 1%3A11.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 995,808 kB
  • sloc: cpp: 4,767,656; ansic: 760,916; asm: 477,436; python: 170,940; objc: 69,804; lisp: 29,914; sh: 23,855; f90: 18,173; pascal: 7,551; perl: 7,471; ml: 5,603; awk: 3,489; makefile: 2,573; xml: 915; cs: 573; fortran: 503; javascript: 452
file content (111 lines) | stat: -rw-r--r-- 4,256 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
// RUN: -- -std=c++11 -I %S/Inputs/bugprone-not-null-terminated-result

#include "not-null-terminated-result-cxx.h"

#define __STDC_LIB_EXT1__ 1
#define __STDC_WANT_LIB_EXT1__ 1

//===----------------------------------------------------------------------===//
// wmemcpy() - destination array tests
//===----------------------------------------------------------------------===//

void bad_wmemcpy_known_dest(const wchar_t *src) {
  wchar_t dest01[13];
  wmemcpy(dest01, src, wcslen(src));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wchar_t dest01[14];
  // CHECK-FIXES-NEXT: wcscpy_s(dest01, src);
}

void good_wmemcpy_known_dest(const wchar_t *src) {
  wchar_t dst01[14];
  wcscpy_s(dst01, src);
}

//===----------------------------------------------------------------------===//
// wmemcpy() - length tests
//===----------------------------------------------------------------------===//

void bad_wmemcpy_full_source_length(const wchar_t *src) {
  wchar_t dest20[13];
  wmemcpy(dest20, src, wcslen(src));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wchar_t dest20[14];
  // CHECK-FIXES-NEXT: wcscpy_s(dest20, src);
}

void good_wmemcpy_full_source_length(const wchar_t *src) {
  wchar_t dst20[14];
  wcscpy_s(dst20, src);
}

void bad_wmemcpy_partial_source_length(const wchar_t *src) {
  wchar_t dest21[13];
  wmemcpy(dest21, src, wcslen(src) - 1);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wchar_t dest21[14];
  // CHECK-FIXES-NEXT: wcsncpy_s(dest21, src, wcslen(src) - 1);
}

void good_wmemcpy_partial_source_length(const wchar_t *src) {
  wchar_t dst21[14];
  wcsncpy_s(dst21, src, wcslen(src) - 1);
}

//===----------------------------------------------------------------------===//
// wmemcpy_s() - destination array tests
//===----------------------------------------------------------------------===//

void bad_wmemcpy_s_unknown_dest(wchar_t *dest40, const wchar_t *src) {
  wmemcpy_s(dest40, 13, src, wcslen(src));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wcscpy_s(dest40, 13, src);
}

void good_wmemcpy_s_unknown_dest(wchar_t *dst40, const wchar_t *src) {
  wcscpy_s(dst40, 13, src);
}

void bad_wmemcpy_s_known_dest(const wchar_t *src) {
  wchar_t dest41[13];
  wmemcpy_s(dest41, 13, src, wcslen(src));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wchar_t dest41[14];
  // CHECK-FIXES-NEXT: wcscpy_s(dest41, src);
}

void good_wmemcpy_s_known_dest(const wchar_t *src) {
  wchar_t dst41[13];
  wcscpy_s(dst41, src);
}

//===----------------------------------------------------------------------===//
// wmemcpy_s() - length tests
//===----------------------------------------------------------------------===//

void bad_wmemcpy_s_full_source_length(const wchar_t *src) {
  wchar_t dest60[13];
  wmemcpy_s(dest60, 13, src, wcslen(src));
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wchar_t dest60[14];
  // CHECK-FIXES-NEXT: wcscpy_s(dest60, src);
}

void good_wmemcpy_s_full_source_length(const wchar_t *src) {
  wchar_t dst60[13];
  wcscpy_s(dst60, src);
}

void bad_wmemcpy_s_partial_source_length(const wchar_t *src) {
  wchar_t dest61[13];
  wmemcpy_s(dest61, 13, src, wcslen(src) - 1);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'wmemcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
  // CHECK-FIXES: wchar_t dest61[14];
  // CHECK-FIXES-NEXT: wcsncpy_s(dest61, src, wcslen(src) - 1);
}

void good_wmemcpy_s_partial_source_length(const wchar_t *src) {
  wchar_t dst61[13];
  wcsncpy_s(dst61, src, wcslen(src) - 1);
}