GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/sanitizer.cc
Date: 2025-12-21 02:39:23
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 38030 CharRange::CharRange(const char range_begin, const char range_end) {
21 38030 range_begin_ = range_begin;
22 38030 range_end_ = range_end;
23 38030 }
24
25
26 32824 CharRange::CharRange(const char single_char) {
27 32824 range_begin_ = range_end_ = single_char;
28 32824 }
29
30
31 447897 bool CharRange::InRange(const char c) const {
32
4/4
✓ Branch 0 taken 369406 times.
✓ Branch 1 taken 78491 times.
✓ Branch 2 taken 239307 times.
✓ Branch 3 taken 130099 times.
447897 return (c >= range_begin_) && (c <= range_end_);
33 }
34
35
36 //------------------------------------------------------------------------------
37
38
39 32597 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40
1/2
✓ Branch 1 taken 32597 times.
✗ Branch 2 not taken.
32597 InitValidRanges(whitelist);
41 32597 }
42
43
44 135 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45 135 : max_length_(max_length) {
46
1/2
✓ Branch 1 taken 135 times.
✗ Branch 2 not taken.
135 InitValidRanges(whitelist);
47 135 }
48
49
50 32732 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
51 // Parse the whitelist
52 32732 const unsigned length = whitelist.length();
53 32732 unsigned pickup_pos = 0;
54
2/2
✓ Branch 0 taken 108884 times.
✓ Branch 1 taken 32732 times.
141616 for (unsigned i = 0; i < length; ++i) {
55
7/8
✓ Branch 0 taken 76158 times.
✓ Branch 1 taken 32726 times.
✓ Branch 3 taken 38030 times.
✓ Branch 4 taken 38128 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38030 times.
✓ Branch 7 taken 70854 times.
✓ Branch 8 taken 38030 times.
108884 if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) {
56
1/2
✓ Branch 1 taken 70854 times.
✗ Branch 2 not taken.
70854 const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1);
57
2/3
✓ Branch 1 taken 32824 times.
✓ Branch 2 taken 38030 times.
✗ Branch 3 not taken.
70854 switch (range.length()) {
58 32824 case 1:
59
1/2
✓ Branch 3 taken 32824 times.
✗ Branch 4 not taken.
32824 valid_ranges_.push_back(CharRange(range[0]));
60 32824 break;
61 38030 case 2:
62
1/2
✓ Branch 4 taken 38030 times.
✗ Branch 5 not taken.
38030 valid_ranges_.push_back(CharRange(range[0], range[1]));
63 38030 break;
64 default:
65 assert(false);
66 }
67 70854 ++i;
68 70854 pickup_pos = i + 1;
69 70854 }
70 }
71 32732 }
72
73
74 33859 bool InputSanitizer::Sanitize(std::string::const_iterator begin,
75 std::string::const_iterator end,
76 std::string *filtered_output) const {
77 33859 int pos = 0;
78 33859 bool is_sane = true;
79
2/2
✓ Branch 2 taken 300453 times.
✓ Branch 3 taken 33835 times.
334288 for (; begin != end; ++begin) {
80
2/2
✓ Branch 2 taken 239307 times.
✓ Branch 3 taken 61146 times.
300453 if (CheckRanges(*begin)) {
81
4/4
✓ Branch 0 taken 1449 times.
✓ Branch 1 taken 237858 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 1425 times.
239307 if ((max_length_ >= 0) && (pos >= max_length_)) {
82 24 is_sane = false;
83 24 break;
84 }
85 239283 filtered_output->push_back(*begin);
86 239283 pos++;
87 } else {
88 61146 is_sane = false;
89 }
90 }
91 33859 return is_sane;
92 }
93
94
95 300453 bool InputSanitizer::CheckRanges(const char chr) const {
96
2/2
✓ Branch 1 taken 447897 times.
✓ Branch 2 taken 61146 times.
509043 for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
97
2/2
✓ Branch 2 taken 239307 times.
✓ Branch 3 taken 208590 times.
447897 if (valid_ranges_[j].InRange(chr)) {
98 239307 return true;
99 }
100 }
101 61146 return false;
102 }
103
104
105 144 string InputSanitizer::Filter(const std::string &input) const {
106 144 string filtered_output;
107
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 Sanitize(input, &filtered_output);
108 144 return filtered_output;
109 }
110
111
112 33727 bool InputSanitizer::IsValid(const std::string &input) const {
113 33727 string dummy;
114
1/2
✓ Branch 1 taken 33727 times.
✗ Branch 2 not taken.
67454 return Sanitize(input, &dummy);
115 33727 }
116
117
118 720 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 6 times.
✓ Branch 2 taken 714 times.
720 if (std::distance(begin, end) == 0) {
122 6 return false;
123 }
124
125
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 702 times.
714 if (*begin == '-') {
126 // minus is allowed as the first character!
127 12 filtered_output->push_back('-');
128 12 begin++;
129 }
130
131 714 return InputSanitizer::Sanitize(begin, end, filtered_output);
132 }
133
134
135 60 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 6 times.
✓ Branch 2 taken 54 times.
60 if (std::distance(begin, end) == 0) {
139 6 return false;
140 }
141
142 54 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