From 42c98ec154de4e348fe06d18d84de2faf33992b5 Mon Sep 17 00:00:00 2001 From: Pavel Penev Date: Fri, 11 Dec 2015 02:19:17 +0200 Subject: [PATCH] Don't crash when parsing corrupt sound files Log the exception and move to the next parser. --- OpenRA.Game/FileFormats/AudLoader.cs | 16 +++++++++++++++- OpenRA.Game/FileFormats/WavLoader.cs | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/FileFormats/AudLoader.cs b/OpenRA.Game/FileFormats/AudLoader.cs index 7896a754d0..d7f7bfc9f5 100644 --- a/OpenRA.Game/FileFormats/AudLoader.cs +++ b/OpenRA.Game/FileFormats/AudLoader.cs @@ -175,8 +175,22 @@ namespace OpenRA.FileFormats { channels = sampleBits = sampleRate = 0; - if (!LoadSound(stream, out rawData)) + try + { + if (!LoadSound(stream, out rawData)) + return false; + } + catch (Exception e) + { + // LoadSound() will check if the stream is in a format that this parser supports. + // If not, it will simply return false so we know we can't use it. If it is, it will start + // parsing the data without any further failsafes, which means that it will crash on corrupted files + // (that end prematurely or otherwise don't conform to the specifications despite the headers being OK). + Log.Write("debug", "Failed to parse AUD file {0}. Error message:".F(fileName)); + Log.Write("debug", e.ToString()); + rawData = null; return false; + } channels = 1; sampleBits = 16; diff --git a/OpenRA.Game/FileFormats/WavLoader.cs b/OpenRA.Game/FileFormats/WavLoader.cs index 6a3518fa5a..3f7d3fba2a 100644 --- a/OpenRA.Game/FileFormats/WavLoader.cs +++ b/OpenRA.Game/FileFormats/WavLoader.cs @@ -190,8 +190,21 @@ namespace OpenRA.FileFormats rawData = null; channels = sampleBits = sampleRate = 0; - if (!LoadSound(stream)) + try + { + if (!LoadSound(stream)) + return false; + } + catch (Exception e) + { + // LoadSound() will check if the stream is in a format that this parser supports. + // If not, it will simply return false so we know we can't use it. If it is, it will start + // parsing the data without any further failsafes, which means that it will crash on corrupted files + // (that end prematurely or otherwise don't conform to the specifications despite the headers being OK). + Log.Write("debug", "Failed to parse WAV file {0}. Error message:".F(fileName)); + Log.Write("debug", e.ToString()); return false; + } rawData = RawOutput; channels = Channels;