From b2b049f727402b6b8a6ac42340e58c5465e1af79 Mon Sep 17 00:00:00 2001 From: Xerbo Date: Wed, 25 Jan 2023 20:37:55 +0000 Subject: [PATCH] Initialize memory before use --- aptdec-cli/main.c | 40 ++++++++++++++++++++-------------------- aptdec-cli/pngio.c | 4 ++-- libaptdec/dsp.c | 12 +++++++----- libaptdec/effects.c | 6 +++--- libaptdec/image.c | 14 +++++++------- 5 files changed, 39 insertions(+), 37 deletions(-) diff --git a/aptdec-cli/main.c b/aptdec-cli/main.c index 581933f..c9da9e0 100644 --- a/aptdec-cli/main.c +++ b/aptdec-cli/main.c @@ -86,7 +86,7 @@ char *basename(const char *filename) { #endif int main(int argc, const char **argv) { - char version[128]; + char version[128] = { 0 }; get_version(version); printf("%s\n", version); // clang-format off @@ -166,7 +166,7 @@ int main(int argc, const char **argv) { static int process_file(const char *path, options_t *opts) { const char *path_basename = basename((char *)path); const char *dot = strrchr(path_basename, '.'); - char name[256]; + char name[256] = { 0 }; if (dot == NULL) { strncpy(name, path_basename, 255); } else { @@ -181,7 +181,7 @@ static int process_file(const char *path, options_t *opts) { writer_t *realtime_png; if (opts->realtime) { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-decoding.png", name); realtime_png = writer_init(filename, APT_REGION_FULL, APTDEC_MAX_HEIGHT, PNG_COLOR_TYPE_GRAY, "Unknown"); @@ -209,7 +209,7 @@ static int process_file(const char *path, options_t *opts) { } // Decode image - float *data = (float *)malloc(APT_IMG_WIDTH * (APTDEC_MAX_HEIGHT+1) * sizeof(float)); + float *data = calloc(APT_IMG_WIDTH * (APTDEC_MAX_HEIGHT+1), sizeof(float)); size_t rows; for (rows = 0; rows < APTDEC_MAX_HEIGHT; rows++) { float *row = &data[rows * APT_IMG_WIDTH]; @@ -238,7 +238,7 @@ static int process_file(const char *path, options_t *opts) { writer_free(realtime_png); #pragma GCC diagnostic pop - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-decoding.png", name); remove(filename); } @@ -294,9 +294,9 @@ static int process_file(const char *path, options_t *opts) { if (strcmp(images[i], "thermal") == 0) { if (img.ch[1] >= 4) { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-thermal.png", base); - char description[128]; + char description[128] = { 0 }; sprintf(description, "Calibrated thermal image, channel %s - %s", channel_name[img.ch[1]], channel_desc[img.ch[1]]); // Perform visible calibration @@ -313,9 +313,9 @@ static int process_file(const char *path, options_t *opts) { } } else if (strcmp(images[i], "visible") == 0) { if (img.ch[0] <= 2) { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-visible.png", base); - char description[128]; + char description[128] = { 0 }; sprintf(description, "Calibrated visible image, channel %s - %s", channel_name[img.ch[0]], channel_desc[img.ch[0]]); // Perform visible calibration @@ -352,9 +352,9 @@ static int process_file(const char *path, options_t *opts) { } if (strcmp(images[i], "raw") == 0) { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-raw.png", base); - char description[128]; + char description[128] = { 0 }; sprintf(description, "Raw image, channel %s - %s / %s - %s", channel_name[img.ch[0]], @@ -376,9 +376,9 @@ static int process_file(const char *path, options_t *opts) { writer_free(writer); } else if (strcmp(images[i], "lut") == 0) { if (opts->lut != NULL && opts->lut[0] != '\0') { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-lut.png", base); - char description[128]; + char description[128] = { 0 }; sprintf(description, "LUT image, channel %s - %s / %s - %s", channel_name[img.ch[0]], @@ -387,7 +387,7 @@ static int process_file(const char *path, options_t *opts) { channel_desc[img.ch[1]] ); - png_colorp lut = (png_colorp)malloc(sizeof(png_color)*256*256); + png_colorp lut = calloc(256*256, sizeof(png_color)); if (read_lut(opts->lut, lut)) { writer_t *writer = writer_init(filename, APT_REGION_CHA, img.rows, PNG_COLOR_TYPE_RGB, description); writer_write_image_lut(writer, &img, lut); @@ -398,9 +398,9 @@ static int process_file(const char *path, options_t *opts) { warning("Cannot create LUT image, missing -l/--lut"); } } else if (strcmp(images[i], "a") == 0) { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-a.png", base); - char description[128]; + char description[128] = { 0 }; sprintf(description, "Channel A: %s - %s", channel_name[img.ch[0]], channel_desc[img.ch[0]]); writer_t *writer; @@ -415,9 +415,9 @@ static int process_file(const char *path, options_t *opts) { } writer_free(writer); } else if (strcmp(images[i], "b") == 0) { - char filename[269]; + char filename[269] = { 0 }; sprintf(filename, "%s-b.png", base); - char description[128]; + char description[128] = { 0 }; sprintf(description, "Channel B: %s - %s", channel_name[img.ch[1]], channel_desc[img.ch[1]]); writer_t *writer; @@ -517,7 +517,7 @@ static void write_line(writer_t *png, float *row) { } apt_image_t strip(apt_image_t img) { - uint8_t *data = (uint8_t *)malloc(img.rows * APT_IMG_WIDTH); + uint8_t *data = calloc(img.rows * APT_IMG_WIDTH, sizeof(uint8_t)); for (size_t y = 0; y < img.rows; y++) { memcpy(&data[y*APT_IMG_WIDTH], &img.data[y*APT_IMG_WIDTH + APT_CHA_OFFSET], APT_CH_WIDTH); memcpy(&data[y*APT_IMG_WIDTH + APT_CH_WIDTH], &img.data[y*APT_IMG_WIDTH + APT_CHB_OFFSET], APT_CH_WIDTH); @@ -534,4 +534,4 @@ int array_contains(char **array, char *value, size_t n) { } } return 0; -} \ No newline at end of file +} diff --git a/aptdec-cli/pngio.c b/aptdec-cli/pngio.c index b267d12..c60feaf 100644 --- a/aptdec-cli/pngio.c +++ b/aptdec-cli/pngio.c @@ -25,7 +25,7 @@ #include "util.h" writer_t *writer_init(const char *filename, apt_region_t region, uint32_t height, int color, char *channel) { - writer_t *png = (writer_t *)malloc(sizeof(writer_t)); + writer_t *png = calloc(1, sizeof(writer_t)); png->region = region; // Create writer @@ -43,7 +43,7 @@ writer_t *writer_init(const char *filename, apt_region_t region, uint32_t height png_set_IHDR(png->png, png->info, png->region.width, height, 8, color, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); - char version[128]; + char version[128] = { 0 }; int version_len = get_version(version); png_text text[] = { diff --git a/libaptdec/dsp.c b/libaptdec/dsp.c index 1bc2af1..c82a412 100644 --- a/libaptdec/dsp.c +++ b/libaptdec/dsp.c @@ -74,11 +74,11 @@ char *aptdec_get_version(void) { } fir_t *fir_init(size_t max_size, size_t ntaps) { - fir_t *fir = (fir_t *)malloc(sizeof(fir_t)); + fir_t *fir = calloc(1, sizeof(fir_t)); fir->ntaps = ntaps; fir->ring_size = max_size + ntaps; - fir->taps = (float *)malloc(ntaps * sizeof(float)); - fir->ring_buffer = (float *)malloc((max_size + ntaps) * sizeof(float)); + fir->taps = calloc(ntaps, sizeof(float)); + fir->ring_buffer = calloc(max_size + ntaps, sizeof(float)); return fir; } @@ -89,7 +89,7 @@ void fir_free(fir_t *fir) { } pll_t *pll_init(float alpha, float beta, float min_freq, float max_freq, float sample_rate) { - pll_t *pll = (pll_t *)malloc(sizeof(pll_t)); + pll_t *pll = calloc(1, sizeof(pll_t)); pll->alpha = alpha; pll->beta = beta; pll->min_freq = M_TAUf * min_freq / sample_rate; @@ -104,9 +104,11 @@ aptdec_t *aptdec_init(float sample_rate) { return NULL; } - aptdec_t *apt = (aptdec_t *)malloc(sizeof(aptdec_t)); + aptdec_t *apt = calloc(1, sizeof(aptdec_t)); apt->sample_rate = sample_rate; apt->sync_frequency = 1.0f; + apt->interpolator_n = APTDEC_BUFFER_SIZE; + apt->interpolator_offset = 0.0f; // PLL configuration // https://www.trondeau.com/blog/2011/8/13/control-loop-gain-values.html diff --git a/libaptdec/effects.c b/libaptdec/effects.c index 61ab0ae..1afec32 100644 --- a/libaptdec/effects.c +++ b/libaptdec/effects.c @@ -102,7 +102,7 @@ void apt_stretch(apt_image_t *img, apt_region_t region) { void apt_denoise(apt_image_t *img, apt_region_t region) { for (size_t y = 1; y < img->rows - 1; y++) { for (size_t x = 1; x < region.width - 1; x++) { - float pixels[9]; + float pixels[9] = { 0.0f }; int pixeln = 0; for (int y2 = -1; y2 < 2; y2++) { for (int x2 = -1; x2 < 2; x2++) { @@ -139,12 +139,12 @@ int apt_crop(apt_image_t *img) { const float sync_pattern[] = {-1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 0}; - float *spc_rows = (float *)malloc(img->rows * sizeof(float)); + float *spc_rows = calloc(img->rows, sizeof(float)); int startCrop = 0; int endCrop = img->rows; for (size_t y = 0; y < img->rows; y++) { - float temp[39]; + float temp[39] = { 0.0f }; for (size_t i = 0; i < 39; i++) { temp[i] = img->data[y * APT_IMG_WIDTH + i]; } diff --git a/libaptdec/image.c b/libaptdec/image.c index 5982c7d..e6498bd 100644 --- a/libaptdec/image.c +++ b/libaptdec/image.c @@ -21,9 +21,9 @@ #include #include #include +#include #include "algebra.h" -#include #include "util.h" #include "calibration.h" @@ -31,14 +31,14 @@ apt_image_t apt_image_clone(apt_image_t img) { apt_image_t _img = img; - _img.data = (uint8_t *)malloc(APT_IMG_WIDTH * img.rows); + _img.data = calloc(APT_IMG_WIDTH * img.rows, sizeof(uint8_t)); memcpy(_img.data, img.data, APT_IMG_WIDTH * img.rows); return _img; } static void decode_telemetry(const float *data, size_t rows, size_t offset, float *wedges) { // Calculate row average - float *telemetry_rows = (float *)malloc(rows * sizeof(float)); + float *telemetry_rows = calloc(rows, sizeof(float)); for (size_t y = 0; y < rows; y++) { telemetry_rows[y] = meanf(&data[y*APT_IMG_WIDTH + offset + APT_CH_WIDTH], APT_TELEMETRY_WIDTH); } @@ -59,7 +59,7 @@ static void decode_telemetry(const float *data, size_t rows, size_t offset, floa // Find the least noisy frame (via standard deviation) float best_noise = FLT_MAX; size_t best_frame = 0; - for (size_t y = telemetry_offset; y < rows; y += APT_FRAME_LEN) { + for (size_t y = telemetry_offset; y < rows-APT_FRAME_LEN; y += APT_FRAME_LEN) { float noise = 0.0f; for (size_t i = 0; i < APT_FRAME_WEDGES; i++) { noise += standard_deviation(&telemetry_rows[y + i*APT_WEDGE_HEIGHT], APT_WEDGE_HEIGHT); @@ -79,7 +79,7 @@ static void decode_telemetry(const float *data, size_t rows, size_t offset, floa } static float average_spc(apt_image_t *img, size_t offset) { - float *rows = (float *)malloc(img->rows * sizeof(float)); + float *rows = calloc(img->rows, sizeof(float)); float average = 0.0f; for (size_t y = 0; y < img->rows; y++) { float row_average = 0.0f; @@ -170,7 +170,7 @@ static void make_thermal_lut(apt_image_t *img, avhrr_channel_t ch, int satellite const float B = calibration.rad[ch].B; // Compute PRT temperature - float T[4]; + float T[4] = { 0.0f }; for (size_t n = 0; n < 4; n++) { T[n] = quadratic_calc(img->telemetry[1][n + 9] * APT_COUNT_RATIO, calibration.prt[n]); } @@ -201,7 +201,7 @@ int apt_calibrate_thermal(apt_image_t *img, apt_region_t region) { return 1; } - float lut[256]; + float lut[256] = { 0.0f }; make_thermal_lut(img, img->ch[1], img->satellite, lut); for (size_t y = 0; y < img->rows; y++) {