Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uninitialized scalar field #20

Open
Lecrapouille opened this issue Apr 16, 2019 · 2 comments
Open

Uninitialized scalar field #20

Lecrapouille opened this issue Apr 16, 2019 · 2 comments

Comments

@Lecrapouille
Copy link

Lecrapouille commented Apr 16, 2019

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.

@FlorianRappl
Copy link
Owner

Hi @Lecrapouille - great catch!

Do you mind making a PR? Would be much appreciated! Thanks 👍

@Lecrapouille
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants