GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/sanitizer.cc
Date: 2026-03-08 02:37:57
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 41974 CharRange::CharRange(const char range_begin, const char range_end) {
21 41974 range_begin_ = range_begin;
22 41974 range_end_ = range_end;
23 41974 }
24
25
26 36322 CharRange::CharRange(const char single_char) {
27 36322 range_begin_ = range_end_ = single_char;
28 36322 }
29
30
31 501367 bool CharRange::InRange(const char c) const {
32
4/4
✓ Branch 0 taken 411321 times.
✓ Branch 1 taken 90046 times.
✓ Branch 2 taken 266786 times.
✓ Branch 3 taken 144535 times.
501367 return (c >= range_begin_) && (c <= range_end_);
33 }
34
35
36 //------------------------------------------------------------------------------
37
38
39 35964 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40
1/2
✓ Branch 1 taken 35964 times.
✗ Branch 2 not taken.
35964 InitValidRanges(whitelist);
41 35964 }
42
43
44 183 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45 183 : max_length_(max_length) {
46
1/2
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
183 InitValidRanges(whitelist);
47 183 }
48
49
50 36147 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
51 // Parse the whitelist
52 36147 const unsigned length = whitelist.length();
53 36147 unsigned pickup_pos = 0;
54
2/2
✓ Branch 0 taken 120270 times.
✓ Branch 1 taken 36147 times.
156417 for (unsigned i = 0; i < length; ++i) {
55
7/8
✓ Branch 0 taken 84150 times.
✓ Branch 1 taken 36120 times.
✓ Branch 3 taken 41974 times.
✓ Branch 4 taken 42176 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 41974 times.
✓ Branch 7 taken 78296 times.
✓ Branch 8 taken 41974 times.
120270 if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) {
56
1/2
✓ Branch 1 taken 78296 times.
✗ Branch 2 not taken.
78296 const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1);
57
2/3
✓ Branch 1 taken 36322 times.
✓ Branch 2 taken 41974 times.
✗ Branch 3 not taken.
78296 switch (range.length()) {
58 36322 case 1:
59
1/2
✓ Branch 3 taken 36322 times.
✗ Branch 4 not taken.
36322 valid_ranges_.push_back(CharRange(range[0]));
60 36322 break;
61 41974 case 2:
62
1/2
✓ Branch 4 taken 41974 times.
✗ Branch 5 not taken.
41974 valid_ranges_.push_back(CharRange(range[0], range[1]));
63 41974 break;
64 default:
65 assert(false);
66 }
67 78296 ++i;
68 78296 pickup_pos = i + 1;
69 78296 }
70 }
71 36147 }
72
73
74 37746 bool InputSanitizer::Sanitize(std::string::const_iterator begin,
75 std::string::const_iterator end,
76 std::string *filtered_output) const {
77 37746 int pos = 0;
78 37746 bool is_sane = true;
79
2/2
✓ Branch 2 taken 334933 times.
✓ Branch 3 taken 37638 times.
372571 for (; begin != end; ++begin) {
80
2/2
✓ Branch 2 taken 266786 times.
✓ Branch 3 taken 68147 times.
334933 if (CheckRanges(*begin)) {
81
4/4
✓ Branch 0 taken 1851 times.
✓ Branch 1 taken 264935 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 1743 times.
266786 if ((max_length_ >= 0) && (pos >= max_length_)) {
82 108 is_sane = false;
83 108 break;
84 }
85 266678 filtered_output->push_back(*begin);
86 266678 pos++;
87 } else {
88 68147 is_sane = false;
89 }
90 }
91 37746 return is_sane;
92 }
93
94
95 334933 bool InputSanitizer::CheckRanges(const char chr) const {
96
2/2
✓ Branch 1 taken 501367 times.
✓ Branch 2 taken 68147 times.
569514 for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
97
2/2
✓ Branch 2 taken 266786 times.
✓ Branch 3 taken 234581 times.
501367 if (valid_ranges_[j].InRange(chr)) {
98 266786 return true;
99 }
100 }
101 68147 return false;
102 }
103
104
105 408 string InputSanitizer::Filter(const std::string &input) const {
106 408 string filtered_output;
107
1/2
✓ Branch 1 taken 408 times.
✗ Branch 2 not taken.
408 Sanitize(input, &filtered_output);
108 408 return filtered_output;
109 }
110
111
112 37392 bool InputSanitizer::IsValid(const std::string &input) const {
113 37392 string dummy;
114
1/2
✓ Branch 1 taken 37392 times.
✗ Branch 2 not taken.
74784 return Sanitize(input, &dummy);
115 37392 }
116
117
118 630 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 27 times.
✓ Branch 2 taken 603 times.
630 if (std::distance(begin, end) == 0) {
122 27 return false;
123 }
124
125
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 549 times.
603 if (*begin == '-') {
126 // minus is allowed as the first character!
127 54 filtered_output->push_back('-');
128 54 begin++;
129 }
130
131 603 return InputSanitizer::Sanitize(begin, end, filtered_output);
132 }
133
134
135 270 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 27 times.
✓ Branch 2 taken 243 times.
270 if (std::distance(begin, end) == 0) {
139 27 return false;
140 }
141
142 243 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