From cb4950fb461e27453fb8b6767679aff311756270 Mon Sep 17 00:00:00 2001 From: KimJeongYeon Date: Mon, 29 May 2017 11:38:53 +0900 Subject: [PATCH] Fix exception handling of 'new' operator. If it fails memory allocation by calling operator 'new', 'std:bad_alloc' exception will throw. Therefore, never check null-pointer at next line. These situations cause potential leak also. This patch uses exception handling of 'new' operator instead of checking null-pointer. [Version] 0.3.77 [Profile] Common [Issue Type] Bug Signed-off-by: KimJeongYeon Change-Id: Ibdd9c4d93273d1a0874eee4cdb5056ff5ae35407 --- packaging/capi-media-audio-io.spec | 2 +- src/cpp/CAudioInput.cpp | 15 ++-- src/cpp/CAudioOutput.cpp | 16 ++--- src/cpp/cpp_audio_io.cpp | 109 +++++++++++------------------ 4 files changed, 58 insertions(+), 84 deletions(-) diff --git a/packaging/capi-media-audio-io.spec b/packaging/capi-media-audio-io.spec index 0710006..3a543ee 100644 --- a/packaging/capi-media-audio-io.spec +++ b/packaging/capi-media-audio-io.spec @@ -1,6 +1,6 @@ Name: capi-media-audio-io Summary: An Audio Input & Audio Output library in Tizen Native API -Version: 0.3.76 +Version: 0.3.77 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/cpp/CAudioInput.cpp b/src/cpp/CAudioInput.cpp index 8b64493..2e46907 100644 --- a/src/cpp/CAudioInput.cpp +++ b/src/cpp/CAudioInput.cpp @@ -15,6 +15,8 @@ */ +#include + #include #include "CAudioIODef.h" #include @@ -213,9 +215,6 @@ void CAudioInput::initialize() throw(CAudioError) { // Create ASM Handler mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE, mAudioInfo, this); - if (mpAudioSessionHandler == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object"); - } // Initialize ASM Handler mpAudioSessionHandler->initialize(); @@ -225,6 +224,9 @@ void CAudioInput::initialize() throw(CAudioError) { } catch (CAudioError err) { finalize(); throw err; + } catch (std::bad_alloc&) { + finalize(); + THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object"); } } @@ -280,10 +282,6 @@ void CAudioInput::prepare() throw(CAudioError) { // Create PulseAudio Handler mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this); - if (mpPulseAudioClient == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, - "Failed to allocate CPulseAudioClient object"); - } // Initialize PulseAudio Handler mpPulseAudioClient->initialize(); @@ -298,6 +296,9 @@ void CAudioInput::prepare() throw(CAudioError) { } catch (CAudioError e) { internalUnlock(); throw e; + } catch (std::bad_alloc&) { + internalUnlock(); + THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object"); } } diff --git a/src/cpp/CAudioOutput.cpp b/src/cpp/CAudioOutput.cpp index df64c48..54b921d 100644 --- a/src/cpp/CAudioOutput.cpp +++ b/src/cpp/CAudioOutput.cpp @@ -15,6 +15,8 @@ */ +#include + #include "CAudioIODef.h" #include @@ -105,10 +107,6 @@ void CAudioOutput::initialize() throw(CAudioError) { // Create ASM Handler mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_PLAYBACK, mAudioInfo, this); - if (mpAudioSessionHandler == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, - "Failed to allocate CAudioSessionHandler object"); - } // Initialize ASM Handler mpAudioSessionHandler->initialize(); @@ -118,6 +116,9 @@ void CAudioOutput::initialize() throw(CAudioError) { } catch (CAudioError err) { finalize(); throw err; + } catch (std::bad_alloc&) { + finalize(); + THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object"); } } @@ -172,10 +173,6 @@ void CAudioOutput::prepare() throw(CAudioError) { // Create PulseAudio Handler mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK, spec, this); - if (mpPulseAudioClient == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, - "Failed to allocate CPulseAudioClient object"); - } // Initialize PulseAudio Handler mpPulseAudioClient->initialize(); @@ -189,6 +186,9 @@ void CAudioOutput::prepare() throw(CAudioError) { } catch (CAudioError e) { internalUnlock(); throw e; + } catch (std::bad_alloc&) { + internalUnlock(); + THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object"); } } diff --git a/src/cpp/cpp_audio_io.cpp b/src/cpp/cpp_audio_io.cpp index 50a2408..f0a6052 100644 --- a/src/cpp/cpp_audio_io.cpp +++ b/src/cpp/cpp_audio_io.cpp @@ -15,6 +15,8 @@ */ +#include + #include "cpp_audio_io.h" #include #include "audio_io.h" @@ -376,6 +378,21 @@ static audio_io_interrupted_code_e __convert_interrupted_code(IAudioSessionEvent } } +static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) { + VALID_POINTER_START(handle) + SAFE_FINALIZE(handle->audioIoHandle); + SAFE_DELETE(handle->audioIoHandle); + SAFE_DELETE(handle); + VALID_POINTER_END + + VALID_POINTER_START(obj) + if (is_output) + *(audio_out_h *)obj = NULL; + else + *(audio_in_h *)obj = NULL; + VALID_POINTER_END +} + /** * Implements CAPI functions */ @@ -399,33 +416,22 @@ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_t } handle = new audio_io_s; - if (handle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle"); - } CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type); handle->audioIoHandle = new CAudioInput(audioInfo); - if (handle->audioIoHandle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle"); - } handle->audioIoHandle->initialize(); *input = handle; } catch (CAudioError e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - - VALID_POINTER_START(handle) - SAFE_FINALIZE(handle->audioIoHandle); - SAFE_DELETE(handle->audioIoHandle); - SAFE_DELETE(handle); - VALID_POINTER_END - - VALID_POINTER_START(input) - *input = NULL; - VALID_POINTER_END - + __handle_safe_free(handle, (void *)input, false); + return __convert_CAudioError(e); + } catch (std::bad_alloc&) { + CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY; + AUDIO_IO_LOGE("Failed to allocate handle"); + __handle_safe_free(handle, (void *)input, false); return __convert_CAudioError(e); } @@ -443,33 +449,22 @@ int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio __check_audio_param(sample_rate, channel, type); handle = new audio_io_s; - if (handle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle"); - } CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type); handle->audioIoHandle = new CAudioInput(audioInfo); - if (handle->audioIoHandle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle"); - } handle->audioIoHandle->initialize(); *input = handle; } catch (CAudioError e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - - VALID_POINTER_START(handle) - SAFE_FINALIZE(handle->audioIoHandle); - SAFE_DELETE(handle->audioIoHandle); - SAFE_DELETE(handle); - VALID_POINTER_END - - VALID_POINTER_START(input) - *input = NULL; - VALID_POINTER_END - + __handle_safe_free(handle, (void *)input, false); + return __convert_CAudioError(e); + } catch (std::bad_alloc&) { + CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY; + AUDIO_IO_LOGE("Failed to allocate handle"); + __handle_safe_free(handle, (void *)input, false); return __convert_CAudioError(e); } @@ -1028,33 +1023,22 @@ int cpp_audio_out_create(int sample_rate, audio_channel_e channel, audio_sample_ __check_audio_param(sample_rate, channel, type, sound_type); handle = new audio_io_s; - if (handle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle"); - } CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type); handle->audioIoHandle = new CAudioOutput(audioInfo); - if (handle->audioIoHandle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle"); - } handle->audioIoHandle->initialize(); *output = handle; } catch (CAudioError e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - - VALID_POINTER_START(handle) - SAFE_FINALIZE(handle->audioIoHandle); - SAFE_DELETE(handle->audioIoHandle); - SAFE_DELETE(handle); - VALID_POINTER_END - - VALID_POINTER_START(output) - *output = NULL; - VALID_POINTER_END - + __handle_safe_free(handle, (void *)output, true); + return __convert_CAudioError(e); + } catch (std::bad_alloc&) { + CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY; + AUDIO_IO_LOGE("Failed to allocate handle"); + __handle_safe_free(handle, (void *)output, true); return __convert_CAudioError(e); } @@ -1072,33 +1056,22 @@ int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sam __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */); handle = new audio_io_s; - if (handle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle"); - } CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA); handle->audioIoHandle = new CAudioOutput(audioInfo); - if (handle->audioIoHandle == NULL) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle"); - } handle->audioIoHandle->initialize(); *output = handle; } catch (CAudioError e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - - VALID_POINTER_START(handle) - SAFE_FINALIZE(handle->audioIoHandle); - SAFE_DELETE(handle->audioIoHandle); - SAFE_DELETE(handle); - VALID_POINTER_END - - VALID_POINTER_START(output) - *output = NULL; - VALID_POINTER_END - + __handle_safe_free(handle, (void *)output, true); + return __convert_CAudioError(e); + } catch (std::bad_alloc&) { + CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY; + AUDIO_IO_LOGE("Failed to allocate handle"); + __handle_safe_free(handle, (void *)output, true); return __convert_CAudioError(e); } -- 2.34.1