From 3f48ffb1b6a34f4eb95e4084976acfb6dae6a171 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Mon, 26 Feb 2018 00:14:19 +0100 Subject: [PATCH] Fix two memory leaks in jar_xm.h Found by LeakSanitizer in #494. --- src/external/jar_xm.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/external/jar_xm.h b/src/external/jar_xm.h index df199e06..f7750664 100644 --- a/src/external/jar_xm.h +++ b/src/external/jar_xm.h @@ -2610,6 +2610,7 @@ uint64_t jar_xm_get_remaining_samples(jar_xm_context_t* ctx) int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename) { FILE* xmf; int size; + int ret; xmf = fopen(filename, "rb"); if(xmf == NULL) { @@ -2629,16 +2630,20 @@ int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const } char* data = malloc(size + 1); - if(fread(data, 1, size, xmf) < size) { + if(!data || fread(data, 1, size, xmf) < size) { fclose(xmf); - DEBUG_ERR("fread() failed"); + DEBUG_ERR(data ? "fread() failed" : "malloc() failed"); + free(data); *ctx = NULL; return 5; } fclose(xmf); - switch(jar_xm_create_context_safe(ctx, data, size, rate)) { + ret = jar_xm_create_context_safe(ctx, data, size, rate); + free(data); + + switch(ret) { case 0: break;