GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/sanitizer.cc
Date: 2026-07-26 02:35:14
Exec Total Coverage
Lines: 69 71 97.2%
Branches: 40 49 81.6%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Provides input data sanitizer in the form of whitelist of character ranges.
5 */
6
7
8 #include "sanitizer.h"
9
10 #include <cassert>
11
12 using namespace std; // NOLINT
13
14 #ifdef CVMFS_NAMESPACE_GUARD
15 namespace CVMFS_NAMESPACE_GUARD {
16 #endif
17
18 namespace sanitizer {
19
20 25949 CharRange::CharRange(const char range_begin, const char range_end) {
21 25949 range_begin_ = range_begin;
22 25949 range_end_ = range_end;
23 25949 }
24
25
26 24316 CharRange::CharRange(const char single_char) {
27 24316 range_begin_ = range_end_ = single_char;
28 24316 }
29
30
31 262721 bool CharRange::InRange(const char c) const {
32
4/4
✓ Branch 0 taken 212074 times.
✓ Branch 1 taken 50647 times.
✓ Branch 2 taken 165021 times.
✓ Branch 3 taken 47053 times.
262721 return (c >= range_begin_) && (c <= range_end_);
33 }
34
35
36 //------------------------------------------------------------------------------
37
38
39 23817 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40
1/2
✓ Branch 1 taken 23817 times.
✗ Branch 2 not taken.
23817 InitValidRanges(whitelist);
41 23817 }
42
43
44 180 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45 180 : max_length_(max_length) {
46
1/2
✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
180 InitValidRanges(whitelist);
47 180 }
48
49
50 23997 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
51 // Parse the whitelist
52 23997 const unsigned length = whitelist.length();
53 23997 unsigned pickup_pos = 0;
54
2/2
✓ Branch 0 taken 76214 times.
✓ Branch 1 taken 23997 times.
100211 for (unsigned i = 0; i < length; ++i) {
55
7/8
✓ Branch 0 taken 52253 times.
✓ Branch 1 taken 23961 times.
✓ Branch 3 taken 25949 times.
✓ Branch 4 taken 26304 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25949 times.
✓ Branch 7 taken 50265 times.
✓ Branch 8 taken 25949 times.
76214 if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) {
56
1/2
✓ Branch 1 taken 50265 times.
✗ Branch 2 not taken.
50265 const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1);
57
2/3
✓ Branch 1 taken 24316 times.
✓ Branch 2 taken 25949 times.
✗ Branch 3 not taken.
50265 switch (range.length()) {
58 24316 case 1:
59
1/2
✓ Branch 3 taken 24316 times.
✗ Branch 4 not taken.
24316 valid_ranges_.push_back(CharRange(range[0]));
60 24316 break;
61 25949 case 2:
62
1/2
✓ Branch 4 taken 25949 times.
✗ Branch 5 not taken.
25949 valid_ranges_.push_back(CharRange(range[0], range[1]));
63 25949 break;
64 default:
65 assert(false);
66 }
67 50265 ++i;
68 50265 pickup_pos = i + 1;
69 50265 }
70 }
71 23997 }
72
73
74 25890 bool InputSanitizer::Sanitize(std::string::const_iterator begin,
75 std::string::const_iterator end,
76 std::string *filtered_output) const {
77 25890 int pos = 0;
78 25890 bool is_sane = true;
79
2/2
✓ Branch 2 taken 188834 times.
✓ Branch 3 taken 25746 times.
214580 for (; begin != end; ++begin) {
80
2/2
✓ Branch 2 taken 165021 times.
✓ Branch 3 taken 23813 times.
188834 if (CheckRanges(*begin)) {
81
4/4
✓ Branch 0 taken 1764 times.
✓ Branch 1 taken 163257 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 1620 times.
165021 if ((max_length_ >= 0) && (pos >= max_length_)) {
82 144 is_sane = false;
83 144 break;
84 }
85 164877 filtered_output->push_back(*begin);
86 164877 pos++;
87 } else {
88 23813 is_sane = false;
89 }
90 }
91 25890 return is_sane;
92 }
93
94
95 188834 bool InputSanitizer::CheckRanges(const char chr) const {
96
2/2
✓ Branch 1 taken 262721 times.
✓ Branch 2 taken 23813 times.
286534 for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
97
2/2
✓ Branch 2 taken 165021 times.
✓ Branch 3 taken 97700 times.
262721 if (valid_ranges_[j].InRange(chr)) {
98 165021 return true;
99 }
100 }
101 23813 return false;
102 }
103
104
105 900 string InputSanitizer::Filter(const std::string &input) const {
106 900 string filtered_output;
107
1/2
✓ Branch 1 taken 900 times.
✗ Branch 2 not taken.
900 Sanitize(input, &filtered_output);
108 900 return filtered_output;
109 }
110
111
112 25062 bool InputSanitizer::IsValid(const std::string &input) const {
113 25062 string dummy;
114
1/2
✓ Branch 1 taken 25062 times.
✗ Branch 2 not taken.
50124 return Sanitize(input, &dummy);
115 25062 }
116
117
118 1356 bool IntegerSanitizer::Sanitize(std::string::const_iterator begin,
119 std::string::const_iterator end,
120 std::string *filtered_output) const {
121
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 1320 times.
1356 if (std::distance(begin, end) == 0) {
122 36 return false;
123 }
124
125
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 1248 times.
1320 if (*begin == '-') {
126 // minus is allowed as the first character!
127 72 filtered_output->push_back('-');
128 72 begin++;
129 }
130
131 1320 return InputSanitizer::Sanitize(begin, end, filtered_output);
132 }
133
134
135 360 bool PositiveIntegerSanitizer::Sanitize(std::string::const_iterator begin,
136 std::string::const_iterator end,
137 std::string *filtered_output) const {
138
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 324 times.
360 if (std::distance(begin, end) == 0) {
139 36 return false;
140 }
141
142 324 return InputSanitizer::Sanitize(begin, end, filtered_output);
143 }
144
145 } // namespace sanitizer
146
147 #ifdef CVMFS_NAMESPACE_GUARD
148 } // namespace CVMFS_NAMESPACE_GUARD
149 #endif
150