From a819ca770f3f7617f28c43727803faec255644cf Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 14 Jul 2020 21:48:30 +1000 Subject: [PATCH] Don't crash if out of memory when allocating samples for writing .wav file --- src/Logger.c | 6 ++++-- src/Resources.c | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Logger.c b/src/Logger.c index 96d14e996..430a06897 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -111,7 +111,8 @@ void Logger_SysWarn(cc_result res, const char* place, Logger_DescribeError descr String msg; char msgBuffer[256]; String_InitArray(msg, msgBuffer); - String_Format2(&msg, "Error %i when %c", &res, place); + String_Format2(&msg, res < 20000 ? "Error %i when %c" : "Error %h when %c", + &res, place); AppendErrorDesc(&msg, res, describeErr); Logger_WarnFunc(&msg); } @@ -120,7 +121,8 @@ void Logger_SysWarn2(cc_result res, const char* place, const String* path, Logge String msg; char msgBuffer[256]; String_InitArray(msg, msgBuffer); - String_Format3(&msg, "Error %h when %c '%s'", &res, place, path); + String_Format3(&msg, res < 20000 ? "Error %i when %c '%s'" : "Error %h when %c '%s'", + &res, place, path); AppendErrorDesc(&msg, res, describeErr); Logger_WarnFunc(&msg); } diff --git a/src/Resources.c b/src/Resources.c index 14d8584ba..4797f4136 100644 --- a/src/Resources.c +++ b/src/Resources.c @@ -647,7 +647,9 @@ static void SoundPatcher_WriteWav(struct Stream* s, struct VorbisState* ctx) { res = Vorbis_DecodeHeaders(ctx); if (res) { Logger_Warn(res, "decoding .ogg header"); return; } - samples = (cc_int16*)Mem_Alloc(ctx->blockSizes[1] * ctx->channels, 2, ".ogg samples"); + + samples = (cc_int16*)Mem_TryAlloc(ctx->blockSizes[1] * ctx->channels, 2); + if (!samples) { Logger_Warn(ERR_OUT_OF_MEMORY, "allocating .ogg samples"); return; } for (;;) { res = Vorbis_DecodeFrame(ctx);