Files
zoneminder/tests/zm_packet_lock.cpp
Nic BoetandClaude Opus 5.5 4eca3d7ee4 fix: don't let a failed ZMPacket::trylock overwrite the lock flag
ZMPacket::trylock() stored the result of try_lock() in the shared
`locked` member and returned that member. A failed attempt wrote false
while another thread held the mutex, and the thread that had just
taken the lock could return the other thread's false. ThreadSanitizer
reports the write (zm_packet.cpp:397) against unlock() (:404).

Return the local result and set `locked` only on success. The member is
read in Debug lines without the mutex, so make it std::atomic<bool>.

fixes #5248

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 11:45:37 -05:00

53 lines
1.6 KiB
C++

/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_packet.h"
#include <mutex>
#include <thread>
TEST_CASE("ZMPacket::trylock") {
ZMPacket packet;
SECTION("a failed try_lock leaves the holder's state alone") {
std::unique_lock<std::mutex> holder(packet.mutex_, std::defer_lock);
packet.lock(holder);
REQUIRE(packet.locked);
bool got = true;
std::thread other([&packet, &got]() {
std::unique_lock<std::mutex> lck(packet.mutex_, std::defer_lock);
got = packet.trylock(lck);
});
other.join();
REQUIRE_FALSE(got);
REQUIRE(packet.locked);
packet.unlock(holder);
REQUIRE_FALSE(packet.locked);
}
SECTION("a successful try_lock reports it from its own result") {
std::unique_lock<std::mutex> lck(packet.mutex_, std::defer_lock);
REQUIRE(packet.trylock(lck));
REQUIRE(lck.owns_lock());
packet.unlock(lck);
}
}