Skip to content

Commit be03d47

Browse files
committed
Library: Added <valid> element that is used to define valid input values for functions
1 parent 85c62f9 commit be03d47

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

lib/library.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ bool Library::load(const tinyxml2::XMLDocument &doc)
112112
bool notuninit = false;
113113
bool formatstr = false;
114114
bool strz = false;
115+
std::string valid;
115116
for (const tinyxml2::XMLElement *argnode = functionnode->FirstChildElement(); argnode; argnode = argnode->NextSiblingElement()) {
116117
if (strcmp(argnode->Name(), "not-null") == 0)
117118
notnull = true;
@@ -121,13 +122,33 @@ bool Library::load(const tinyxml2::XMLDocument &doc)
121122
formatstr = true;
122123
else if (strcmp(argnode->Name(), "strz") == 0)
123124
strz = true;
125+
else if (strcmp(argnode->Name(), "valid") == 0) {
126+
// Validate the validation expression
127+
const char *p = argnode->GetText();
128+
if (!std::isdigit(*p))
129+
return false;
130+
for (; *p; p++) {
131+
if (std::isdigit(*p))
132+
continue;
133+
if (*p == '-' && std::isdigit(*(p-1)))
134+
continue;
135+
if (*p == ',' && *(p+1) != ',')
136+
continue;
137+
return false;
138+
}
139+
140+
// Set validation expression
141+
valid = argnode->GetText();
142+
}
143+
124144
else
125145
return false;
126146
}
127-
argumentChecks[name][nr].notnull = notnull;
147+
argumentChecks[name][nr].notnull = notnull;
128148
argumentChecks[name][nr].notuninit = notuninit;
129149
argumentChecks[name][nr].formatstr = formatstr;
130-
argumentChecks[name][nr].strz = strz;
150+
argumentChecks[name][nr].strz = strz;
151+
argumentChecks[name][nr].valid = valid;
131152
} else if (strcmp(functionnode->Name(), "ignorefunction") == 0) {
132153
_ignorefunction[name] = (strcmp(functionnode->GetText(), "true") == 0);
133154
} else

lib/library.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ class CPPCHECKLIB Library {
9696
notnull = notuninit = formatstr = strz = false;
9797
}
9898

99-
bool notnull;
100-
bool notuninit;
101-
bool formatstr;
102-
bool strz;
99+
bool notnull;
100+
bool notuninit;
101+
bool formatstr;
102+
bool strz;
103+
std::string valid;
103104
};
104105

105106
// function name, argument nr => argument data

test/testlibrary.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class TestLibrary : public TestFixture {
7979
" <arg nr=\"4\">\n"
8080
" <strz/>\n"
8181
" </arg>\n"
82+
" <arg nr=\"5\">\n"
83+
" <valid>1-</valid>\n"
84+
" </arg>\n"
8285
" </function>\n"
8386
"</def>";
8487
tinyxml2::XMLDocument doc;
@@ -90,6 +93,7 @@ class TestLibrary : public TestFixture {
9093
ASSERT_EQUALS(true, library.argumentChecks["foo"][2].notnull);
9194
ASSERT_EQUALS(true, library.argumentChecks["foo"][3].formatstr);
9295
ASSERT_EQUALS(true, library.argumentChecks["foo"][4].strz);
96+
ASSERT_EQUALS("1-", library.argumentChecks["foo"][5].valid);
9397
}
9498

9599
void memory() {

0 commit comments

Comments
 (0)