diff --git a/qoa.h b/qoa.h index e11c842..69a8438 100644 --- a/qoa.h +++ b/qoa.h @@ -501,6 +501,9 @@ void *qoa_encode(const short *sample_data, qoa_desc *qoa, unsigned int *out_len) num_slices * 8 * qoa->channels; /* 8 byte slices */ unsigned char *bytes = QOA_MALLOC(encoded_size); + if (!bytes) { + return NULL; + } for (unsigned int c = 0; c < qoa->channels; c++) { /* Set the initial LMS weights to {0, 0, -1, 2}. This helps with the @@ -658,6 +661,9 @@ short *qoa_decode(const unsigned char *bytes, int size, qoa_desc *qoa) { /* Calculate the required size of the sample buffer and allocate */ int total_samples = qoa->samples * qoa->channels; short *sample_data = QOA_MALLOC(total_samples * sizeof(short)); + if (!sample_data) { + return NULL; + } unsigned int sample_index = 0; unsigned int frame_len; diff --git a/qoaplay.c b/qoaplay.c index 475d6cf..2f442e1 100644 --- a/qoaplay.c +++ b/qoaplay.c @@ -58,12 +58,14 @@ qoaplay_desc *qoaplay_open(const char *path) { unsigned char header[QOA_MIN_FILESIZE]; int read = fread(header, QOA_MIN_FILESIZE, 1, file); if (!read) { + fclose(file); return NULL; } qoa_desc qoa; unsigned int first_frame_pos = qoa_decode_header(header, QOA_MIN_FILESIZE, &qoa); if (!first_frame_pos) { + fclose(file); return NULL; } @@ -78,8 +80,12 @@ qoaplay_desc *qoaplay_open(const char *path) { unsigned int sample_data_size = qoa.channels * QOA_FRAME_LEN * sizeof(short) * 2; qoaplay_desc *qp = malloc(sizeof(qoaplay_desc) + buffer_size + sample_data_size); + if (!qp) { + fclose(file); + return NULL; + } + memset(qp, 0, sizeof(qoaplay_desc)); - qp->first_frame_pos = first_frame_pos; qp->file = file; qp->buffer = ((unsigned char *)qp) + sizeof(qoaplay_desc);