Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch all the possible reallocs that could return a NULLto later be… #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/f_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ _WM_Event2Midi(struct _mdi *mdi, uint8_t **out, uint32_t *outsize) {
uint32_t track_size = 0;
uint32_t track_start = 0;
uint32_t track_count = 0;
uint8_t *new_out;

if (!mdi->event_count) {
_WM_GLOBAL_ERROR(WM_ERR_CONVERT, "(No events to convert)", 0);
Expand Down Expand Up @@ -1014,7 +1015,13 @@ _WM_Event2Midi(struct _mdi *mdi, uint8_t **out, uint32_t *outsize) {
(*out)[10] = (track_count >> 8) & 0xff;
(*out)[11] = track_count & 0xff;

(*out) = (uint8_t *) realloc((*out), out_ofs);
new_out = (uint8_t *) realloc((*out), out_ofs);
if (!new_out) {
free(out);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return -1;
}
*out = new_out;
(*outsize) = out_ofs;

return 0;
Expand Down
19 changes: 16 additions & 3 deletions src/internal_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,16 @@ float _WM_GetSamplesPerTick(uint32_t divisions, uint32_t tempo) {

static void _WM_CheckEventMemoryPool(struct _mdi *mdi) {
if ((mdi->event_count + 1) >= mdi->events_size) {
struct _event * new_events;
mdi->events_size += MEM_CHUNK;
mdi->events = (struct _event *) realloc(mdi->events,
(mdi->events_size * sizeof(struct _event)));
new_events = (struct _event *)
realloc(mdi->events, (mdi->events_size * sizeof(struct _event)));
if (!new_events){
free(mdi->events);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return;
}
mdi->events = new_events;
}
}

Expand Down Expand Up @@ -2147,7 +2154,13 @@ uint32_t _WM_SetupMidiEvent(struct _mdi *mdi, const uint8_t * event_data, uint32

/* Copy copyright info in the getinfo struct */
if (mdi->extra_info.copyright) {
mdi->extra_info.copyright = (char *) realloc(mdi->extra_info.copyright,(strlen(mdi->extra_info.copyright) + 1 + tmp_length + 1));
char * new_copyright = (char *) realloc(mdi->extra_info.copyright,(strlen(mdi->extra_info.copyright) + 1 + tmp_length + 1));
if (!new_copyright){
free(mdi->extra_info.copyright);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return(0);
}
mdi->extra_info.copyright = new_copyright;
memcpy(&mdi->extra_info.copyright[strlen(mdi->extra_info.copyright) + 1], event_data, tmp_length);
mdi->extra_info.copyright[strlen(mdi->extra_info.copyright) + 1 + tmp_length] = '\0';
mdi->extra_info.copyright[strlen(mdi->extra_info.copyright)] = '\n';
Expand Down
8 changes: 7 additions & 1 deletion src/mus2mid.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ struct mus_ctx {
#define DST_CHUNK 8192
static void resize_dst(struct mus_ctx *ctx) {
uint32_t pos = ctx->dst_ptr - ctx->dst;
ctx->dst = (uint8_t *) realloc(ctx->dst, ctx->dstsize + DST_CHUNK);
uint8_t *new_dst = (uint8_t *) realloc(ctx->dst, ctx->dstsize + DST_CHUNK);
if (!new_dst){
free(ctx->dst);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return;
}
ctx->dst = new_dst;
ctx->dstsize += DST_CHUNK;
ctx->dstrem += DST_CHUNK;
ctx->dst_ptr = ctx->dst + pos;
Expand Down
12 changes: 10 additions & 2 deletions src/patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdlib.h>

#include "wildmidi_lib.h"
#include "wm_error.h"
#include "internal_midi.h"
#include "lock.h"
#include "patches.h"
Expand Down Expand Up @@ -67,6 +68,7 @@ _WM_get_patch_data(struct _mdi *mdi, uint16_t patchid) {
void _WM_load_patch(struct _mdi *mdi, uint16_t patchid) {
uint32_t i;
struct _patch *tmp_patch = NULL;
struct _patch **new_patches;

for (i = 0; i < mdi->patch_count; i++) {
if (mdi->patches[i]->patchid == patchid) {
Expand All @@ -93,8 +95,14 @@ void _WM_load_patch(struct _mdi *mdi, uint16_t patchid) {
}

mdi->patch_count++;
mdi->patches = (struct _patch **) realloc(mdi->patches,
(sizeof(struct _patch*) * mdi->patch_count));
new_patches = (struct _patch **) realloc(mdi->patches, (sizeof(struct _patch*) * mdi->patch_count));
if (!new_patches) {
free(mdi->patches);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
_WM_Unlock(&_WM_patch_lock);
return;
}
mdi->patches = new_patches;
mdi->patches[mdi->patch_count - 1] = tmp_patch;
tmp_patch->inuse_count++;
_WM_Unlock(&_WM_patch_lock);
Expand Down
35 changes: 29 additions & 6 deletions src/wildmidi_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ static char** WM_LC_Tokenize_Line(char *line_data) {
int line_ofs = 0;
int token_start = 0;
char **token_data = NULL;
char **new_data;
int token_count = 0;

if (!line_length) return (NULL);
Expand All @@ -328,11 +329,13 @@ static char** WM_LC_Tokenize_Line(char *line_data) {
token_start = 1;
if (token_count >= token_data_length) {
token_data_length += TOKEN_CNT_INC;
token_data = (char **) realloc(token_data, token_data_length * sizeof(char *));
if (token_data == NULL) {
new_data = (char **) realloc(token_data, token_data_length * sizeof(char *));
if (!new_data) {
free(token_data);
_WM_GLOBAL_ERROR(WM_ERR_MEM, NULL, errno);
return (NULL);
}
token_data = new_data;
}

token_data[token_count] = &line_data[line_ofs];
Expand All @@ -345,7 +348,13 @@ static char** WM_LC_Tokenize_Line(char *line_data) {
/* if we have found some tokens then add a null token to the end */
if (token_count) {
if (token_count >= token_data_length) {
token_data = (char **) realloc(token_data, ((token_count + 1) * sizeof(char *)));
new_data = (char **) realloc(token_data, ((token_count + 1) * sizeof(char *)));
if (!new_data) {
free(token_data);
_WM_GLOBAL_ERROR(WM_ERR_MEM,"to parse config", errno);
return (NULL);
}
token_data = new_data;
}
token_data[token_count] = NULL;
}
Expand Down Expand Up @@ -838,12 +847,19 @@ static int WM_GetOutput_Linear(midi * handle, int8_t *buffer, uint32_t size) {
memset(buffer, 0, size);

if ( (size / 2) > mdi->mix_buffer_size) {
int32_t *new_mix_buffer;
if ( (size / 2) <= ( mdi->mix_buffer_size * 2 )) {
mdi->mix_buffer_size += MEM_CHUNK;
} else {
mdi->mix_buffer_size = size / 2;
}
mdi->mix_buffer = (int32_t *) realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(int32_t));
new_mix_buffer = (int32_t *) realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(int32_t));
if (!new_mix_buffer){
free(mdi->mix_buffer);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return(-1);
}
mdi->mix_buffer = new_mix_buffer;
}

tmp_buffer = mdi->mix_buffer;
Expand Down Expand Up @@ -1155,12 +1171,19 @@ static int WM_GetOutput_Gauss(midi * handle, int8_t *buffer, uint32_t size) {
memset(buffer, 0, size);

if ( (size / 2) > mdi->mix_buffer_size) {
int32_t *new_mix_buffer;
if ( (size / 2) <= ( mdi->mix_buffer_size * 2 )) {
mdi->mix_buffer_size += MEM_CHUNK;
} else {
mdi->mix_buffer_size = size / 2;
}
mdi->mix_buffer = (int32_t *) realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(int32_t));
new_mix_buffer = (int32_t *) realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(int32_t));
if (!new_mix_buffer){
free(mdi->mix_buffer);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return(-1);
}
mdi->mix_buffer = new_mix_buffer;
}

tmp_buffer = mdi->mix_buffer;
Expand Down Expand Up @@ -2086,7 +2109,7 @@ WildMidi_GetInfo(midi * handle) {
mdi->tmp_info->copyright = NULL;
}
_WM_Unlock(&mdi->lock);
return ((struct _WM_Info *)mdi->tmp_info);
return (mdi->tmp_info);
}

WM_SYMBOL int WildMidi_Shutdown(void) {
Expand Down
8 changes: 7 additions & 1 deletion src/xmi2mid.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ static void copy(struct xmi_ctx *ctx, char *b, uint32_t len)
#define DST_CHUNK 8192
static void resize_dst(struct xmi_ctx *ctx) {
uint32_t pos = ctx->dst_ptr - ctx->dst;
ctx->dst = (uint8_t *) realloc(ctx->dst, ctx->dstsize + DST_CHUNK);
uint8_t *new_dst = (uint8_t *) realloc(ctx->dst, ctx->dstsize + DST_CHUNK);
if (!new_dst){
free(ctx->dst);
_WM_GLOBAL_ERROR(WM_ERR_MEM, "Unable to reallocate memory.", 0);
return;
}
ctx->dst = new_dst;
ctx->dstsize += DST_CHUNK;
ctx->dstrem += DST_CHUNK;
ctx->dst_ptr = ctx->dst + pos;
Expand Down