[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Cleaning up C++ input validation of strings
From: |
Rik |
Subject: |
Cleaning up C++ input validation of strings |
Date: |
Fri, 13 Feb 2015 17:09:54 -0800 |
2/13/15
There seem to be multiple instances where we check whether an input is a
string, then extract a string_value, and then check the error_status to
make sure that the string_value was extracted correctly. This seems
redundant and possibly a lazy copying of a correct pattern.
If something might not be of type A, but possibly could be converted to
type A, then asking for A_value () and checking the error_status is the
correct thing to do. In this case, If something is determined to be a
string, can the string_value() call really fail?
Here is an example from lu.cc
-- Code --
if (args(n).is_string ())
{
std::string tmp = args(n++).string_value ();
if (! error_state)
{
if (tmp.compare ("vector") == 0)
vecout = true;
else
error ("lu: unrecognized string argument");
}
}
-- End Code --
I propose simplifying to
-- Code --
if (args(n).is_string ())
{
std::string tmp = args(n++).string_value ();
if (tmp.compare ("vector") == 0)
vecout = true;
else
error ("lu: unrecognized string argument");
}
-- End Code --
Are there any objections?
--Rik
- Cleaning up C++ input validation of strings,
Rik <=