CmdParser icon indicating copy to clipboard operation
CmdParser copied to clipboard

Uninitialized scalar field

Open Lecrapouille opened this issue 6 years ago • 2 comments

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.

Lecrapouille avatar Apr 16 '19 02:04 Lecrapouille

Hi @Lecrapouille - great catch!

Do you mind making a PR? Would be much appreciated! Thanks :+1:

FlorianRappl avatar Apr 16 '19 06:04 FlorianRappl

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:

  1. Is print_value() is supposed to return 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; 
		};

  1. Remove the member variable and do not check callback() return code
callback(args);
  1. Use local variable, get the result of callback() then void it:
T value = callback(args);
(void) value;
return true;

Lecrapouille avatar Apr 16 '19 17:04 Lecrapouille