File: suspicious-stringview-data-usage.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,388 kB
  • sloc: cpp: 7,438,767; ansic: 1,393,871; asm: 1,012,926; python: 241,728; f90: 86,635; objc: 75,411; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (56 lines) | stat: -rw-r--r-- 2,659 bytes parent folder | download | duplicates (18)
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
// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-suspicious-stringview-data-usage %t -- -- -isystem %clang_tidy_headers
#include <string>

struct View {
   const char* str;
};

struct Pair {
   const char* begin;
   const char* end;
};

struct ViewWithSize {
   const char* str;
   std::string_view::size_type size;
};

void something(const char*);
void something(const char*, unsigned);
void something(const char*, unsigned, const char*);
void something_str(std::string, unsigned);

void invalid(std::string_view sv, std::string_view sv2) {
  std::string s(sv.data());
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
  std::string si{sv.data()};
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
  std::string_view s2(sv.data());
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
  something(sv.data());
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
  something(sv.data(), sv.size(), sv2.data());
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
  something_str(sv.data(), sv.size());
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
  View view{sv.data()};
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
}

void valid(std::string_view sv) {
  std::string s1(sv.data(), sv.data() + sv.size());
  std::string s2(sv.data(), sv.data() + sv.length());
  std::string s3(sv.data(), sv.size() + sv.data());
  std::string s4(sv.data(), sv.length() + sv.data());
  std::string s5(sv.data(), sv.size());
  std::string s6(sv.data(), sv.length());
  something(sv.data(), sv.size());
  something(sv.data(), sv.length());
  ViewWithSize view1{sv.data(), sv.size()};
  ViewWithSize view2{sv.data(), sv.length()};
  Pair view3{sv.data(), sv.data() + sv.size()};
  Pair view4{sv.data(), sv.data() + sv.length()};
  Pair view5{sv.data(), sv.size() + sv.data()};
  Pair view6{sv.data(), sv.length() + sv.data()};
  const char* str{sv.data()};
}