From 44c587400d67cb5ba2a3aeff3fe61d033553cf5b Mon Sep 17 00:00:00 2001 From: James Turner Date: Sun, 5 Sep 2010 14:15:18 +0100 Subject: [PATCH] Validate sound file paths in an XML file. --- simgear/sound/sample_openal.cxx | 10 ++++++++++ simgear/sound/sample_openal.hxx | 4 ++++ simgear/sound/soundmgr_openal.cxx | 12 ++++++++---- simgear/sound/xmlsound.cxx | 7 ++++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index 0a69170c..7b355002 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -220,3 +220,13 @@ string SGSoundSample::random_string() { return rstr; } +SGPath SGSoundSample::file_path() const +{ + if (!_is_file) { + return SGPath(); + } + + return SGPath(_refname); +} + + diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx index 75d09752..491ba572 100644 --- a/simgear/sound/sample_openal.hxx +++ b/simgear/sound/sample_openal.hxx @@ -43,6 +43,8 @@ #include #include +class SGPath; + /** * manages everything we need to know for an individual audio sample */ @@ -89,6 +91,8 @@ public: */ inline bool is_file() const { return _is_file; } + SGPath file_path() const; + /** * Test if this audio sample configuration has changed since the last call. * Calling this function will reset the flag so calling it a second diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 369adede..c3eda45a 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -470,11 +470,15 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample) if ( sample->is_file() ) { int freq, format; size_t size; - bool res; - - res = load(sample_name, &sample_data, &format, &size, &freq); - if (res == false) return buffer; + try { + bool res = load(sample_name, &sample_data, &format, &size, &freq); + if (res == false) return buffer; + } catch (sg_exception& e) { + SG_LOG(SG_GENERAL, SG_ALERT, "failed to load sound buffer:" << e.getFormattedMessage()); + return NO_BUFFER; + } + sample->set_frequency( freq ); sample->set_format( format ); sample->set_size( size ); diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx index 8958af91..616d9514 100644 --- a/simgear/sound/xmlsound.cxx +++ b/simgear/sound/xmlsound.cxx @@ -32,7 +32,8 @@ #include #include #include - +#include +#include #include "xmlsound.hxx" @@ -272,6 +273,10 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, _sgrp = sgrp; } _sample = new SGSoundSample( path.c_str(), node->getStringValue("path", "")); + if (!_sample->file_path().exists()) { + throw sg_io_exception("XML sound: couldn't find file: " + _sample->file_path().str()); + } + _sample->set_relative_position( offset_pos ); _sample->set_direction( dir ); _sample->set_audio_cone( inner, outer, outer_gain ); -- 2.39.5