From 1257a7ea374c81a78bbeda13e2db6c1e2d7d5e20 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 1 Nov 2020 16:11:19 -0500 Subject: [PATCH] improvements reported by cppcheck --- src/zm_comms.h | 6 +++--- src/zm_config.h | 5 +++-- src/zm_coord.h | 12 ++++++------ src/zm_db.h | 2 +- src/zm_event.cpp | 4 +++- src/zm_event.h | 8 ++++---- src/zm_exception.h | 32 +++++++++++++------------------- src/zm_fifo.h | 13 ++++++++++--- src/zm_local_camera.h | 20 ++++++++++---------- src/zm_logger.h | 4 ++-- src/zm_mem_utils.h | 4 ++-- src/zm_monitor.h | 12 ++++++------ src/zm_packetqueue.h | 2 +- src/zm_time.h | 2 ++ src/zm_user.h | 2 +- src/zmu.cpp | 30 +++++++++++++++--------------- 16 files changed, 82 insertions(+), 76 deletions(-) diff --git a/src/zm_comms.h b/src/zm_comms.h index 46face33c..a5af97dd5 100644 --- a/src/zm_comms.h +++ b/src/zm_comms.h @@ -49,7 +49,7 @@ protected: const int &mWd; protected: - CommsBase( int &rd, int &wd ) : mRd( rd ), mWd( wd ) { + CommsBase(const int &rd, const int &wd) : mRd(rd), mWd(wd) { } virtual ~CommsBase() { } @@ -62,10 +62,10 @@ public: public: int getReadDesc() const { - return( mRd ); + return mRd; } int getWriteDesc() const { - return( mWd ); + return mWd; } int getMaxDesc() const { return( mRd>mWd?mRd:mWd ); diff --git a/src/zm_config.h b/src/zm_config.h index d348f38e9..687ac430c 100644 --- a/src/zm_config.h +++ b/src/zm_config.h @@ -112,8 +112,9 @@ public: double DecimalValue() const; const char *StringValue() const; - ConfigItem &operator=(const ConfigItem item) { - Copy(item);return *this; + ConfigItem &operator=(const ConfigItem &item) { + Copy(item); + return *this; } inline operator bool() const { return BooleanValue(); diff --git a/src/zm_coord.h b/src/zm_coord.h index c6234fb1c..bb49c2f8b 100644 --- a/src/zm_coord.h +++ b/src/zm_coord.h @@ -48,12 +48,12 @@ public: return( result ); } - inline bool operator==( const Coord &coord ) { return( x == coord.x && y == coord.y ); } - inline bool operator!=( const Coord &coord ) { return( x != coord.x || y != coord.y ); } - inline bool operator>( const Coord &coord ) { return( x > coord.x && y > coord.y ); } - inline bool operator>=( const Coord &coord ) { return( !(operator<(coord)) ); } - inline bool operator<( const Coord &coord ) { return( x < coord.x && y < coord.y ); } - inline bool operator<=( const Coord &coord ) { return( !(operator>(coord)) ); } + inline bool operator==( const Coord &coord ) const { return( x == coord.x && y == coord.y ); } + inline bool operator!=( const Coord &coord ) const { return( x != coord.x || y != coord.y ); } + inline bool operator>( const Coord &coord ) const { return( x > coord.x && y > coord.y ); } + inline bool operator>=( const Coord &coord ) const { return( !(operator<(coord)) ); } + inline bool operator<( const Coord &coord ) const { return( x < coord.x && y < coord.y ); } + inline bool operator<=( const Coord &coord ) const { return( !(operator>(coord)) ); } inline Coord &operator+=( const Coord &coord ) { x += coord.x; y += coord.y; return( *this ); } inline Coord &operator-=( const Coord &coord ) { x -= coord.x; y -= coord.y; return( *this ); } diff --git a/src/zm_db.h b/src/zm_db.h index 11fc9faaa..f9f168cd0 100644 --- a/src/zm_db.h +++ b/src/zm_db.h @@ -28,7 +28,7 @@ class zmDbRow { MYSQL_RES *result_set; MYSQL_ROW row; public: - zmDbRow() { result_set = nullptr; row = nullptr; }; + zmDbRow() : result_set(nullptr), row(nullptr) { }; MYSQL_RES *fetch( const char *query ); zmDbRow( MYSQL_RES *, MYSQL_ROW *row ); ~zmDbRow(); diff --git a/src/zm_event.cpp b/src/zm_event.cpp index b201f5c86..a342d91a7 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -571,6 +571,7 @@ void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, str void Event::WriteDbFrames() { char *frame_insert_values_ptr = (char *)&frame_insert_sql + 90; // 90 == strlen(frame_insert_sql); + Debug(1, "Inserting %d frames", frame_data.size()); while ( frame_data.size() ) { Frame *frame = frame_data.front(); frame_data.pop(); @@ -586,8 +587,9 @@ void Event::WriteDbFrames() { frame->score); delete frame; } - *(frame_insert_values_ptr-1) = '\0'; // The -2 is for the extra , added for values above + *(frame_insert_values_ptr-1) = '\0'; // The -1 is for the extra , added for values above db_mutex.lock(); + Debug(1, "SQL: %s", frame_insert_sql); int rc = mysql_query(&dbconn, frame_insert_sql); db_mutex.unlock(); diff --git a/src/zm_event.h b/src/zm_event.h index 8c44af1f2..ff709e5c6 100644 --- a/src/zm_event.h +++ b/src/zm_event.h @@ -113,14 +113,14 @@ class Event { ~Event(); uint64_t Id() const { return id; } - const std::string &Cause() { return cause; } + const std::string &Cause() const { return cause; } int Frames() const { return frames; } int AlarmFrames() const { return alarm_frames; } const struct timeval &StartTime() const { return start_time; } const struct timeval &EndTime() const { return end_time; } - struct timeval &StartTime() { return start_time; } - struct timeval &EndTime() { return end_time; } + struct timeval &StartTime() const { return start_time; } + struct timeval &EndTime() const { return end_time; } bool SendFrameImage( const Image *image, bool alarm_frame=false ); bool WriteFrameImage( Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame=false ); @@ -146,7 +146,7 @@ class Event { return Event::getSubPath( localtime( time ) ); } - const char* getEventFile(void) { + const char* getEventFile(void) const { return video_file.c_str(); } diff --git a/src/zm_exception.h b/src/zm_exception.h index a02653b88..1bceeed8a 100644 --- a/src/zm_exception.h +++ b/src/zm_exception.h @@ -22,8 +22,7 @@ #include -class Exception -{ +class Exception { protected: typedef enum { INFO, WARNING, ERROR, FATAL } Severity; @@ -32,33 +31,28 @@ protected: Severity mSeverity; public: - Exception( const std::string &message, Severity severity=ERROR ) : mMessage( message ), mSeverity( severity ) + explicit Exception(const std::string &message, const Severity severity=ERROR) : + mMessage(message), + mSeverity(severity) { } -public: - const std::string &getMessage() const - { - return( mMessage ); + const std::string &getMessage() const { + return mMessage; } - Severity getSeverity() const - { - return( mSeverity ); + Severity getSeverity() const { + return mSeverity; } - bool isInfo() const - { - return( mSeverity == INFO ); + bool isInfo() const { + return mSeverity == INFO; } - bool isWarning() const - { + bool isWarning() const { return( mSeverity == WARNING ); } - bool isError() const - { + bool isError() const { return( mSeverity == ERROR ); } - bool isFatal() const - { + bool isFatal() const { return( mSeverity == FATAL ); } }; diff --git a/src/zm_fifo.h b/src/zm_fifo.h index 065fd569c..abd666891 100644 --- a/src/zm_fifo.h +++ b/src/zm_fifo.h @@ -68,19 +68,26 @@ class FifoStream : public StreamBase { ); protected: - typedef enum { MJPEG, RAW } StreamType; + typedef enum { UNKNOWN, MJPEG, RAW } StreamType; StreamType stream_type; bool sendMJEGFrames(); bool sendRAWFrames(); void processCommand(const CmdMsg *msg) {} public: - FifoStream() {} + FifoStream() : + stream_path(nullptr), + fd(0), + total_read(0), + bytes_read(0), + frame_count(0), + stream_type(UNKNOWN) + {} static void fifo_create_if_missing( const char * path, bool delete_fake_fifo = true); void setStreamStart(const char * path); void setStreamStart(int monitor_id, const char * format); - void runStream(); + void runStream() override; }; #endif // ZM_FIFO_H diff --git a/src/zm_local_camera.h b/src/zm_local_camera.h index d06631c23..f4fb8bd36 100644 --- a/src/zm_local_camera.h +++ b/src/zm_local_camera.h @@ -153,17 +153,17 @@ public: int Palette() const { return( palette ); } int Extras() const { return( extras ); } - int Brightness( int p_brightness=-1 ); - int Hue( int p_hue=-1 ); - int Colour( int p_colour=-1 ); - int Contrast( int p_contrast=-1 ); + int Brightness( int p_brightness=-1 ) override; + int Hue( int p_hue=-1 ) override; + int Colour( int p_colour=-1 ) override; + int Contrast( int p_contrast=-1 ) override; - int PrimeCapture(); - int PreCapture(); - int Capture( Image &image ); - int PostCapture(); - int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) {return(0);}; - int Close() { return 0; }; + int PrimeCapture()override ; + int PreCapture()override ; + int Capture( Image &image )override ; + int PostCapture()override ; + int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) override {return(0);}; + int Close() override { return 0; }; static bool GetCurrentSettings( const char *device, char *output, int version, bool verbose ); }; diff --git a/src/zm_logger.h b/src/zm_logger.h index 82144a1c9..ad1ac398b 100644 --- a/src/zm_logger.h +++ b/src/zm_logger.h @@ -121,7 +121,7 @@ private: Logger(); ~Logger(); - int limit(int level) { + int limit(const int level) const { if ( level > DEBUG9 ) return DEBUG9; if ( level < NOLOG ) @@ -163,7 +163,7 @@ public: } Level level(Level=NOOPT); - bool debugOn() { + bool debugOn() const { return mEffectiveLevel >= DEBUG1; } diff --git a/src/zm_mem_utils.h b/src/zm_mem_utils.h index 6ef1bdac5..a9e1ab097 100644 --- a/src/zm_mem_utils.h +++ b/src/zm_mem_utils.h @@ -26,7 +26,7 @@ inline void* zm_mallocaligned(unsigned int reqalignment, size_t reqsize) { uint8_t* retptr; #if HAVE_POSIX_MEMALIGN - if ( posix_memalign((void**)&retptr,reqalignment,reqsize) != 0 ) + if ( posix_memalign((void**)&retptr, reqalignment, reqsize) != 0 ) return nullptr; return retptr; @@ -39,7 +39,7 @@ inline void* zm_mallocaligned(unsigned int reqalignment, size_t reqsize) { alloc = retptr + sizeof(void*); - if(((long)alloc % reqalignment) != 0) + if ( ((long)alloc % reqalignment) != 0 ) alloc = alloc + (reqalignment - ((long)alloc % reqalignment)); /* Store a pointer before to the start of the block, just before returned aligned memory */ diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 4b6ec5088..a6d5084bb 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -444,7 +444,7 @@ public: inline Function GetFunction() const { return( function ); } - inline bool Enabled() { + inline bool Enabled() const { if ( function <= MONITOR ) return false; return enabled; @@ -452,17 +452,17 @@ public: inline const char *EventPrefix() const { return event_prefix; } - inline bool Ready() { + inline bool Ready() const { if ( function <= MONITOR ) return false; return( image_count > ready_count ); } - inline bool Active() { + inline bool Active() const { if ( function <= MONITOR ) return false; return( enabled && shared_data->active ); } - inline bool Exif() { + inline bool Exif() const { return embed_exif; } Orientation getOrientation() const; @@ -479,7 +479,7 @@ public: uint64_t GetVideoWriterEventId() const { return video_store_data->current_event; } void SetVideoWriterEventId( unsigned long long p_event_id ) { video_store_data->current_event = p_event_id; } struct timeval GetVideoWriterStartTime() const { return video_store_data->recording; } - void SetVideoWriterStartTime(struct timeval &t) { video_store_data->recording = t; } + void SetVideoWriterStartTime(const struct timeval &t) { video_store_data->recording = t; } unsigned int GetPreEventCount() const { return pre_event_count; }; struct timeval GetVideoBufferDuration() const { return video_buffer_duration; }; @@ -505,7 +505,7 @@ public: inline time_t getStartupTime() const { return shared_data->startup_time; } inline void setStartupTime( time_t p_time ) { shared_data->startup_time = p_time; } - int LabelSize() { return label_size; } + int LabelSize() const { return label_size; } void actionReload(); void actionEnable(); diff --git a/src/zm_packetqueue.h b/src/zm_packetqueue.h index 31fe321cb..cbdf556b4 100644 --- a/src/zm_packetqueue.h +++ b/src/zm_packetqueue.h @@ -31,7 +31,7 @@ extern "C" { } class zm_packetqueue { public: - zm_packetqueue(int max_stream_id); + explicit zm_packetqueue(int max_stream_id); virtual ~zm_packetqueue(); bool queuePacket(AVPacket* packet, struct timeval *timestamp); bool queuePacket(ZMPacket* packet); diff --git a/src/zm_time.h b/src/zm_time.h index 0f7dbc794..490ca1e2c 100644 --- a/src/zm_time.h +++ b/src/zm_time.h @@ -76,7 +76,9 @@ struct DeltaTimeval #define USEC_PER_SEC 1000000 #define MSEC_PER_SEC 1000 +/* extern struct timeval tv; +*/ inline int tvDiffUsec( struct timeval first, struct timeval last ) { diff --git a/src/zm_user.h b/src/zm_user.h index 42fe07554..519ea13de 100644 --- a/src/zm_user.h +++ b/src/zm_user.h @@ -46,7 +46,7 @@ class User { User(); explicit User(const MYSQL_ROW &dbrow); ~User(); - User(User &u) { Copy(u); } + User(const User &u) { Copy(u); } void Copy(const User &u); User& operator=(const User &u) { Copy(u); return *this; diff --git a/src/zmu.cpp b/src/zmu.cpp index bdc467812..30a2a0ce7 100644 --- a/src/zmu.cpp +++ b/src/zmu.cpp @@ -482,7 +482,7 @@ int main(int argc, char *argv[]) { } // end if ! MONITOR if ( verbose ) { - printf("Monitor %d(%s)\n", monitor->Id(), monitor->Name()); + printf("Monitor %u(%s)\n", monitor->Id(), monitor->Name()); } if ( !monitor->connect() ) { Error("Can't connect to capture daemon: %d %s", monitor->Id(), monitor->Name()); @@ -521,19 +521,19 @@ int main(int argc, char *argv[]) { } if ( function & ZMU_READ_IDX ) { if ( verbose ) - printf("Last read index: %d\n", monitor->GetLastReadIndex()); + printf("Last read index: %u\n", monitor->GetLastReadIndex()); else { if ( have_output ) fputc(separator, stdout); - printf("%d", monitor->GetLastReadIndex()); + printf("%u", monitor->GetLastReadIndex()); have_output = true; } } if ( function & ZMU_WRITE_IDX ) { if ( verbose ) { - printf("Last write index: %d\n", monitor->GetLastWriteIndex()); + printf("Last write index: %u\n", monitor->GetLastWriteIndex()); } else { if ( have_output ) fputc(separator, stdout); - printf("%d", monitor->GetLastWriteIndex()); + printf("%u", monitor->GetLastWriteIndex()); have_output = true; } } @@ -558,9 +558,9 @@ int main(int argc, char *argv[]) { if ( function & ZMU_IMAGE ) { if ( verbose ) { if ( image_idx == -1 ) - printf("Dumping last image captured to Monitor%d.jpg", monitor->Id()); + printf("Dumping last image captured to Monitor%u.jpg", monitor->Id()); else - printf("Dumping buffer image %d to Monitor%d.jpg", image_idx, monitor->Id()); + printf("Dumping buffer image %d to Monitor%u.jpg", image_idx, monitor->Id()); if ( scale != -1 ) printf(", scaling by %d%%", scale); printf("\n"); @@ -569,7 +569,7 @@ int main(int argc, char *argv[]) { } if ( function & ZMU_ZONES ) { if ( verbose ) - printf("Dumping zone image to Zones%d.jpg\n", monitor->Id()); + printf("Dumping zone image to Zones%u.jpg\n", monitor->Id()); monitor->DumpZoneImage(zoneString); } if ( function & ZMU_ALARM ) { @@ -735,17 +735,17 @@ int main(int argc, char *argv[]) { Debug(1, "Got %d monitors", mysql_num_rows(result)); printf("%4s%5s%6s%9s%14s%6s%6s%8s%8s\n", "Id", "Func", "State", "TrgState", "LastImgTim", "RdIdx", "WrIdx", "LastEvt", "FrmRate"); - for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row(result); i++ ) { - int mon_id = atoi(dbrow[0]); - int function = atoi(dbrow[1]); - if ( !user || user->canAccess(mon_id) ) { - if ( function > 1 ) { - Monitor *monitor = Monitor::Load(mon_id, false, Monitor::QUERY); + for ( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row(result); i++ ) { + int monitor_id = atoi(dbrow[0]); + int monitor_function = atoi(dbrow[1]); + if ( !user || user->canAccess(monitor_id) ) { + if ( monitor_function > 1 ) { + Monitor *monitor = Monitor::Load(monitor_id, false, Monitor::QUERY); if ( monitor && monitor->connect() ) { struct timeval tv = monitor->GetTimestamp(); printf( "%4d%5d%6d%9d%11ld.%02ld%6d%6d%8" PRIu64 "%8.2f\n", monitor->Id(), - function, + monitor_function, monitor->GetState(), monitor->GetTriggerState(), tv.tv_sec, tv.tv_usec/10000,