PVS-Studio analys fixes: libfreedv serious issues

pull/375/head
f4exb 2019-06-14 18:08:38 +02:00
rodzic 5d7aa89304
commit 8eb0dcf267
7 zmienionych plików z 214 dodań i 136 usunięć

Wyświetl plik

@ -119,32 +119,41 @@ struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
if ((mode != FREEDV_MODE_1600) && (mode != FREEDV_MODE_700) &&
(mode != FREEDV_MODE_700B) && (mode != FREEDV_MODE_2400A) &&
(mode != FREEDV_MODE_2400B) && (mode != FREEDV_MODE_800XA) &&
(mode != FREEDV_MODE_700C) && (mode != FREEDV_MODE_700D) )
return NULL;
(mode != FREEDV_MODE_700C) && (mode != FREEDV_MODE_700D)) {
return nullptr;
}
f = (struct freedv*) malloc(sizeof(struct freedv));
if (f == NULL)
return NULL;
if (f == nullptr) {
return nullptr;
}
f->mode = mode;
f->verbose = 0;
f->test_frames = f->smooth_symbols = 0;
f->freedv_put_error_pattern = NULL;
f->error_pattern_callback_state = NULL;
f->freedv_put_error_pattern = nullptr;
f->error_pattern_callback_state = nullptr;
f->n_protocol_bits = 0;
f->frames = 0;
/* Init states for this mode, and set up samples in/out -----------------------------------------*/
if (mode == FREEDV_MODE_1600) {
if (mode == FREEDV_MODE_1600)
{
f->snr_squelch_thresh = 2.0;
f->squelch_en = 1;
Nc = 16;
f->tx_sync_bit = 0;
codec2_mode = CODEC2_MODE_1300;
f->fdmdv = fdmdv_create(Nc);
if (f->fdmdv == NULL)
return NULL;
if (f->fdmdv == nullptr)
{
free(f);
return nullptr;
}
golay23_init();
f->nin = FDMDV_NOM_SAMPLES_PER_FRAME;
f->n_nom_modem_samples = 2*FDMDV_NOM_SAMPLES_PER_FRAME;
@ -153,22 +162,32 @@ struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
f->modem_sample_rate = FS;
nbit = fdmdv_bits_per_frame(f->fdmdv);
f->fdmdv_bits = (int*) malloc(nbit*sizeof(int));
if (f->fdmdv_bits == NULL)
return NULL;
if (f->fdmdv_bits == nullptr) {
return nullptr;
}
nbit = 2*fdmdv_bits_per_frame(f->fdmdv);
f->tx_bits = (int*) malloc(nbit*sizeof(int));
f->rx_bits = (int*) malloc(nbit*sizeof(int));
if ((f->tx_bits == NULL) || (f->rx_bits == NULL)) {
if (f->tx_bits != NULL) {
if ((f->tx_bits == nullptr) || (f->rx_bits == nullptr))
{
if (f->tx_bits != nullptr)
{
free(f->tx_bits);
f->tx_bits = NULL;
f->tx_bits = nullptr;
}
if (f->rx_bits != NULL) {
if (f->rx_bits != nullptr)
{
free(f->rx_bits);
f->rx_bits = NULL;
f->rx_bits = nullptr;
}
return NULL;
return nullptr;
}
f->evenframe = 0;
f->sz_error_pattern = fdmdv_error_pattern_size(f->fdmdv);
}
@ -310,8 +329,8 @@ struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
}
#endif
if ((mode == FREEDV_MODE_2400A) || (mode == FREEDV_MODE_2400B)) {
if ((mode == FREEDV_MODE_2400A) || (mode == FREEDV_MODE_2400B))
{
/* Set up the C2 mode */
codec2_mode = CODEC2_MODE_1300;
/* Set the number of protocol bits */
@ -320,22 +339,25 @@ struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
f->ext_vco = 0;
}
if (mode == FREEDV_MODE_2400A) {
if (mode == FREEDV_MODE_2400A)
{
/* Create the framer|deframer */
f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A,0);
if(f->deframer == NULL)
return NULL;
f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A, 0);
if (f->deframer == nullptr) {
return nullptr;
}
f->fsk = fsk_create_hbr(48000,1200,10,4,1200,1200);
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fsk->Nbits*sizeof(uint8_t));
if(f->fsk == NULL){
if (f->fsk == nullptr)
{
fvhff_destroy_deframer(f->deframer);
return NULL;
return nullptr;
}
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fsk->Nbits*sizeof(int));
f->n_nom_modem_samples = f->fsk->N;
f->n_max_modem_samples = f->fsk->N + (f->fsk->Ts);
f->n_nat_modem_samples = f->fsk->N;
@ -343,53 +365,63 @@ struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
f->modem_sample_rate = 48000;
f->modem_symbol_rate = 1200;
/* Malloc something to appease freedv_init and freedv_destroy */
f->codec_bits = (int*) malloc(1);
f->codec_bits = (int*) malloc(1*sizeof(int));
}
if (mode == FREEDV_MODE_2400B) {
if (mode == FREEDV_MODE_2400B)
{
/* Create the framer|deframer */
f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A,1);
if(f->deframer == NULL) {
if (f->codec_bits != NULL) { free(f->codec_bits); }
return NULL;
f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A, 1);
if (f->deframer == nullptr)
{
if (f->codec_bits != nullptr) {
free(f->codec_bits);
}
return nullptr;
}
f->fmfsk = fmfsk_create(48000,2400);
f->fmfsk = fmfsk_create(48000, 2400);
if(f->fmfsk == NULL){
if (f->fmfsk == nullptr)
{
fvhff_destroy_deframer(f->deframer);
return NULL;
return nullptr;
}
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fmfsk->nbit*sizeof(uint8_t));
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fmfsk->nbit*sizeof(int));
f->n_nom_modem_samples = f->fmfsk->N;
f->n_max_modem_samples = f->fmfsk->N + (f->fmfsk->Ts);
f->n_nat_modem_samples = f->fmfsk->N;
f->nin = fmfsk_nin(f->fmfsk);
f->modem_sample_rate = 48000;
/* Malloc something to appease freedv_init and freedv_destroy */
f->codec_bits = (int*) malloc(1);
f->codec_bits = (int*) malloc(1*sizeof(int));
}
#ifdef CODEC2_MODE_700C
if (mode == FREEDV_MODE_800XA) {
if (mode == FREEDV_MODE_800XA)
{
/* Create the framer|deframer */
f->deframer = fvhff_create_deframer(FREEDV_HF_FRAME_B,0);
if(f->deframer == NULL)
return NULL;
f->deframer = fvhff_create_deframer(FREEDV_HF_FRAME_B, 0);
f->fsk = fsk_create_hbr(8000,400,10,4,800,400);
fsk_set_nsym(f->fsk,32);
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fsk->Nbits*sizeof(uint8_t));
if(f->fsk == NULL){
fvhff_destroy_deframer(f->deframer);
return NULL;
if (f->deframer == nullptr) {
return nullptr;
}
f->fsk = fsk_create_hbr(8000, 400, 10, 4, 800, 400);
fsk_set_nsym(f->fsk,32);
if (f->fsk == nullptr)
{
fvhff_destroy_deframer(f->deframer);
return nullptr;
}
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fsk->Nbits*sizeof(int));
f->n_nom_modem_samples = f->fsk->N;
f->n_max_modem_samples = f->fsk->N + (f->fsk->Ts);
f->n_nat_modem_samples = f->fsk->N;
@ -398,7 +430,7 @@ struct freedv *freedv_open_advanced(int mode, struct freedv_advanced *adv) {
f->modem_symbol_rate = 400;
/* Malloc something to appease freedv_init and freedv_destroy */
f->codec_bits = (int*) malloc(1);
f->codec_bits = (int*) malloc(1*sizeof(int));
f->n_protocol_bits = 0;
codec2_mode = CODEC2_MODE_700C;
@ -2410,15 +2442,18 @@ void freedv_set_carrier_ampl(struct freedv *freedv, int c, float ampl) {
\*---------------------------------------------------------------------------*/
int freedv_set_alt_modem_samp_rate(struct freedv *f, int samp_rate){
if(f->mode == FREEDV_MODE_2400A){
if(samp_rate == 24000 || samp_rate == 48000 || samp_rate == 96000){
int freedv_set_alt_modem_samp_rate(struct freedv *f, int samp_rate)
{
if (f->mode == FREEDV_MODE_2400A)
{
if (samp_rate == 24000 || samp_rate == 48000 || samp_rate == 96000)
{
fsk_destroy(f->fsk);
f->fsk = fsk_create_hbr(samp_rate,1200,10,4,1200,1200);
f->fsk = fsk_create_hbr(samp_rate, 1200, 10, 4, 1200, 1200);
free(f->tx_bits);
/* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
f->tx_bits = (int*) malloc(f->fsk->Nbits*sizeof(uint8_t));
f->tx_bits = (int*) malloc(f->fsk->Nbits*sizeof(int));
f->n_nom_modem_samples = f->fsk->N;
f->n_max_modem_samples = f->fsk->N + (f->fsk->Ts);
@ -2426,14 +2461,21 @@ int freedv_set_alt_modem_samp_rate(struct freedv *f, int samp_rate){
f->nin = fsk_nin(f->fsk);
f->modem_sample_rate = samp_rate;
return 0;
}else
return -1;
}else if(f->mode == FREEDV_MODE_2400B){
if(samp_rate == 48000 || samp_rate == 96000){
return -1;
}else
}
else
{
return -1;
}
}
else if (f->mode == FREEDV_MODE_2400B)
{
if (samp_rate == 48000 || samp_rate == 96000) {
return -1;
} else {
return -1;
}
}
return -1;
}

Wyświetl plik

@ -93,16 +93,22 @@ static unsigned char fdc_crc4(unsigned char *buffer, std::size_t len)
unsigned char crc = 0x0f;
std::size_t i;
for (i = 0; i < len; i++, buffer++) {
for (i = 0; i < len; i++, buffer++)
{
int shift;
for (shift = 7; shift <= 0; shift--) {
for (shift = 7; shift >= 0; shift--)
{
crc <<= 1;
if ((*buffer >> shift) & 0x1)
if ((*buffer >> shift) & 0x1) {
crc |= 1;
if (crc & 0x10)
}
if (crc & 0x10) {
crc ^= 0x03;
}
}
}
}
return crc & 0x0f;

Wyświetl plik

@ -19,6 +19,7 @@
#include <string.h>
#include <math.h>
#include <complex.h>
#include <algorithm>
#include "freedv_filter.h"
#include "freedv_filter_coef.h"
@ -45,16 +46,17 @@ namespace FreeDV
\*---------------------------------------------------------------------------*/
void quisk_filt_cfInit(struct quisk_cfFilter * filter, float * coefs, int taps) {
void quisk_filt_cfInit(struct quisk_cfFilter * filter, float * coefs, int taps)
{
// Prepare a new filter using coefs and taps. Samples are complex. Coefficients can
// be real or complex.
filter->dCoefs = coefs;
filter->cpxCoefs = NULL;
filter->cSamples = (std::complex<float> *) malloc(taps * sizeof(std::complex<float>));
memset(filter->cSamples, 0, taps * sizeof(std::complex<float>));
filter->cpxCoefs = nullptr;
filter->cSamples = new std::complex<float>[taps];
std::fill(filter->cSamples, filter->cSamples + taps, std::complex<float>{0.0, 0.0});
filter->ptcSamp = filter->cSamples;
filter->nTaps = taps;
filter->cBuf = NULL;
filter->cBuf = nullptr;
filter->nBuf = 0;
filter->decim_index = 0;
}
@ -70,20 +72,24 @@ void quisk_filt_cfInit(struct quisk_cfFilter * filter, float * coefs, int taps)
\*---------------------------------------------------------------------------*/
void quisk_filt_destroy(struct quisk_cfFilter * filter) {
if (filter->cSamples) {
free(filter->cSamples);
filter->cSamples = NULL;
void quisk_filt_destroy(struct quisk_cfFilter * filter)
{
if (filter->cSamples)
{
delete[] filter->cSamples;
filter->cSamples = nullptr;
}
if (filter->cBuf) {
free(filter->cBuf);
filter->cBuf = NULL;
if (filter->cBuf)
{
delete[] filter->cBuf;
filter->cBuf = nullptr;
}
if (filter->cpxCoefs) {
free(filter->cpxCoefs);
filter->cpxCoefs = NULL;
if (filter->cpxCoefs)
{
delete[] filter->cpxCoefs;
filter->cpxCoefs = nullptr;
}
}
@ -115,7 +121,7 @@ int quisk_cfInterpDecim(std::complex<float> * cSamples, int count, struct quisk_
if (filter->cBuf)
free(filter->cBuf);
filter->cBuf = (std::complex<float> *) malloc(filter->nBuf * sizeof(std::complex<float>));
filter->cBuf = new std::complex<float>[filter->nBuf];
}
memcpy(filter->cBuf, cSamples, count * sizeof(std::complex<float>));
@ -232,8 +238,9 @@ void quisk_cfTune(struct quisk_cfFilter * filter, float freq) {
float D, tune;
int i;
if ( ! filter->cpxCoefs)
filter->cpxCoefs = (std::complex<float> *) malloc(filter->nTaps * sizeof(std::complex<float>));
if ( ! filter->cpxCoefs) {
filter->cpxCoefs = new std::complex<float>[filter->nTaps];
}
tune = 2.0 * M_PI * freq;
D = (filter->nTaps - 1.0) / 2.0;

Wyświetl plik

@ -294,46 +294,65 @@ void fvhff_frame_data_bits(struct freedv_vhf_deframer * def, int frame_type,
}
/* Init and allocate memory for a freedv-vhf framer/deframer */
struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type, int enable_bit_flip){
struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type, int enable_bit_flip)
{
struct freedv_vhf_deframer * deframer;
uint8_t *bits,*invbits;
int frame_size;
int uw_size;
assert( (frame_type == FREEDV_VHF_FRAME_A) || (frame_type == FREEDV_HF_FRAME_B) );
assert((frame_type == FREEDV_VHF_FRAME_A) || (frame_type == FREEDV_HF_FRAME_B));
/* It's a Type A frame */
if(frame_type == FREEDV_VHF_FRAME_A){
if (frame_type == FREEDV_VHF_FRAME_A)
{
frame_size = 96;
uw_size = 16;
}else if(frame_type == FREEDV_HF_FRAME_B){
}
else if (frame_type == FREEDV_HF_FRAME_B)
{
frame_size = 64;
uw_size = 8;
}else{
return NULL;
}
else
{
return nullptr;
}
/* Allocate memory for the thing */
deframer = (freedv_vhf_deframer*) malloc(sizeof(struct freedv_vhf_deframer));
if(deframer == NULL)
return NULL;
if (deframer == nullptr) {
return nullptr;
}
/* Allocate the not-bit buffer */
if(enable_bit_flip){
if (enable_bit_flip)
{
invbits = (uint8_t*) malloc(sizeof(uint8_t)*frame_size);
if(invbits == NULL) {
if (invbits == nullptr)
{
free(deframer);
return NULL;
return nullptr;
}
}else{
invbits = NULL;
}
else
{
invbits = nullptr;
}
/* Allocate the bit buffer */
bits = (uint8_t*) malloc(sizeof(uint8_t)*frame_size);
if(bits == NULL) {
if (bits == nullptr)
{
if (invbits) {
free(invbits);
}
free(deframer);
return NULL;
return nullptr;
}
deframer->bits = bits;

Wyświetl plik

@ -671,7 +671,8 @@ void fsk_demod_freq_est(struct FSK *fsk, COMP fsk_in[],float *freqs,int M){
free(fftout);
}
void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float rx_sd[], COMP fsk_in[]){
void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float rx_sd[], COMP fsk_in[])
{
int N = fsk->N;
int Ts = fsk->Ts;
int Rs = fsk->Rs;
@ -687,7 +688,7 @@ void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float rx_sd[], COMP fsk_in[]
COMP* *f_int = new COMP*[M]; /* Filtered and downsampled symbol tones */
COMP *t = new COMP[M]; /* complex number temps */
COMP t_c; /* another complex temp */
COMP t_c; /* another complex temp */
COMP *phi_c = new COMP[M];
COMP phi_ft;
int nold = Nmem-nin;
@ -863,7 +864,12 @@ void fsk2_demod(struct FSK *fsk, uint8_t rx_bits[], float rx_sd[], COMP fsk_in[]
/* Check for NaNs in the fine timing estimate, return if found */
/* otherwise segfaults happen */
if( std::isnan(t_c.real) || std::isnan(t_c.imag)){
if (std::isnan(t_c.real) || std::isnan(t_c.imag))
{
free(f_intbuf_m);
delete[] dphi;
delete[] phi_c;
delete[] t;
return;
}

Wyświetl plik

@ -68,7 +68,8 @@ int choose_interleaver_b(int Nbits)
{
unsigned int i;
for(i=0; i<sizeof(b_table)/(2*sizeof(int)); i+=2) {
for (i = 0; i < sizeof(b_table)/(2*sizeof(int)); i += 2)
{
if (b_table[i] == Nbits) {
return b_table[i+1];
}

Wyświetl plik

@ -222,14 +222,10 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
ofdm_nuwbits = (ofdm_ns - 1) * ofdm_bps - ofdm_ntxtbits; // 10
/* Were ready to start filling in the OFDM structure now */
if ((ofdm = (struct OFDM *) malloc(sizeof (struct OFDM))) == NULL) {
return NULL;
}
ofdm->pilot_samples = (std::complex<float>*) malloc(sizeof (std::complex<float>) * (ofdm_m + ofdm_ncp));
ofdm->rxbuf = (std::complex<float>*) malloc(sizeof (std::complex<float>) * ofdm_rxbuf);
ofdm->pilots = (std::complex<float>*) malloc(sizeof (std::complex<float>) * (ofdm_nc + 2));
ofdm = new OFDM();
ofdm->pilot_samples = new std::complex<float>[ofdm_m + ofdm_ncp];
ofdm->rxbuf = new std::complex<float>[ofdm_rxbuf];
ofdm->pilots = new std::complex<float>[ofdm_nc + 2];
/*
* rx_sym is a 2D array of variable size
@ -237,14 +233,14 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
* allocate rx_sym row storage. It is a pointer to a pointer
*/
ofdm->rx_sym = (std::complex<float>**) malloc(sizeof (std::complex<float>) * (ofdm_ns + 3));
ofdm->rx_sym = new std::complex<float>*[ofdm_ns + 3];
/* allocate rx_sym column storage */
int free_last_rx_sym = 0;
for (i = 0; i < (ofdm_ns + 3); i++)
{
ofdm->rx_sym[i] = (std::complex<float> *) malloc(sizeof(std::complex<float>) * (ofdm_nc + 2));
ofdm->rx_sym[i] = new std::complex<float>[ofdm_nc + 2];
if (ofdm->rx_sym[i] == NULL) {
free_last_rx_sym = i;
@ -255,7 +251,7 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
/* The rest of these are 1D arrays of variable size */
ofdm->rx_np = (std::complex<float>*) malloc(sizeof (std::complex<float>) * (ofdm_rowsperframe * ofdm_nc));
ofdm->rx_np = new std::complex<float>[ofdm_rowsperframe * ofdm_nc];
ofdm->rx_amp = (float*) malloc(sizeof (float) * (ofdm_rowsperframe * ofdm_nc));
ofdm->aphase_est_pilot_log = (float*) malloc(sizeof (float) * (ofdm_rowsperframe * ofdm_nc));
ofdm->tx_uw = (uint8_t*) malloc(sizeof (uint8_t) * ofdm_nuwbits);
@ -346,7 +342,7 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
uw_ind[j + 1] = (val * 2) + 1; // bit index 2
}
tx_uw_syms = (std::complex<float>*) malloc(sizeof (std::complex<float>) * (ofdm_nuwbits / 2));
tx_uw_syms = new std::complex<float>[ofdm_nuwbits / 2];
for (i = 0; i < (ofdm_nuwbits / 2); i++) {
tx_uw_syms[i] = 1.0f; // qpsk_mod(0:0)
@ -369,7 +365,7 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
/* create the OFDM waveform */
std::complex<float> *temp = (std::complex<float>*) malloc(sizeof (std::complex<float>) * ofdm_m);
std::complex<float> *temp = new std::complex<float>[ofdm_m];
idft(ofdm, temp, ofdm->pilots);
@ -390,7 +386,8 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
for (i = ofdm_ncp, j = 0; j < ofdm_m; i++, j++) {
ofdm->pilot_samples[i] = temp[j];
}
free(temp);
delete[] temp;
/* calculate constant used to normalise timing correlation maximum */
@ -409,25 +406,25 @@ struct OFDM *ofdm_create(const struct OFDM_CONFIG *config) {
//// Error return points with free call in the reverse order of allocation:
free(tx_uw_syms);
delete[] tx_uw_syms;
free(uw_ind);
free(uw_ind_sym);
free(ofdm->tx_uw);
free(ofdm->aphase_est_pilot_log);
free(ofdm->rx_amp);
free(ofdm->rx_np);
delete[] ofdm->rx_np;
for (i = 0; i < free_last_rx_sym; i++) {
free(ofdm->rx_sym[i]);
delete[] ofdm->rx_sym[i];
}
free(ofdm->rx_sym);
free(ofdm->pilots);
free(ofdm->rxbuf);
free(ofdm->pilot_samples);
free(ofdm);
delete[] ofdm->rx_sym;
delete[] ofdm->pilots;
delete[] ofdm->rxbuf;
delete[] ofdm->pilot_samples;
delete ofdm;
return(NULL);
return(nullptr);
}
void allocate_tx_bpf(struct OFDM *ofdm) {
@ -451,16 +448,16 @@ void ofdm_destroy(struct OFDM *ofdm) {
if (ofdm->ofdm_tx_bpf)
deallocate_tx_bpf(ofdm);
free(ofdm->pilot_samples);
free(ofdm->rxbuf);
free(ofdm->pilots);
delete[] ofdm->pilot_samples;
delete[] ofdm->rxbuf;
delete[] ofdm->pilots;
for (i = 0; i < (ofdm_ns + 3); i++) { /* 2D array */
free(ofdm->rx_sym[i]);
delete[] ofdm->rx_sym[i];
}
free(ofdm->rx_sym);
free(ofdm->rx_np);
delete[] ofdm->rx_sym;
delete[] ofdm->rx_np;
free(ofdm->rx_amp);
free(ofdm->aphase_est_pilot_log);
free(ofdm->tx_uw);