GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/sanitizer.cc
Date: 2025-06-22 02:36:02
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 38829 CharRange::CharRange(const char range_begin, const char range_end) {
21 38829 range_begin_ = range_begin;
22 38829 range_end_ = range_end;
23 38829 }
24
25
26 37085 CharRange::CharRange(const char single_char) {
27 37085 range_begin_ = range_end_ = single_char;
28 37085 }
29
30
31 392313 bool CharRange::InRange(const char c) const {
32
4/4
✓ Branch 0 taken 316882 times.
✓ Branch 1 taken 75431 times.
✓ Branch 2 taken 250173 times.
✓ Branch 3 taken 66709 times.
392313 return (c >= range_begin_) && (c <= range_end_);
33 }
34
35
36 //------------------------------------------------------------------------------
37
38
39 36047 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40
1/2
✓ Branch 1 taken 36047 times.
✗ Branch 2 not taken.
36047 InitValidRanges(whitelist);
41 36047 }
42
43
44 245 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45 245 : max_length_(max_length) {
46
1/2
✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
245 InitValidRanges(whitelist);
47 245 }
48
49
50 36292 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
51 // Parse the whitelist
52 36292 const unsigned length = whitelist.length();
53 36292 unsigned pickup_pos = 0;
54
2/2
✓ Branch 0 taken 114743 times.
✓ Branch 1 taken 36292 times.
151035 for (unsigned i = 0; i < length; ++i) {
55
7/8
✓ Branch 0 taken 78500 times.
✓ Branch 1 taken 36243 times.
✓ Branch 3 taken 38829 times.
✓ Branch 4 taken 39671 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38829 times.
✓ Branch 7 taken 75914 times.
✓ Branch 8 taken 38829 times.
114743 if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) {
56
1/2
✓ Branch 1 taken 75914 times.
✗ Branch 2 not taken.
75914 const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1);
57
2/3
✓ Branch 1 taken 37085 times.
✓ Branch 2 taken 38829 times.
✗ Branch 3 not taken.
75914 switch (range.length()) {
58 37085 case 1:
59
1/2
✓ Branch 3 taken 37085 times.
✗ Branch 4 not taken.
37085 valid_ranges_.push_back(CharRange(range[0]));
60 37085 break;
61 38829 case 2:
62
1/2
✓ Branch 4 taken 38829 times.
✗ Branch 5 not taken.
38829 valid_ranges_.push_back(CharRange(range[0], range[1]));
63 38829 break;
64 default:
65 assert(false);
66 }
67 75914 ++i;
68 75914 pickup_pos = i + 1;
69 75914 }
70 }
71 36292 }
72
73
74 38061 bool InputSanitizer::Sanitize(std::string::const_iterator begin,
75 std::string::const_iterator end,
76 std::string *filtered_output) const {
77 38061 int pos = 0;
78 38061 bool is_sane = true;
79
2/2
✓ Branch 2 taken 283802 times.
✓ Branch 3 taken 37865 times.
321667 for (; begin != end; ++begin) {
80
2/2
✓ Branch 2 taken 250173 times.
✓ Branch 3 taken 33629 times.
283802 if (CheckRanges(*begin)) {
81
4/4
✓ Branch 0 taken 2401 times.
✓ Branch 1 taken 247772 times.
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 2205 times.
250173 if ((max_length_ >= 0) && (pos >= max_length_)) {
82 196 is_sane = false;
83 196 break;
84 }
85 249977 filtered_output->push_back(*begin);
86 249977 pos++;
87 } else {
88 33629 is_sane = false;
89 }
90 }
91 38061 return is_sane;
92 }
93
94
95 283802 bool InputSanitizer::CheckRanges(const char chr) const {
96
2/2
✓ Branch 1 taken 392313 times.
✓ Branch 2 taken 33629 times.
425942 for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
97
2/2
✓ Branch 2 taken 250173 times.
✓ Branch 3 taken 142140 times.
392313 if (valid_ranges_[j].InRange(chr)) {
98 250173 return true;
99 }
100 }
101 33629 return false;
102 }
103
104
105 1176 string InputSanitizer::Filter(const std::string &input) const {
106 1176 string filtered_output;
107
1/2
✓ Branch 1 taken 1176 times.
✗ Branch 2 not taken.
1176 Sanitize(input, &filtered_output);
108 1176 return filtered_output;
109 }
110
111
112 36983 bool InputSanitizer::IsValid(const std::string &input) const {
113 36983 string dummy;
114
1/2
✓ Branch 1 taken 36983 times.
✗ Branch 2 not taken.
73966 return Sanitize(input, &dummy);
115 36983 }
116
117
118 610 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 49 times.
✓ Branch 2 taken 561 times.
610 if (std::distance(begin, end) == 0) {
122 49 return false;
123 }
124
125
2/2
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 463 times.
561 if (*begin == '-') {
126 // minus is allowed as the first character!
127 98 filtered_output->push_back('-');
128 98 begin++;
129 }
130
131 561 return InputSanitizer::Sanitize(begin, end, filtered_output);
132 }
133
134
135 490 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 49 times.
✓ Branch 2 taken 441 times.
490 if (std::distance(begin, end) == 0) {
139 49 return false;
140 }
141
142 441 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