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

[tests] util_tests.cpp: actually check ignored args #11997

Merged
merged 1 commit into from Jan 4, 2018

Conversation

ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Dec 23, 2017

An array with 7 elements was setup for checking argument parsing, but
was passed to ParseParamaeters with argc=5, meaning the interpretation
of the last two arguments was never actually checked.

Copy link
Member

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 4e196a9.

@@ -121,7 +121,8 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
testArgs.ParseParameters(1, (char**)argv_test);
BOOST_CHECK(testArgs.GetMapArgs().empty() && testArgs.GetMapMultiArgs().empty());

testArgs.ParseParameters(5, (char**)argv_test);
static_assert(7 * sizeof(argv_test[0]) == sizeof(argv_test), "argv_test length has changed");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO remove this assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert doesn't have any runtime cost, and documents/enforces the change as well as helping avoid the same mistake happening again, so seems better to have it to me?

An alternative approach is using templates to directly determine the size of the array:

template<size_t N>
inline void ParseParameters(int argc, const char* (&argv)[N])
{
    assert(argc >= 0 && (size_t) argc < N);
    ArgsManager::ParseParameters(argc, (char**) argv);
}
template<size_t N>
inline void ParseParameters(const char* (&argv)[N])
{
    ArgsManager::ParseParameters(N, (char**) argv);
}
...
testArgs.ParseParameters(1, argv_test); // to test only some args
testArgs.ParseParameters(argv_test); // to test all args

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since passing an argc that does not specify the size of argv is only ever used in tests, you could pass argv_test.size() instead of 7 by switching to const std::array<const char*, 7> argv_test = {... braced-init-list ...}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes it robust to future changes, but not robust to future changes that remove the trailing or the two trailing args. Imo such buggy changes must be covered by code review. Not excessive documentation. Note that we are talking about a function that spans about 20 lines... If code review does not work on that scale we have far worse issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@maflcko
Copy link
Member

maflcko commented Jan 3, 2018

Concept ACK, but agree with @promag

An array with 7 elements was setup for checking argument parsing, but
was passed to ParseParamaeters with argc=5, meaning the interpretation
of the last two arguments was never actually checked.
@maflcko
Copy link
Member

maflcko commented Jan 4, 2018

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

utACK c99a3c32c830384a9959a468a13441fcd2e48a72
-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJaTjV7AAoJENLqSFDnUoslYJUP+wRtblQkTEa0UlmhOiDk4KFa
BPiYWVsYCIfS2Mt7OtQq0RlxcGnNydneulsVQHzOhzTMo0DQdvKS32W5sUzhCjUa
JBc3McQ1kprl0NoiHoD3VuR2YbSdBPs8iwue14oD1ilNjGO3P6SlJdiv6Cg5svN0
FdpTEsJFkaZqIbceG4kfOlHnxqMhfUuaXoXCcN4KEPVG+FqUCE5fyxZAGlGEqwjW
WqkmcPK2caRXovv9DKTKHjPNp9KjGIR9zkKTd+upLMkormW9A5yCD9fyJCwjTBD2
eHc34VjjiRS043V3Q8bZpxMVGxZoPCv5ZZPol1whQY0fmbX3mTiDelp+IlCHX50L
LQ4x/zhQ18+EjP4tbYQYyT7TrHljt8v6ai14+zqjTXyfZDN2ba75SlfTzO6qxi9Z
Iw6eEIDK5HxnMmtCXLxNNV9eIjiaH9lnNzt0S177MRacDn2KClZcsDOEodXCqy99
OXvL4jYYZY/d86kLZskWfpYYNRoFf67s6ybYLj6coS5hMeRFBDJrmng0AcMohib6
+ixWmA8d4kXjZGgepRMkjMQtLaxvc0Sa0lAzAJ8io3jkB+0wbL9aap7Qb/hrybcu
/h7psyPdUyRTbOxuf3+7ke/X9xmY51vwshxbq3aGOPSidMbnPX2Tjo4szjAw7je2
oabzFOrUMgtIzDt4H+57
=kwru
-----END PGP SIGNATURE-----

@maflcko maflcko merged commit c99a3c3 into bitcoin:master Jan 4, 2018
maflcko pushed a commit that referenced this pull request Jan 4, 2018
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jul 25, 2019
Summary:
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428

Backport of Core PR11997:
bitcoin/bitcoin#11997

Test Plan:
  make check

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3707
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 23, 2020
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 28, 2020
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 29, 2020
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 31, 2020
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Mar 31, 2020
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 1, 2020
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Jun 30, 2021
c99a3c3 [tests] util_tests.cpp: actually check ignored args (Anthony Towns)

Pull request description:

  An array with 7 elements was setup for checking argument parsing, but
  was passed to ParseParamaeters with argc=5, meaning the interpretation
  of the last two arguments was never actually checked.

Tree-SHA512: 7b81fde49742e524f1bb67e2ec084f5909ae36125f237f0210df4587c62e5a5a8f277f13543f0a85ad145c4bb80d62339a7d50d7ed41659df318c8198ea7f428
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants