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

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (111 lines) | stat: -rw-r--r-- 4,247 bytes parent folder | download | duplicates (15)
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/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);
}