diff --git a/ChangeLog b/ChangeLog index a38a8aab26..04ce6042de 100644 --- a/ChangeLog +++ b/ChangeLog @@ -61,6 +61,7 @@ Enhancements: - #7412 Reduce GUI compile time by building a GUI library - #7413 Improve UI design and reduce over-use of `#ifdef` - #7414 Expand BSD sockets poll tests and remove legacy-poll code +- #7415 Assert arg value `num > 0` for BSD socket poll unblock pipe # 1.14.6 diff --git a/src/lib/arch/unix/ArchNetworkBSD.cpp b/src/lib/arch/unix/ArchNetworkBSD.cpp index 5465bbeef0..e69ae05dc4 100644 --- a/src/lib/arch/unix/ArchNetworkBSD.cpp +++ b/src/lib/arch/unix/ArchNetworkBSD.cpp @@ -269,7 +269,7 @@ bool ArchNetworkBSD::connectSocket(ArchSocket s, ArchNetAddress addr) { } int ArchNetworkBSD::pollSocket(PollEntry pe[], int num, double timeout) { - assert(pe != NULL || num == 0); + assert((pe != nullptr && num > 0) || num == 0); // return if nothing to do if (num == 0) { @@ -299,9 +299,7 @@ int ArchNetworkBSD::pollSocket(PollEntry pe[], int num, double timeout) { // add the unblock pipe const int *unblockPipe = getUnblockPipe(); if (unblockPipe != nullptr) { - assert(n < (1 + num)); - pfd[n].fd = unblockPipe[0]; - + pfd[n].fd = unblockPipe[0]; // test pfd[n].events = POLLIN; ++n; } diff --git a/src/test/unittests/arch/unix/ArchNetworkBSDTests.cpp b/src/test/unittests/arch/unix/ArchNetworkBSDTests.cpp index 3145479c01..b9796f6820 100644 --- a/src/test/unittests/arch/unix/ArchNetworkBSDTests.cpp +++ b/src/test/unittests/arch/unix/ArchNetworkBSDTests.cpp @@ -48,6 +48,19 @@ struct MockDeps : public ArchNetworkBSD::Deps { std::shared_ptr m_pollFD; }; +#ifndef NDEBUG + +TEST(ArchNetworkBSDTests, pollSocket_negativeNum_death) { + MockDeps deps; + ArchNetworkBSD networkBSD(deps); + + PollEntries entries{{nullptr, 0, 0}}; + + EXPECT_DEATH({ networkBSD.pollSocket(entries.data(), -1, 1); }, "num > 0"); +} + +#endif // DEBUG + TEST(ArchNetworkBSDTests, pollSocket_zeroEntries_callsSleep) { MockDeps deps; ArchNetworkBSD networkBSD(deps);