CmdParser
CmdParser copied to clipboard
Uninitialized scalar field
Minor bug concerning class CmdFunction final : public CmdBase
Coverity scan says line 100:
CID 281693 (#1 of 1): Uninitialized scalar field (UNINIT_CTOR)
2. uninit_member: Non-static class member value is not initialized in this constructor nor in any functions that it calls.
and line 118:
1. member_decl: Class member declaration for value.
but seems to me this member variable is set but never used.
Hi @Lecrapouille - great catch!
Do you mind making a PR? Would be much appreciated! Thanks :+1:
How do you want I fix it ? I do not know well this library and therefore I'm not sure to clearly understand what the code does. They are several solutions:
- Is
print_value()is supposed toreturn stringify(value);instead of return "" ? And what initial value shall I set to value ?
template<typename T>
class CmdFunction final : public CmdBase {
public:
explicit CmdFunction(const std::string& name, const std::string& alternative, const std::string& description, bool required, bool dominant) :
CmdBase(name, alternative, description, required, dominant, ArgumentCountChecker<T>::Variadic) {
}
virtual bool parse(std::ostream& output, std::ostream& error) {
try {
CallbackArgs args { arguments, output, error };
value = callback(args);
emptyValue_ = false; // FIX HERE ?
return true;
} catch (...) {
return false;
}
}
virtual std::string print_value() const {
return emptyValue_ ? "" : stringify(value); // FIX HERE ?
}
std::function<T(CallbackArgs&)> callback;
T value;
private: // FIX HERE ?
bool emptyValue_ = true;
};
- Remove the member variable and do not check callback() return code
callback(args);
- Use local variable, get the result of callback() then void it:
T value = callback(args);
(void) value;
return true;