GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/sanitizer.cc
Date: 2026-04-26 02:35:59
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 35134 CharRange::CharRange(const char range_begin, const char range_end) {
21 35134 range_begin_ = range_begin;
22 35134 range_end_ = range_end;
23 35134 }
24
25
26 31734 CharRange::CharRange(const char single_char) {
27 31734 range_begin_ = range_end_ = single_char;
28 31734 }
29
30
31 401005 bool CharRange::InRange(const char c) const {
32
4/4
✓ Branch 0 taken 326594 times.
✓ Branch 1 taken 74411 times.
✓ Branch 2 taken 223676 times.
✓ Branch 3 taken 102918 times.
401005 return (c >= range_begin_) && (c <= range_end_);
33 }
34
35
36 //------------------------------------------------------------------------------
37
38
39 31083 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40
1/2
✓ Branch 1 taken 31083 times.
✗ Branch 2 not taken.
31083 InitValidRanges(whitelist);
41 31083 }
42
43
44 149 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45 149 : max_length_(max_length) {
46
1/2
✓ Branch 1 taken 149 times.
✗ Branch 2 not taken.
149 InitValidRanges(whitelist);
47 149 }
48
49
50 31232 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
51 // Parse the whitelist
52 31232 const unsigned length = whitelist.length();
53 31232 unsigned pickup_pos = 0;
54
2/2
✓ Branch 0 taken 102002 times.
✓ Branch 1 taken 31232 times.
133234 for (unsigned i = 0; i < length; ++i) {
55
7/8
✓ Branch 0 taken 70804 times.
✓ Branch 1 taken 31198 times.
✓ Branch 3 taken 35134 times.
✓ Branch 4 taken 35670 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 35134 times.
✓ Branch 7 taken 66868 times.
✓ Branch 8 taken 35134 times.
102002 if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) {
56
1/2
✓ Branch 1 taken 66868 times.
✗ Branch 2 not taken.
66868 const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1);
57
2/3
✓ Branch 1 taken 31734 times.
✓ Branch 2 taken 35134 times.
✗ Branch 3 not taken.
66868 switch (range.length()) {
58 31734 case 1:
59
1/2
✓ Branch 3 taken 31734 times.
✗ Branch 4 not taken.
31734 valid_ranges_.push_back(CharRange(range[0]));
60 31734 break;
61 35134 case 2:
62
1/2
✓ Branch 4 taken 35134 times.
✗ Branch 5 not taken.
35134 valid_ranges_.push_back(CharRange(range[0], range[1]));
63 35134 break;
64 default:
65 assert(false);
66 }
67 66868 ++i;
68 66868 pickup_pos = i + 1;
69 66868 }
70 }
71 31232 }
72
73
74 33176 bool InputSanitizer::Sanitize(std::string::const_iterator begin,
75 std::string::const_iterator end,
76 std::string *filtered_output) const {
77 33176 int pos = 0;
78 33176 bool is_sane = true;
79
2/2
✓ Branch 2 taken 272894 times.
✓ Branch 3 taken 33040 times.
305934 for (; begin != end; ++begin) {
80
2/2
✓ Branch 2 taken 223676 times.
✓ Branch 3 taken 49218 times.
272894 if (CheckRanges(*begin)) {
81
4/4
✓ Branch 0 taken 1435 times.
✓ Branch 1 taken 222241 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 1299 times.
223676 if ((max_length_ >= 0) && (pos >= max_length_)) {
82 136 is_sane = false;
83 136 break;
84 }
85 223540 filtered_output->push_back(*begin);
86 223540 pos++;
87 } else {
88 49218 is_sane = false;
89 }
90 }
91 33176 return is_sane;
92 }
93
94
95 272894 bool InputSanitizer::CheckRanges(const char chr) const {
96
2/2
✓ Branch 1 taken 401005 times.
✓ Branch 2 taken 49218 times.
450223 for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
97
2/2
✓ Branch 2 taken 223676 times.
✓ Branch 3 taken 177329 times.
401005 if (valid_ranges_[j].InRange(chr)) {
98 223676 return true;
99 }
100 }
101 49218 return false;
102 }
103
104
105 936 string InputSanitizer::Filter(const std::string &input) const {
106 936 string filtered_output;
107
1/2
✓ Branch 1 taken 936 times.
✗ Branch 2 not taken.
936 Sanitize(input, &filtered_output);
108 936 return filtered_output;
109 }
110
111
112 32308 bool InputSanitizer::IsValid(const std::string &input) const {
113 32308 string dummy;
114
1/2
✓ Branch 1 taken 32308 times.
✗ Branch 2 not taken.
64616 return Sanitize(input, &dummy);
115 32308 }
116
117
118 832 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 34 times.
✓ Branch 2 taken 798 times.
832 if (std::distance(begin, end) == 0) {
122 34 return false;
123 }
124
125
2/2
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 730 times.
798 if (*begin == '-') {
126 // minus is allowed as the first character!
127 68 filtered_output->push_back('-');
128 68 begin++;
129 }
130
131 798 return InputSanitizer::Sanitize(begin, end, filtered_output);
132 }
133
134
135 340 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 34 times.
✓ Branch 2 taken 306 times.
340 if (std::distance(begin, end) == 0) {
139 34 return false;
140 }
141
142 306 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