Skip to content

Commit

Permalink
style and performance improvements reported by cppcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Connor committed May 9, 2021
1 parent 01834d4 commit 3cd9bdc
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 384 deletions.
3 changes: 1 addition & 2 deletions src/zm_fifo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ void Fifo::file_create_if_missing(
Debug(5, "Supposed to be a fifo pipe but isn't, unlinking: %s", path);
unlink(path);
}
int fd;
if (!is_fifo) {
Debug(5, "Creating non fifo file as requested: %s", path);
fd = ::open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
int fd = ::open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
::close(fd);
return;
}
Expand Down
13 changes: 7 additions & 6 deletions src/zm_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,7 +2381,7 @@ void Monitor::ReloadLinkedMonitors(const char *p_linked_monitors) {
} // end if p_linked_monitors
} // end void Monitor::ReloadLinkedMonitors(const char *p_linked_monitors)

std::vector<std::shared_ptr<Monitor>> Monitor::LoadMonitors(std::string &where, Purpose purpose) {
std::vector<std::shared_ptr<Monitor>> Monitor::LoadMonitors(const std::string &where, Purpose purpose) {
std::string sql = load_monitor_sql + " WHERE " + where;
Debug(1, "Loading Monitors with %s", sql.c_str());

Expand Down Expand Up @@ -2974,7 +2974,7 @@ bool Monitor::DumpSettings(char *output, bool verbose) {
function==MOCORD?"Continuous Record with Motion Detection":(
function==NODECT?"Externally Triggered only, no Motion Detection":"Unknown"
))))));
sprintf(output+strlen(output), "Zones : %lu\n", zones.size());
sprintf(output+strlen(output), "Zones : %zu\n", zones.size());
for (const Zone &zone : zones) {
zone.DumpSettings(output+strlen(output), verbose);
}
Expand Down Expand Up @@ -3145,21 +3145,22 @@ void Monitor::get_ref_image() {

std::vector<Group *> Monitor::Groups() {
// At the moment, only load groups once.
if ( !groups.size() ) {
if (!groups.size()) {
std::string sql = stringtf(
"SELECT `Id`, `ParentId`, `Name` FROM `Groups` WHERE `Groups.Id` IN "
"(SELECT `GroupId` FROM `Groups_Monitors` WHERE `MonitorId`=%d)", id);
MYSQL_RES *result = zmDbFetch(sql.c_str());
if ( !result ) {
if (!result) {
Error("Can't load groups: %s", mysql_error(&dbconn));
return groups;
}
int n_groups = mysql_num_rows(result);
Debug(1, "Got %d groups", n_groups);
while ( MYSQL_ROW dbrow = mysql_fetch_row(result) ) {
groups.reserve(n_groups);
while (MYSQL_ROW dbrow = mysql_fetch_row(result)) {
groups.push_back(new Group(dbrow));
}
if ( mysql_errno(&dbconn) ) {
if (mysql_errno(&dbconn)) {
Error("Can't fetch row: %s", mysql_error(&dbconn));
}
mysql_free_result(result);
Expand Down
2 changes: 1 addition & 1 deletion src/zm_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class Monitor {
std::vector<Group *> Groups();
StringVector GroupNames();

static std::vector<std::shared_ptr<Monitor>> LoadMonitors(std::string &sql, Purpose purpose); // Returns # of Monitors loaded, 0 on failure.
static std::vector<std::shared_ptr<Monitor>> LoadMonitors(const std::string &sql, Purpose purpose); // Returns # of Monitors loaded, 0 on failure.
#if ZM_HAS_V4L
static std::vector<std::shared_ptr<Monitor>> LoadLocalMonitors(const char *device, Purpose purpose);
#endif // ZM_HAS_V4L
Expand Down
2 changes: 1 addition & 1 deletion src/zm_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ AVFrame *ZMPacket::get_out_frame(int width, int height, AVPixelFormat format) {
codec_imgsize = avpicture_get_size(
format,
width,
>height);
height);
buffer = (uint8_t *)av_malloc(codec_imgsize);
avpicture_fill(
(AVPicture *)out_frame,
Expand Down
8 changes: 4 additions & 4 deletions src/zm_packetqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {

packetqueue_iterator it = pktQueue.begin();
packetqueue_iterator next_front = pktQueue.begin();
int video_packets_to_delete = 0; // This is a count of how many packets we will delete so we know when to stop looking

// First packet is special because we know it is a video keyframe and only need to check for lock
std::shared_ptr<ZMPacket> zm_packet = *it;
Expand All @@ -247,6 +246,7 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
Debug(1, "trying lock on first packet");
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
if (lp->trylock()) {
int video_packets_to_delete = 0; // This is a count of how many packets we will delete so we know when to stop looking
Debug(1, "Have lock on first packet");
++it;
delete lp;
Expand Down Expand Up @@ -282,7 +282,7 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
break;
}
}
it++;
++it;
} // end while
}
} // end if first packet not locked
Expand All @@ -292,7 +292,7 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
);
if (next_front != pktQueue.begin()) {
while (pktQueue.begin() != next_front) {
std::shared_ptr<ZMPacket> zm_packet = *pktQueue.begin();
zm_packet = *pktQueue.begin();
if (!zm_packet) {
Error("NULL zm_packet in queue");
continue;
Expand Down Expand Up @@ -478,10 +478,10 @@ packetqueue_iterator *PacketQueue::get_event_start_packet_it(
}
(*it)--;
}
packet = *(*it);
}
// it either points to beginning or we have seen pre_event_count video packets.

packet = *(*it);
if (pre_event_count) {
if (packet->image_index < (int)pre_event_count) {
// probably just starting up
Expand Down
2 changes: 1 addition & 1 deletion src/zm_rtsp_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ std::string Authenticator::quote( const std::string &src ) {
return ReplaceAll(ReplaceAll(src, "\\", "\\\\"), "\"", "\\\"");
}

std::string Authenticator::getAuthHeader(std::string method, std::string uri) {
std::string Authenticator::getAuthHeader(const std::string &method, const std::string &uri) {
std::string result = "Authorization: ";
if ( fAuthMethod == AUTH_BASIC ) {
result += "Basic " + Base64Encode(username() + ":" + password());
Expand Down
4 changes: 2 additions & 2 deletions src/zm_rtsp_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Authenticator {
AuthMethod auth_method() const { return fAuthMethod; }

std::string computeDigestResponse(const std::string &cmd, const std::string &url);
void authHandleHeader( std::string headerData );
std::string getAuthHeader( std::string method, std::string path );
void authHandleHeader(std::string headerData);
std::string getAuthHeader(const std::string &method, const std::string &path);
void checkAuthResponse(const std::string &response);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/zm_rtsp_server_device_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ ZoneMinderDeviceSource::ZoneMinderDeviceSource(
unsigned int queueSize
) :
FramedSource(env),
m_eventTriggerId(envir().taskScheduler().createEventTrigger(ZoneMinderDeviceSource::deliverFrameStub)),
m_stream(stream),
m_monitor(std::move(monitor)),
m_packetqueue(nullptr),
m_packetqueue_it(nullptr),
m_queueSize(queueSize)
{
m_eventTriggerId = envir().taskScheduler().createEventTrigger(ZoneMinderDeviceSource::deliverFrameStub);
memset(&m_thid, 0, sizeof(m_thid));
memset(&m_mutex, 0, sizeof(m_mutex));
if ( m_monitor ) {
Expand Down
2 changes: 1 addition & 1 deletion src/zm_rtsp_server_fifo_adts_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ADTS_ZoneMinderFifoSource::ADTS_ZoneMinderFifoSource(
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
)
:
ZoneMinderFifoAudioSource(rtspServer, sessionId, channelId, fifo)
Expand Down
2 changes: 1 addition & 1 deletion src/zm_rtsp_server_fifo_adts_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ADTS_ZoneMinderFifoSource : public ZoneMinderFifoAudioSource {
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
);

virtual ~ADTS_ZoneMinderFifoSource() {}
Expand Down
10 changes: 5 additions & 5 deletions src/zm_rtsp_server_fifo_h264_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ H264_ZoneMinderFifoSource::H264_ZoneMinderFifoSource(
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
)
: H26X_ZoneMinderFifoSource(rtspServer, sessionId, channelId, fifo)
{
Expand Down Expand Up @@ -96,7 +96,7 @@ H265_ZoneMinderFifoSource::H265_ZoneMinderFifoSource(
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
)
: H26X_ZoneMinderFifoSource(rtspServer, sessionId, channelId, fifo)
{
Expand Down Expand Up @@ -199,12 +199,12 @@ unsigned char* H26X_ZoneMinderFifoSource::extractFrame(unsigned char* frame, si
Debug(4, "ExtractFrame: %p %zu", frame, size);
outsize = 0;
size_t markerLength = 0;
size_t endMarkerLength = 0;
m_frameType = 0;
unsigned char *startFrame = nullptr;
if ( size >= 3 )
if (size >= 3)
startFrame = this->findMarker(frame, size, markerLength);
if ( startFrame != nullptr ) {
if (startFrame != nullptr) {
size_t endMarkerLength = 0;
Debug(4, "startFrame: %p marker Length %zu", startFrame, markerLength);
m_frameType = startFrame[markerLength];

Expand Down
4 changes: 2 additions & 2 deletions src/zm_rtsp_server_fifo_h264_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class H264_ZoneMinderFifoSource : public H26X_ZoneMinderFifoSource {
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
);

// overide ZoneMinderFifoSource
Expand All @@ -63,7 +63,7 @@ class H265_ZoneMinderFifoSource : public H26X_ZoneMinderFifoSource {
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
);

// overide ZoneMinderFifoSource
Expand Down
4 changes: 2 additions & 2 deletions src/zm_rtsp_server_fifo_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ZoneMinderFifoSource::ZoneMinderFifoSource(
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
) :
stop_(false),
m_rtspServer(rtspServer),
Expand Down Expand Up @@ -225,7 +225,7 @@ int ZoneMinderFifoSource::getNextFrame() {
if (bytes_needed > 0) {
Debug(4, "Need another %d bytes. Trying to read them", bytes_needed);
while (bytes_needed) {
int bytes_read = m_buffer.read_into(m_fd, bytes_needed);
bytes_read = m_buffer.read_into(m_fd, bytes_needed);
if (bytes_read <= 0) {
Debug(1, "Failed to read another %d bytes, got %d.", bytes_needed, bytes_read);
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/zm_rtsp_server_fifo_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ZoneMinderFifoSource {
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
);
virtual ~ZoneMinderFifoSource();

Expand Down
6 changes: 4 additions & 2 deletions src/zm_rtsp_server_fifo_video_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ ZoneMinderFifoVideoSource::ZoneMinderFifoVideoSource(
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
) :
ZoneMinderFifoSource(rtspServer, sessionId, channelId, fifo)
ZoneMinderFifoSource(rtspServer, sessionId, channelId, fifo),
m_width(0),
m_height(0)
{
m_timeBase = {1, 90000};
}
Expand Down
2 changes: 1 addition & 1 deletion src/zm_rtsp_server_fifo_video_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ZoneMinderFifoVideoSource: public ZoneMinderFifoSource {
std::shared_ptr<xop::RtspServer>& rtspServer,
xop::MediaSessionId sessionId,
xop::MediaChannelId channelId,
std::string fifo
const std::string &fifo
);
protected:
void PushFrame(const uint8_t *data, size_t size, int64_t pts) override;
Expand Down
Loading

0 comments on commit 3cd9bdc

Please sign in to comment.