Skip to content

Commit

Permalink
Merge pull request networkupstools#2434 from jimklimov/issue-2344
Browse files Browse the repository at this point in the history
Refine nutconf-related code about exceptional situations
  • Loading branch information
jimklimov authored May 7, 2024
2 parents 050e101 + 9adf60a commit 5f9645f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
4 changes: 4 additions & 0 deletions common/nutwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ static std::string serializeCertHost(const nut::CertHost & certHost) {
// need to add relaxed mode for 0/1 as false/true handling:
//bi.bool01 = true;

// Avoid static analysis concerns that the internal _value
// "may be used uninitialized in this function" (ETOOSMART):
bi = false;

// Assumed to be set() - exception otherwise
bi = certHost.certVerify;
bi.bool01 = true;
Expand Down
78 changes: 71 additions & 7 deletions include/nutconf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class Settable
protected:
Type _value;
bool _set;
std::string errMsg_ENOTSET()const {
static const std::string msg =
"Can not retrieve a Settable value of "
"an instance that was not assigned yet "
"(or was last known cleared)";
return msg;
}

public:
Settable():_set(false){}
Settable(const Settable<Type>& val):_value(val._value), _set(val._set){}
Expand All @@ -76,11 +84,43 @@ class Settable
bool set()const{return _set;}
void clear(){_set = false;}

operator const Type&()const{return _value;}
operator Type&(){return _value;}
operator const Type&()const
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (!set())
throw std::invalid_argument(errMsg_ENOTSET());
return _value;
}
operator Type&()
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (!set())
throw std::invalid_argument(errMsg_ENOTSET());
return _value;
}

const Type& operator *()const{return _value;}
Type& operator *(){return _value;}
const Type& operator *()const
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (!set())
throw std::invalid_argument(errMsg_ENOTSET());
return _value;
}
Type& operator *()
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (!set())
throw std::invalid_argument(errMsg_ENOTSET());
return _value;
}

Settable<Type>& operator=(const Type& val){_value = val; _set = true; return *this;}

Expand Down Expand Up @@ -256,6 +296,9 @@ class BoolInt
}

inline BoolInt& operator=(const char* s)
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (!s)
throw std::invalid_argument(
Expand All @@ -266,6 +309,9 @@ class BoolInt
}

inline BoolInt& operator=(std::string src)
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
static const Settable<bool> b0(false);
static const Settable<bool> b1(true);
Expand Down Expand Up @@ -406,6 +452,9 @@ class BoolInt
}

inline bool set()const
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (i.set() && b.set())
throw std::invalid_argument(
Expand All @@ -414,7 +463,11 @@ class BoolInt
return (i.set() || b.set());
}

operator int() {
operator int()
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (i.set()) return i;
if (bool01.set() && bool01 == true) {
if (b.set()) {
Expand All @@ -433,7 +486,11 @@ class BoolInt
"BoolInt value not set, neither to bool nor to int");
}

operator bool() {
operator bool()
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (b.set()) return b;
if (bool01.set() && bool01 == true) {
if (i.set()) {
Expand All @@ -449,7 +506,11 @@ class BoolInt
"BoolInt value not set, neither to bool nor to int");
}

inline std::string toString()const {
inline std::string toString()const
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
if (b.set()) {
if (b == true) return "yes";
return "no";
Expand Down Expand Up @@ -1323,6 +1384,9 @@ class GenericConfiguration : public BaseConfiguration, public Serialisable
const std::string & section,
const std::string & entry,
nut::BoolInt val = true)
#if (defined __cplusplus) && (__cplusplus < 201100)
throw(std::invalid_argument)
#endif
{
setStr(section, entry, val);
}
Expand Down

0 comments on commit 5f9645f

Please sign in to comment.