Files
zoneminder/tests/zm_monitorstream.cpp
Steve Gilvarry dc4cf32a63 fix: guard zms buffer_level against zero buffer count refs #4936
MonitorStream::processCommand computed buffer_level by dividing by
temp_image_buffer_count (and via MOD_ADD modulo) while only guarding on
playback_buffer. processCommand runs on the command_processor thread,
started in runStream before temp_image_buffer_count is assigned from
playback_buffer. In that window temp_image_buffer_count is still 0 while
playback_buffer is already > 0, so a command arriving then divided by
zero and raised SIGFPE, crashing nph-zms.

Extract the percentage math into MonitorStreamBufferLevel which returns 0
when the buffer count is not positive, and add Catch2 coverage for the
zero-count and wrap-around cases.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 08:21:29 +10:00

54 lines
2.0 KiB
C++

/*
* This file is part of the ZoneMinder Project. See AUTHORS file for contributors.
*
* 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_monitorstream.h"
TEST_CASE("MonitorStreamBufferLevel") {
SECTION("zero buffer count does not divide by zero") {
// Regression for zoneminder/zoneminder#4936: processCommand() could run on
// the command thread before runStream() assigned temp_image_buffer_count,
// leaving it 0 while playback_buffer was already > 0, causing a SIGFPE.
REQUIRE(MonitorStreamBufferLevel(0, 0, 0) == 0);
REQUIRE(MonitorStreamBufferLevel(5, 2, 0) == 0);
REQUIRE(MonitorStreamBufferLevel(0, 5, 0) == 0);
}
SECTION("negative buffer count is treated as empty") {
REQUIRE(MonitorStreamBufferLevel(3, 1, -10) == 0);
}
SECTION("empty buffer reports 0%") {
REQUIRE(MonitorStreamBufferLevel(0, 0, 100) == 0);
}
SECTION("full buffer reports nearly 100%") {
// write wraps around to one behind read => count-1 of count slots used
REQUIRE(MonitorStreamBufferLevel(99, 0, 100) == 99);
}
SECTION("half full buffer reports ~50%") {
REQUIRE(MonitorStreamBufferLevel(50, 0, 100) == 50);
}
SECTION("wrap-around (write behind read) is handled modularly") {
// write_index - read_index is negative; modular arithmetic keeps it in range
REQUIRE(MonitorStreamBufferLevel(2, 7, 10) == 50);
}
}