From 00ce812db3c06362ed28ded94699bbc69c641be5 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 6 Oct 2014 14:55:12 +0200 Subject: [PATCH] Couple of small fixes (mainly leaks) --- dxt_compress/dxt_util.c | 5 +++- ldgm-coding/ldgm-session-cpu.cpp | 2 +- ldgm-coding/ldgm-session-gpu.cpp | 10 ++++---- ldgm-coding/ldgm-session.cpp | 4 ++- ldgm-coding/ldgm-session.h | 12 +++++---- ldgm-coding/matrix-gen/matrix-generator.cpp | 4 +-- ldgm-coding/matrix-gen/rand_pmms.h | 2 +- ldgm-coding/tanner.cpp | 1 + ldgm-coding/tanner.h | 3 --- src/audio/capture/testcard.c | 1 + src/audio/playback/decklink.cpp | 1 + src/audio/wav_reader.h | 10 ++++---- src/capture_filter/logo.cpp | 6 ++++- src/control_socket.cpp | 11 ++++++-- src/main.cpp | 4 +-- src/messaging.cpp | 1 + src/module.c | 4 ++- src/rtp/audio_decoders.cpp | 6 ++--- src/rtp/rtp.c | 2 +- src/rtp/video_decoders.cpp | 13 +++++----- src/video_capture.cpp | 1 + src/video_capture/decklink.cpp | 26 ++++++++++++------- src/video_capture/deltacast.cpp | 2 ++ src/video_capture/deltacast_dvi.cpp | 13 ++++++---- src/video_capture/dvs.c | 28 +++++++++++++-------- src/video_capture/import.c | 27 ++++++++++++-------- src/video_capture/switcher.c | 1 + src/video_capture/swmix.cpp | 21 +++++++++------- src/video_capture/testcard.c | 25 ++++++++++++------ src/video_capture/v4l2.c | 8 ++++-- src/video_compress.cpp | 4 +-- src/video_compress/jpeg.cpp | 6 +++-- src/video_display/bluefish444.cpp | 10 ++++---- src/video_display/decklink.cpp | 4 ++- src/video_display/dvs.c | 9 +++---- src/video_export.c | 1 + src/video_rxtx/ihdtv.cpp | 4 +++ 37 files changed, 183 insertions(+), 109 deletions(-) diff --git a/dxt_compress/dxt_util.c b/dxt_compress/dxt_util.c index 4698869ba..b2b0d0aee 100644 --- a/dxt_compress/dxt_util.c +++ b/dxt_compress/dxt_util.c @@ -69,6 +69,7 @@ dxt_shader_create_from_file(const char* filename, GLenum type) char* program = (char*)malloc((data_size + 1) * sizeof(char)); if ( (size_t) data_size != fread(program, sizeof(char), data_size, file) ) { fprintf(stderr, "Failed to load program [%d bytes] from file %s!\n", data_size, filename); + free(program); fclose(file); return 0; } @@ -77,8 +78,10 @@ dxt_shader_create_from_file(const char* filename, GLenum type) printf("Compiling program [%s]...\n", filename); GLuint shader = dxt_shader_create_from_source(program, type); - if ( shader == 0 ) + if ( shader == 0 ) { + free(program); return 0; + } free(program); diff --git a/ldgm-coding/ldgm-session-cpu.cpp b/ldgm-coding/ldgm-session-cpu.cpp index 330a4beef..4d043e173 100644 --- a/ldgm-coding/ldgm-session-cpu.cpp +++ b/ldgm-coding/ldgm-session-cpu.cpp @@ -345,7 +345,7 @@ LDGM_session_cpu::decode_frame ( char* received, int buf_size, int* frame_size, map_it++; found = true; } - if ( map_it->first > 0 ) + if ( found ) map_it--; //Next, find out if some interval covers this symbol diff --git a/ldgm-coding/ldgm-session-gpu.cpp b/ldgm-coding/ldgm-session-gpu.cpp index 7f6a1ca1d..9d405cc98 100644 --- a/ldgm-coding/ldgm-session-gpu.cpp +++ b/ldgm-coding/ldgm-session-gpu.cpp @@ -122,8 +122,8 @@ void LDGM_session_gpu::encode ( char *source_data, char *parity ) { assert(parity == source_data+param_k*get_packet_size()); - int w_f = max_row_weight + 2; - int buf_size = (param_k + param_m) * packet_size; + unsigned int w_f = max_row_weight + 2; + unsigned int buf_size = (param_k + param_m) * packet_size; cudaError_t error; @@ -135,7 +135,7 @@ void LDGM_session_gpu::encode ( char *source_data, char *parity ) cudaMalloc(&OUTBUF,buf_size); OUTBUF_SIZE=buf_size; } - if(buf_size>OUTBUF_SIZE){ + if((int)buf_size>OUTBUF_SIZE){ // puts("cudaMalloc"); cudaFree(OUTBUF); cudaMalloc(&OUTBUF,buf_size); @@ -231,7 +231,7 @@ char *LDGM_session_gpu::decode_frame ( char *received_data, int buf_size, int *f map_it++; found = true; } - if ( map_it->first > 0 ) map_it--; + if ( found ) map_it--; if ( found && (map_it->first + map_it->second) >= (node_offset + p_size) ) @@ -269,7 +269,7 @@ char *LDGM_session_gpu::decode_frame ( char *received_data, int buf_size, int *f * params.packet_size = p_size; * params.buf_size = buf_size; */ - int w_f = max_row_weight + 2; + unsigned int w_f = max_row_weight + 2; // printf("K: %d, M: %d, max_row_weight: %d, buf_size: %d,\n", param_k, param_m, max_row_weight, buf_size ); diff --git a/ldgm-coding/ldgm-session.cpp b/ldgm-coding/ldgm-session.cpp index e9afed6aa..390783b9c 100644 --- a/ldgm-coding/ldgm-session.cpp +++ b/ldgm-coding/ldgm-session.cpp @@ -68,7 +68,9 @@ LDGM_session::set_pcMatrix ( char* fname) "\nReceived K = " << k_f << ", M = " << m_f << "\n"; throw oss.str(); } - fseek (f, 1, SEEK_CUR ); + if (fseek (f, 1, SEEK_CUR ) != 0) { + perror("fseek"); + } pcm = (int*) malloc(w_f*param_m*sizeof(int)); for ( int i = 0; i < (int)w_f*param_m; i++) { diff --git a/ldgm-coding/ldgm-session.h b/ldgm-coding/ldgm-session.h index c33cf08be..05fd45f7f 100644 --- a/ldgm-coding/ldgm-session.h +++ b/ldgm-coding/ldgm-session.h @@ -37,10 +37,12 @@ class LDGM_session : public Coding_session { public: /* ==================== LIFECYCLE ======================================= */ - LDGM_session (){ - elapsed_sum2=0.0; - no_frames2=0; - } /* constructor **/ + LDGM_session () : /* constructor **/ + pcMatrix(NULL), pcm(NULL), param_k(0), param_m(0), row_weight(0), + packet_size(0), max_row_weight(0), received_ptr(NULL), + lost_ptr(NULL), elapsed_sum2(0.0), no_frames2(0) + { + } virtual ~LDGM_session (); /* destructor **/ /* ==================== ACCESSORS ======================================= */ @@ -116,7 +118,7 @@ class LDGM_session : public Coding_session char *received_ptr; char *lost_ptr; - double elapsed_sum2; + double elapsed_sum2; long no_frames2; static const int HEADER_SIZE = 4; diff --git a/ldgm-coding/matrix-gen/matrix-generator.cpp b/ldgm-coding/matrix-gen/matrix-generator.cpp index 02708401f..04645a9dc 100644 --- a/ldgm-coding/matrix-gen/matrix-generator.cpp +++ b/ldgm-coding/matrix-gen/matrix-generator.cpp @@ -167,12 +167,12 @@ int generate_ldgm_matrix(char *fname, unsigned int k, unsigned int m, unsigned i } // printf ( "fwrite: %d\n", tmp ); } + fflush(out); + fclose(out); } else { fprintf(stderr, "Cannot open file for writing."); } - fflush(out); - fclose(out); /* out = fopen(fname, "rb"); * diff --git a/ldgm-coding/matrix-gen/rand_pmms.h b/ldgm-coding/matrix-gen/rand_pmms.h index cb315f6cc..6bb344976 100644 --- a/ldgm-coding/matrix-gen/rand_pmms.h +++ b/ldgm-coding/matrix-gen/rand_pmms.h @@ -23,7 +23,7 @@ class Rand_pmms { public: - Rand_pmms() {}; + Rand_pmms() = default; void seedi ( unsigned long int s ) { diff --git a/ldgm-coding/tanner.cpp b/ldgm-coding/tanner.cpp index e5d651364..ad29f64d3 100644 --- a/ldgm-coding/tanner.cpp +++ b/ldgm-coding/tanner.cpp @@ -34,6 +34,7 @@ Node::Node(Tanner_graph * /* tanner */, Node::Node_type t, char *d) { done = false; // neighbours[0] = 0; // printf("Created node with data: %d\n", *data); + tanner = nullptr; /// @todo remove, it is no longer used } Node::~Node() { diff --git a/ldgm-coding/tanner.h b/ldgm-coding/tanner.h index 8d89153de..b30178ca2 100644 --- a/ldgm-coding/tanner.h +++ b/ldgm-coding/tanner.h @@ -128,9 +128,6 @@ class Tanner_graph { public: /* ==================== LIFECYCLE ======================================= */ - Tanner_graph () {} /* constructor */ - Tanner_graph ( const Tanner_graph &other ); /* copy constructor */ - ~Tanner_graph () {} /* destructor */ /* ==================== ACCESSORS ======================================= */ diff --git a/src/audio/capture/testcard.c b/src/audio/capture/testcard.c index 7a9fc89cf..d935c9984 100644 --- a/src/audio/capture/testcard.c +++ b/src/audio/capture/testcard.c @@ -183,6 +183,7 @@ void * audio_cap_testcard_init(char *cfg) FILE *wav = fopen(wav_file, "r"); if(!wav) { fprintf(stderr, "Unable to open WAV.\n"); + free(s); return NULL; } struct wav_metadata metadata; diff --git a/src/audio/playback/decklink.cpp b/src/audio/playback/decklink.cpp index 187063363..0f7d0f7b3 100644 --- a/src/audio/playback/decklink.cpp +++ b/src/audio/playback/decklink.cpp @@ -317,6 +317,7 @@ void *decklink_playback_init(char *cfg) printf("audioConsumerLevels\n"); printf("\tIf set true the analog audio levels are set to maximum gain on audio input.\n"); printf("\tIf set false the selected analog input gain levels are used.\n"); + free(s); return NULL; } else { char *tmp = strdup(cfg); diff --git a/src/audio/wav_reader.h b/src/audio/wav_reader.h index 72431d523..1a2b432b2 100644 --- a/src/audio/wav_reader.h +++ b/src/audio/wav_reader.h @@ -1,12 +1,12 @@ #include // FILE * struct wav_metadata { - int ch_count; - int sample_rate; - int bits_per_sample; + unsigned int ch_count; + unsigned int sample_rate; + unsigned int bits_per_sample; - int data_size; - int data_offset; // from the beginning of file + unsigned int data_size; + unsigned int data_offset; // from the beginning of file }; #define WAV_HDR_PARSE_OK 0 diff --git a/src/capture_filter/logo.cpp b/src/capture_filter/logo.cpp index 5bf0a5ebe..dc7c0a204 100644 --- a/src/capture_filter/logo.cpp +++ b/src/capture_filter/logo.cpp @@ -103,6 +103,9 @@ static bool load_logo_data_from_file(struct state_capture_filter_logo *s, const } getline(file, line); } + if (s->width * s->height == 0) { + throw string("Unspecified header field!"); + } if ((rgb && depth != 3) || depth != 4) { throw string("Unsupported depth passed."); } @@ -146,7 +149,7 @@ static int init(struct module *parent, const char *cfg, void **state) printf("'logo' usage:\n"); printf("\tlogo:[:[:]]\n"); printf("\t\t - is path to logo to be added in PAM format with alpha\n"); - free(s); + delete s; return 1; } char *tmp = strdup(cfg); @@ -203,6 +206,7 @@ static struct video_frame *filter(void *state, struct video_frame *in) if (rect_x < 0 || rect_x + s->width > in->tiles[0].width) { rect_x = in->tiles[0].width - s->width; } + assert(get_pf_block_size(in->color_spec) > 0); rect_x = (rect_x / get_pf_block_size(in->color_spec)) * get_pf_block_size(in->color_spec); if (rect_y < 0 || rect_y + s->height > in->tiles[0].height) { diff --git a/src/control_socket.cpp b/src/control_socket.cpp index 7a1b7a1a6..183dd7155 100644 --- a/src/control_socket.cpp +++ b/src/control_socket.cpp @@ -187,8 +187,12 @@ int control_init(int port, int connection_type, struct control_state **state, st s->socket_fd = socket(AF_INET6, SOCK_STREAM, 0); assert(s->socket_fd != INVALID_SOCKET); int val = 1; - setsockopt(s->socket_fd, SOL_SOCKET, SO_REUSEADDR, + int rc; + rc = setsockopt(s->socket_fd, SOL_SOCKET, SO_REUSEADDR, (sso_val_type) &val, sizeof(val)); + if (rc != 0) { + perror("Control socket - setsockopt"); + } /* setting address to in6addr_any allows connections to be established * from both IPv4 and IPv6 hosts. This behavior can be modified @@ -199,7 +203,10 @@ int control_init(int port, int connection_type, struct control_state **state, st s_in.sin6_addr = in6addr_any; s_in.sin6_port = htons(s->network_port); - ::bind(s->socket_fd, (const struct sockaddr *) &s_in, sizeof(s_in)); + rc = ::bind(s->socket_fd, (const struct sockaddr *) &s_in, sizeof(s_in)); + if (rc != 0) { + perror("Control socket - bind"); + } listen(s->socket_fd, MAX_CLIENTS); } else { s->socket_fd = socket(AF_INET, SOCK_STREAM, 0); diff --git a/src/main.cpp b/src/main.cpp index be667588f..4006fcb54 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -995,7 +995,7 @@ int main(int argc, char *argv[]) // UltraGrid RTP params["postprocess"].ptr = (void *) postprocess; - params["decoder_mode"].l = decoder_mode; + params["decoder_mode"].l = (long) decoder_mode; params["display_device"].ptr = uv->display_device; // SAGE @@ -1019,7 +1019,7 @@ int main(int argc, char *argv[]) avType = none; } - params["avType"].l = avType; + params["avType"].l = (long) avType; } uv->state_video_rxtx = video_rxtx::create(video_protocol, params); diff --git a/src/messaging.cpp b/src/messaging.cpp index 11aaa6d9e..ed5e1e3cb 100644 --- a/src/messaging.cpp +++ b/src/messaging.cpp @@ -42,6 +42,7 @@ struct response *send_message(struct module *root, const char *const_path, struc simple_linked_list_append(old_receiver->msg_queue_childs, saved_message); pthread_mutex_unlock(&old_receiver->lock); + free(tmp); return new_response(RESPONSE_ACCEPTED, NULL); } pthread_mutex_lock(&receiver->lock); diff --git a/src/module.c b/src/module.c index 790e8eccd..11583bdc7 100644 --- a/src/module.c +++ b/src/module.c @@ -205,6 +205,7 @@ static void get_receiver_index(char *node_str, int *index) { struct module *get_module(struct module *root, const char *const_path) { assert(root != NULL); + assert(const_path != NULL); struct module *receiver = root; char *path, *tmp; @@ -213,10 +214,11 @@ struct module *get_module(struct module *root, const char *const_path) pthread_mutex_lock(&root->lock); tmp = path = strdup(const_path); + assert(path != NULL); while ((item = strtok_r(path, ".", &save_ptr))) { struct module *old_receiver = receiver; - receiver = get_matching_child(receiver, path); + receiver = get_matching_child(receiver, item); if (!receiver) { pthread_mutex_unlock(&old_receiver->lock); diff --git a/src/rtp/audio_decoders.cpp b/src/rtp/audio_decoders.cpp index a658c85bb..3383b16ca 100644 --- a/src/rtp/audio_decoders.cpp +++ b/src/rtp/audio_decoders.cpp @@ -316,10 +316,8 @@ void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, const error: free(tmp); - if (s) { - audio_decoder_destroy(s); - free(s); - } + audio_decoder_destroy(s); + free(s); return NULL; } diff --git a/src/rtp/rtp.c b/src/rtp/rtp.c index 7c8ed1eec..19c10a27f 100644 --- a/src/rtp/rtp.c +++ b/src/rtp/rtp.c @@ -3634,6 +3634,7 @@ int rtp_set_encryption_key(struct rtp *session, const char *passphrase) MD5Update(&context, (u_char *) canonical_passphrase, strlen(canonical_passphrase)); MD5Final((u_char *) hash, &context); + free(canonical_passphrase); /* Initialize the encryption algorithm we've received */ @@ -3644,7 +3645,6 @@ int rtp_set_encryption_key(struct rtp *session, const char *passphrase) } else { debug_msg("Encryption algorithm \"%s\" not found\n", session->encryption_algorithm); - free(canonical_passphrase); return FALSE; } } diff --git a/src/rtp/video_decoders.cpp b/src/rtp/video_decoders.cpp index 33a36fc09..44183839c 100644 --- a/src/rtp/video_decoders.cpp +++ b/src/rtp/video_decoders.cpp @@ -245,7 +245,7 @@ struct state_video_decoder * has been processed and we can write to a new one */ pthread_cond_t buffer_swapped_cv; ///< condition variable associated with @ref buffer_swapped - unique_ptr> decompress_queue; + unique_ptr> decompress_queue; codec_t out_codec; // display or postprocessor @@ -259,7 +259,7 @@ struct state_video_decoder int pp_output_frames_count; /// @} - unique_ptr> fec_queue; + unique_ptr> fec_queue; enum video_mode video_mode; ///< video mode set for this decoder unsigned merged_fb:1; ///< flag if the display device driver requires tiled video or not @@ -309,8 +309,7 @@ static void *fec_thread(void *args) { struct fec_desc desc(FEC_NONE); while(1) { - struct fec_msg *data; - data = dynamic_cast(decoder->fec_queue->pop()); + struct fec_msg *data = decoder->fec_queue->pop(); if(data->poisoned) { decompress_msg *msg = new decompress_msg(0); @@ -487,7 +486,7 @@ static void *decompress_thread(void *args) { struct tile *tile; while(1) { - decompress_msg *msg = dynamic_cast(decoder->decompress_queue->pop()); + decompress_msg *msg = decoder->decompress_queue->pop(); if(msg->poisoned) { delete msg; @@ -647,8 +646,8 @@ struct state_video_decoder *video_decoder_init(struct module *parent, s->buffer_swapped = true; s->last_buffer_number = -1; - s->decompress_queue = unique_ptr>(new message_queue<>(1)); - s->fec_queue = unique_ptr>(new message_queue<>(1)); + s->decompress_queue = unique_ptr>(new message_queue(1)); + s->fec_queue = unique_ptr>(new message_queue(1)); if (encryption) { if(openssl_decrypt_init(&s->decrypt, diff --git a/src/video_capture.cpp b/src/video_capture.cpp index 6485e211d..8dfd54058 100644 --- a/src/video_capture.cpp +++ b/src/video_capture.cpp @@ -604,6 +604,7 @@ int vidcap_init(struct module *parent, vidcap_id_t id, struct vidcap_params *par if (ret != 0) { module_done(&d->mod); + free(d); return ret; } diff --git a/src/video_capture/decklink.cpp b/src/video_capture/decklink.cpp index baf0f06dc..9fe2254fd 100644 --- a/src/video_capture/decklink.cpp +++ b/src/video_capture/decklink.cpp @@ -222,7 +222,7 @@ static void print_input_modes (IDeckLink* deckLink); static int blackmagic_api_version_check(BMD_STR *current_version); HRESULT -VideoDelegate::VideoInputFrameArrived (IDeckLinkVideoInputFrame *arrivedFrame, IDeckLinkAudioInputPacket *audioPacket) +VideoDelegate::VideoInputFrameArrived (IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioPacket) { bool noSignal = false; // Video @@ -230,22 +230,28 @@ VideoDelegate::VideoInputFrameArrived (IDeckLinkVideoInputFrame *arrivedFrame, I pthread_mutex_lock(&(s->lock)); // LOCK - LOCK - LOCK - LOCK - LOCK - LOCK - LOCK - LOCK - LOCK - LOCK - LOCK // - if(arrivedFrame) + if (videoFrame) { - if (arrivedFrame->GetFlags() & bmdFrameHasNoInputSource) + if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) { fprintf(stderr, "Frame received (#%d) - No input signal detected\n", s->frames); noSignal = true; } else{ newFrameReady = 1; // The new frame is ready to grab - // printf("Frame received (#%lu) - Valid Frame (Size: %li bytes)\n", framecount, arrivedFrame->GetRowBytes() * arrivedFrame->GetHeight()); + // printf("Frame received (#%lu) - Valid Frame (Size: %li bytes)\n", framecount, videoFrame->GetRowBytes() * videoFrame->GetHeight()); } } - if(newFrameReady) { - arrivedFrame->GetBytes(&pixelFrame); + /// @todo + /// Figure out when there are comming audio packets (if video processing is not fast enough/progressive NTSC with Intensity or 3:2 pulldown) + /// @todo + /// All the newFrameReady stuff is a bit ugly... + + if (videoFrame && newFrameReady) { + /// @todo videoFrame should be actually retained until the data are processed + videoFrame->GetBytes(&pixelFrame); if(audioPacket) { audioPacket->GetBytes(&audioFrame); @@ -271,7 +277,7 @@ VideoDelegate::VideoInputFrameArrived (IDeckLinkVideoInputFrame *arrivedFrame, I if(s->stereo) { IDeckLinkVideoFrame3DExtensions *rightEye; HRESULT result; - result = arrivedFrame->QueryInterface(IID_IDeckLinkVideoFrame3DExtensions, (void **)&rightEye); + result = videoFrame->QueryInterface(IID_IDeckLinkVideoFrame3DExtensions, (void **)&rightEye); if (result == S_OK) { result = rightEye->GetFrameForRightEye(&rightEyeFrame); @@ -293,7 +299,7 @@ VideoDelegate::VideoInputFrameArrived (IDeckLinkVideoInputFrame *arrivedFrame, I timecode = NULL; if(s->use_timecode && !noSignal) { HRESULT result; - result = arrivedFrame->GetTimecode(bmdTimecodeRP188Any, &timecode); + result = videoFrame->GetTimecode(bmdTimecodeRP188Any, &timecode); if(result != S_OK) { fprintf(stderr, "Failed to acquire timecode from stream. Disabling sync.\n"); s->use_timecode = FALSE; @@ -807,13 +813,13 @@ vidcap_decklink_init(const struct vidcap_params *params) device_found[i] = false; if(s->stereo) { - s->frame = vf_alloc(2); if (s->devices_cnt > 1) { fprintf(stderr, "[DeckLink] Passed more than one device while setting 3D mode. " "In this mode, only one device needs to be passed."); free(s); return NULL; } + s->frame = vf_alloc(2); } else { s->frame = vf_alloc(s->devices_cnt); } @@ -827,6 +833,8 @@ vidcap_decklink_init(const struct vidcap_params *params) // Create an IDeckLinkIterator object to enumerate all DeckLink cards in the system deckLinkIterator = create_decklink_iterator(); if (deckLinkIterator == NULL) { + vf_free(s->frame); + free(s); return NULL; } while (deckLinkIterator->Next(&deckLink) == S_OK) diff --git a/src/video_capture/deltacast.cpp b/src/video_capture/deltacast.cpp index 1238938f8..23e78f277 100644 --- a/src/video_capture/deltacast.cpp +++ b/src/video_capture/deltacast.cpp @@ -368,6 +368,8 @@ vidcap_deltacast_init(const struct vidcap_params *params) if(s == NULL) { printf("Unable to allocate DELTACAST state\n"); + free(init_fmt); + free(s); return NULL; } diff --git a/src/video_capture/deltacast_dvi.cpp b/src/video_capture/deltacast_dvi.cpp index e718634db..535c19e6c 100644 --- a/src/video_capture/deltacast_dvi.cpp +++ b/src/video_capture/deltacast_dvi.cpp @@ -509,11 +509,12 @@ vidcap_deltacast_dvi_init(const struct vidcap_params *params) printf("vidcap_deltacast_dvi_init\n"); - char *init_fmt = NULL; + char *init_fmt = NULL, + *tmp = NULL; if (vidcap_params_get_fmt(params) != NULL) - init_fmt = strdup(vidcap_params_get_fmt(params)); + tmp = init_fmt = strdup(vidcap_params_get_fmt(params)); if(init_fmt && strcmp(init_fmt, "help") == 0) { - free(init_fmt); + free(tmp); usage(); return &vidcap_init_noerr; } @@ -521,7 +522,7 @@ vidcap_deltacast_dvi_init(const struct vidcap_params *params) s = (struct vidcap_deltacast_dvi_state *) calloc(1, sizeof(struct vidcap_deltacast_dvi_state)); if(s == NULL) { printf("Unable to allocate DELTACAST state\n"); - return NULL; + goto error; } s->codec = BGR; @@ -580,7 +581,8 @@ vidcap_deltacast_dvi_init(const struct vidcap_params *params) BrdId = 0; printf("[DELTACAST] Automatically choosen device nr. 0\n"); } - free(init_fmt); + free(tmp); + tmp = NULL; /* Query VideoMasterHD information */ Result = VHD_GetApiInfo(&DllVersion,&NbBoards); @@ -721,6 +723,7 @@ no_stream: bad_channel: VHD_CloseBoardHandle(s->BoardHandle); error: + free(tmp); free(s); return NULL; } diff --git a/src/video_capture/dvs.c b/src/video_capture/dvs.c index b08f219d2..d3b7ebc5c 100644 --- a/src/video_capture/dvs.c +++ b/src/video_capture/dvs.c @@ -234,10 +234,15 @@ void *vidcap_dvs_init(const struct vidcap_params *params) int i; int res; int mode_index = 0; - char *card_name = ""; + char card_name[128] = ""; s = (struct vidcap_dvs_state *) calloc(1, sizeof(struct vidcap_dvs_state)); + + if (s == NULL) { + debug_msg("Unable to allocate DVS state\n"); + return NULL; + } s->frame = vf_alloc(1); s->tile = vf_get_tile(s->frame, 0); @@ -245,14 +250,10 @@ void *vidcap_dvs_init(const struct vidcap_params *params) s->detect_mode = FALSE; - if (s == NULL) { - debug_msg("Unable to allocate DVS state\n"); - return NULL; - } - if (vidcap_params_get_fmt(params) != NULL) { if (strcmp(vidcap_params_get_fmt(params), "help") == 0) { show_help(); + free(s); return 0; } @@ -260,13 +261,15 @@ void *vidcap_dvs_init(const struct vidcap_params *params) char *tmp; if(strncmp(fmt, "PCI", 3) == 0) { - card_name = fmt; + strncat(card_name, fmt, sizeof card_name - 1); printf("[DVS] Choosen card: %s.\n", card_name); s->detect_mode = TRUE; } else { tmp = strtok(fmt, ":"); if (!tmp) { fprintf(stderr, "Wrong config %s\n", fmt); + free(fmt); + free(s); return 0; } mode_index = atoi(tmp); @@ -278,6 +281,7 @@ void *vidcap_dvs_init(const struct vidcap_params *params) } if(s->mode == NULL) { fprintf(stderr, "dvs: unknown video mode: %d\n", mode_index); + free(fmt); free(s); return 0; } @@ -285,20 +289,24 @@ void *vidcap_dvs_init(const struct vidcap_params *params) tmp = strtok(NULL, ":"); if (!tmp) { fprintf(stderr, "Wrong config %s\n", fmt); + free(fmt); + free(s); return 0; } s->frame->color_spec = get_codec_from_name(tmp); if (s->frame->color_spec == VIDEO_CODEC_NONE) { fprintf(stderr, "dvs: unknown codec: %s\n", tmp); + free(fmt); + free(s); return 0; } /* card name */ tmp = strtok(NULL, ":"); if(tmp) { - card_name = tmp; - tmp[strlen(card_name)] = ':'; + tmp[strlen(tmp)] = ':'; + strncat(card_name, fmt, sizeof card_name - 1); printf("[DVS] Choosen card: %s.\n", card_name); } } @@ -343,6 +351,7 @@ void *vidcap_dvs_init(const struct vidcap_params *params) break; default: fprintf(stderr, "[dvs] Unsupported video codec passed!"); + free(s); return NULL; } @@ -475,7 +484,6 @@ void *vidcap_dvs_init(const struct vidcap_params *params) ); if (res != SV_OK) { goto error; - abort(); } res = sv_fifo_start(s->sv, s->fifo); if (res != SV_OK) { diff --git a/src/video_capture/import.c b/src/video_capture/import.c index 2e7419a66..e59ea8a00 100644 --- a/src/video_capture/import.c +++ b/src/video_capture/import.c @@ -304,7 +304,7 @@ vidcap_import_init(const struct vidcap_params *params) pthread_cond_init(&s->boss_cv, NULL); char *tmp = strdup(vidcap_params_get_fmt(params)); - char *save_ptr; + char *save_ptr = NULL; s->directory = strdup(strtok_r(tmp, ":", &save_ptr)); char *suffix; if (!s->directory || strcmp(s->directory, "help") == 0) { @@ -343,6 +343,7 @@ vidcap_import_init(const struct vidcap_params *params) s->audio_state.has_audio = true; if(pthread_create(&s->audio_state.thread_id, NULL, audio_reading_thread, (void *) s) != 0) { fprintf(stderr, "Unable to create thread.\n"); + free(audio_filename); goto error; } } else { @@ -468,6 +469,7 @@ vidcap_import_init(const struct vidcap_params *params) s->video_desc = desc; fclose(info); + info = NULL; if(items_found != (1 << 7) - 1) { fprintf(stderr, "[import] Failed while reading config file - some items missing.\n"); @@ -494,9 +496,8 @@ error: free(tmp); if (info != NULL) fclose(info); - if (s) { - cleanup_common(s); - } + cleanup_common(s); + free(s); return NULL; } @@ -608,8 +609,6 @@ static void cleanup_common(struct vidcap_import_state *s) { pthread_cond_destroy(&s->audio_state.boss_cv); pthread_mutex_destroy(&s->audio_state.lock); } - - free(s); } void vidcap_import_done(void *state) @@ -620,6 +619,7 @@ void vidcap_import_done(void *state) vidcap_import_finish(state); cleanup_common(s); + free(s); } /* @@ -790,27 +790,34 @@ static void * control_thread(void *args) { struct vidcap_import_state *s = (struct vidcap_import_state *) args; fd_t fd; + int rc; fd = socket(AF_INET6, SOCK_STREAM, 0); assert(fd != INVALID_SOCKET); int val = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + if (!rc) { + perror("Video import - setsockopt"); + } struct sockaddr_in6 s_in; s_in.sin6_family = AF_INET6; s_in.sin6_addr = in6addr_any; s_in.sin6_port = htons(CONTROL_PORT); - bind(fd, (const struct sockaddr *) &s_in, sizeof(s_in)); + rc = bind(fd, (const struct sockaddr *) &s_in, sizeof(s_in)); + if (!rc) { + perror("Video import: unable to bind communication pipe"); + } listen(fd, MAX_CLIENTS); struct sockaddr_storage client_addr; socklen_t len; unlink(PIPE); errno = 0; - int fifo_status = mkfifo(PIPE, 0777); + rc = mkfifo(PIPE, 0777); struct client *clients = NULL; - if(!fifo_status) { + if (!rc) { clients = malloc(sizeof(struct client)); clients->fd = open(PIPE, O_RDONLY | O_NONBLOCK); assert(clients->fd != -1); diff --git a/src/video_capture/switcher.c b/src/video_capture/switcher.c index 7988d92a5..7aa3f521e 100644 --- a/src/video_capture/switcher.c +++ b/src/video_capture/switcher.c @@ -110,6 +110,7 @@ vidcap_switcher_init(const struct vidcap_params *params) char *cfg = strdup(cfg_c); char *save_ptr, *item; char *tmp = cfg; + assert(cfg != NULL); while ((item = strtok_r(cfg, ":", &save_ptr))) { if (strcmp(item, "help") == 0) { show_help(); diff --git a/src/video_capture/swmix.cpp b/src/video_capture/swmix.cpp index f0f506d0c..b289bc2cc 100644 --- a/src/video_capture/swmix.cpp +++ b/src/video_capture/swmix.cpp @@ -931,8 +931,10 @@ static int parse_config_string(const char *fmt, unsigned int *width, while((item = strtok_r(tmp, ":", &save_ptr))) { switch (token_nr) { case 0: - if(strcasecmp(item, "file") == 0) + if(strcasecmp(item, "file") == 0) { + free(parse_string); return PARSE_FILE; + } *width = atoi(item); break; case 1: @@ -1063,7 +1065,13 @@ vidcap_swmix_init(const struct vidcap_params *params) printf("vidcap_swmix_init\n"); - s = new vidcap_swmix_state; + if(!vidcap_params_get_fmt(params) || + strcmp(vidcap_params_get_fmt(params), "help") == 0) { + show_help(); + return &vidcap_init_noerr; + } + + s = new vidcap_swmix_state(); if(s == NULL) { printf("Unable to allocate swmix capture state\n"); return NULL; @@ -1078,12 +1086,6 @@ vidcap_swmix_init(const struct vidcap_params *params) s->bicubic_algo = strdup("BSpline"); gettimeofday(&s->t0, NULL); - if(!vidcap_params_get_fmt(params) || - strcmp(vidcap_params_get_fmt(params), "help") == 0) { - show_help(); - return &vidcap_init_noerr; - } - memset(&desc, 0, sizeof(desc)); desc.tile_count = 1; desc.color_spec = RGBA; @@ -1130,7 +1132,8 @@ vidcap_swmix_init(const struct vidcap_params *params) s->slaves_data = init_slave_data(s, config_file); if(!s->slaves_data) { - free(s); + free(config_file); + delete s; return NULL; } diff --git a/src/video_capture/testcard.c b/src/video_capture/testcard.c index 9c8360e2d..7d474ca0d 100644 --- a/src/video_capture/testcard.c +++ b/src/video_capture/testcard.c @@ -126,13 +126,13 @@ static void grab_audio(int chan, void *stream, int len, void *udata) UNUSED(chan); struct testcard_state *s = (struct testcard_state *) udata; - if(s->audio_end + len <= AUDIO_BUFFER_SIZE) { + if(s->audio_end + len <= (int) AUDIO_BUFFER_SIZE) { memcpy(s->audio_data + s->audio_end, stream, len); s->audio_end += len; } else { int offset = AUDIO_BUFFER_SIZE - s->audio_end; memcpy(s->audio_data + s->audio_end, stream, offset); - memcpy(s->audio_data, stream + offset, len - offset); + memcpy(s->audio_data, (char *) stream + offset, len - offset); s->audio_end = len - offset; } /* just hack - Mix_Volume doesn't mute correctly the audio */ @@ -145,7 +145,7 @@ static int configure_audio(struct testcard_state *s) UNUSED(s); #if defined HAVE_LIBSDL_MIXER && ! defined HAVE_MACOSX - char *filename; + char filename[1024] = ""; int fd; Mix_Music *music; ssize_t bytes_written = 0l; @@ -157,8 +157,12 @@ static int configure_audio(struct testcard_state *s) fprintf(stderr,"[testcard] error initalizing sound\n"); return -1; } - filename = strdup("/tmp/uv.midiXXXXXX"); + strncpy(filename, "/tmp/uv.midiXXXXXX", sizeof filename - 1); fd = mkstemp(filename); + if (fd < 0) { + perror("mkstemp"); + return -1; + } do { ssize_t ret; @@ -169,7 +173,6 @@ static int configure_audio(struct testcard_state *s) } while (bytes_written < (ssize_t) sizeof(song1)); close(fd); music = Mix_LoadMUS(filename); - free(filename); s->audio_data = calloc(1, AUDIO_BUFFER_SIZE /* 1 sec */); s->audio_start = 0; @@ -343,6 +346,8 @@ void *vidcap_testcard_init(const struct vidcap_params *params) if(bpp == 0) { fprintf(stderr, "Unknown codec '%s'\n", tmp); + free(tmp); + free(s); return NULL; } @@ -362,22 +367,25 @@ void *vidcap_testcard_init(const struct vidcap_params *params) && strncmp(filename, "s=", 2ul) != 0 && strcmp(filename, "i") != 0 && strcmp(filename, "sf") != 0) { - s->data = malloc(s->size * bpp * 2); - in = fopen(filename, "r"); if (!in) { perror("fopen"); + free(fmt); free(s); return NULL; } fseek(in, 0L, SEEK_END); long filesize = ftell(in); + assert(filesize >= 0); fseek(in, 0L, SEEK_SET); + s->data = malloc(s->size * bpp * 2); + if (s->size < filesize) { fprintf(stderr, "Error wrong file size for selected " "resolution and codec. File size %ld, " "computed size %d\n", filesize, s->size); + free(fmt); free(s->data); free(s); fclose(in); @@ -386,8 +394,11 @@ void *vidcap_testcard_init(const struct vidcap_params *params) if (!in || fread(s->data, filesize, 1, in) == 0) { fprintf(stderr, "Cannot read file %s\n", filename); + free(fmt); free(s->data); free(s); + if (in) + fclose(in); return NULL; } diff --git a/src/video_capture/v4l2.c b/src/video_capture/v4l2.c index 97c5ff536..6b42bad4c 100644 --- a/src/video_capture/v4l2.c +++ b/src/video_capture/v4l2.c @@ -149,7 +149,9 @@ static void show_help() struct v4l2_capability capab; memset(&capab, 0, sizeof capab); - ioctl(fd, VIDIOC_QUERYCAP, &capab); + if (ioctl(fd, VIDIOC_QUERYCAP, &capab) != 0) { + perror("[V4L2] Unable to query device capabilities"); + } printf("\t%sDevice %s (%s):\n", (i == 0 ? "(*) " : " "), @@ -263,7 +265,9 @@ struct vidcap_type * vidcap_v4l2_probe(bool verbose) strncpy(vt->cards[vt->card_count - 1].id, name, sizeof vt->cards[vt->card_count - 1].id - 1); struct v4l2_capability capab; memset(&capab, 0, sizeof capab); - ioctl(fd, VIDIOC_QUERYCAP, &capab); + if (ioctl(fd, VIDIOC_QUERYCAP, &capab) != 0) { + perror("[V4L2] Unable to query device capabilities"); + } snprintf(vt->cards[vt->card_count - 1].name, sizeof vt->cards[vt->card_count - 1].name, "V4L2 %s", capab.card); close(fd); diff --git a/src/video_compress.cpp b/src/video_compress.cpp index 41cff4cda..52313048a 100644 --- a/src/video_compress.cpp +++ b/src/video_compress.cpp @@ -541,12 +541,12 @@ static shared_ptr compress_frame_tiles(struct compress_state_real * s->state_count = frame->tile_count; } - task_result_handle_t task_handle[frame->tile_count]; - vector> separate_tiles = vf_separate_tiles(frame); // frame pointer may no longer be valid frame = NULL; + vector task_handle(separate_tiles.size()); + vector data_tile(separate_tiles.size()); for(unsigned int i = 0; i < separate_tiles.size(); ++i) { struct compress_worker_data *data = &data_tile[i]; diff --git a/src/video_compress/jpeg.cpp b/src/video_compress/jpeg.cpp index dd066f8be..5d426f507 100644 --- a/src/video_compress/jpeg.cpp +++ b/src/video_compress/jpeg.cpp @@ -274,8 +274,6 @@ struct module * jpeg_compress_init(struct module *parent, const struct video_com struct state_video_compress_jpeg *s; const char *opts = params->cfg; - s = new state_video_compress_jpeg(); - if(opts && strcmp(opts, "help") == 0) { printf("JPEG comperssion usage:\n"); printf("\t-c JPEG[:[:]]\n"); @@ -286,6 +284,8 @@ struct module * jpeg_compress_init(struct module *parent, const struct video_com return &compress_init_noerr; } + s = new state_video_compress_jpeg(); + s->restart_interval = -1; gpujpeg_set_default_parameters(&s->encoder_param); @@ -293,6 +293,8 @@ struct module * jpeg_compress_init(struct module *parent, const struct video_com if(opts && opts[0] != '\0') { char *fmt = strdup(opts); if (!parse_fmt(s, fmt)) { + free(fmt); + delete s; return NULL; } free(fmt); diff --git a/src/video_display/bluefish444.cpp b/src/video_display/bluefish444.cpp index 6a51f8bfa..1fb5d81fb 100644 --- a/src/video_display/bluefish444.cpp +++ b/src/video_display/bluefish444.cpp @@ -65,6 +65,7 @@ #include #include #include +#include #include "bluefish444_common.h" @@ -92,20 +93,19 @@ static void RouteChannel(CBLUEVELVET_H pSDK, ULONG Source, ULONG Destination, struct av_buffer { av_buffer(int TilesCount, int GoldenSize) : - BufferId(0), m_TilesCount(TilesCount) + pVideoBuffer(TilesCount), BufferId(0) { - for(int i = 0; i < m_TilesCount; ++i) { + for(int i = 0; i < TilesCount; ++i) { pVideoBuffer[i] = (unsigned int *) page_aligned_alloc(GoldenSize); } } virtual ~av_buffer() { - for(int i = 0; i < m_TilesCount; ++i) { + for(unsigned int i = 0; i < pVideoBuffer.size(); ++i) { page_aligned_free(pVideoBuffer[i]); } } - unsigned int *pVideoBuffer[MAX_BLUE_OUT_CHANNELS]; + vector pVideoBuffer; unsigned int BufferId; - int m_TilesCount; }; struct display_bluefish444_state { diff --git a/src/video_display/decklink.cpp b/src/video_display/decklink.cpp index a791e3286..6d9a79d28 100644 --- a/src/video_display/decklink.cpp +++ b/src/video_display/decklink.cpp @@ -758,6 +758,7 @@ void *display_decklink_init(const char *fmt, unsigned int flags) } else if (strcmp(fmt, "help") == 0) { show_help(); + delete s; return &display_init_noerr; } else { char *tmp = strdup(fmt); @@ -829,6 +830,7 @@ void *display_decklink_init(const char *fmt, unsigned int flags) fprintf(stderr, "\nA DeckLink iterator could not be created. The DeckLink drivers may not be installed or are outdated.\n"); fprintf(stderr, "This UltraGrid version was compiled with DeckLink drivers %s. You should have at least this version.\n\n", BLACKMAGIC_DECKLINK_API_VERSION_STRING); + delete s; return NULL; } @@ -889,7 +891,7 @@ void *display_decklink_init(const char *fmt, unsigned int flags) for(int i = 0; i < s->devices_cnt; ++i) { // Obtain the audio/video output interface (IDeckLinkOutput) - if (s->state[i].deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&s->state[i].deckLinkOutput) != S_OK) { + if ((result = s->state[i].deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&s->state[i].deckLinkOutput)) != S_OK) { printf("Could not obtain the IDeckLinkOutput interface: %08x\n", (int) result); goto error; } diff --git a/src/video_display/dvs.c b/src/video_display/dvs.c index 828cdd1a6..1b048925d 100644 --- a/src/video_display/dvs.c +++ b/src/video_display/dvs.c @@ -657,8 +657,9 @@ void *display_dvs_init(const char *cfg, unsigned int flags) int res; char *fmt = NULL; - if (strcmp(cfg, "help") == 0) { + if (cfg && strcmp(cfg, "help") == 0) { show_help(); + free(name); return &display_init_noerr; } @@ -796,10 +797,8 @@ void *display_dvs_init(const char *cfg, unsigned int flags) error: free(name); free(fmt); - if (s) { - vf_free(s->frame); - free(s); - } + vf_free(s->frame); + free(s); return NULL; } diff --git a/src/video_export.c b/src/video_export.c index 44c92fff6..61ea3bfe7 100644 --- a/src/video_export.c +++ b/src/video_export.c @@ -122,6 +122,7 @@ static void *video_export_thread(void *arg) } FILE *out = fopen(current->filename, "wb"); + assert (out != NULL); if (fwrite(current->data, current->data_len, 1, out) != 1) { perror("fwrite"); } diff --git a/src/video_rxtx/ihdtv.cpp b/src/video_rxtx/ihdtv.cpp index 9844f590e..f81f7866b 100644 --- a/src/video_rxtx/ihdtv.cpp +++ b/src/video_rxtx/ihdtv.cpp @@ -121,6 +121,10 @@ static void *ihdtv_sender_thread(void *arg) ihdtv_video_rxtx::ihdtv_video_rxtx(map const ¶ms) : video_rxtx(params) +#ifdef HAVE_IHDTV + , m_tx_connection(), m_rx_connection() +#endif + { #ifdef HAVE_IHDTV int argc = params.at("argc").i;