Fix exception handling of 'new' operator. 99/131399/4
authorKimJeongYeon <jeongyeon.kim@samsung.com>
Mon, 29 May 2017 02:38:53 +0000 (11:38 +0900)
committerSeungbae Shin <seungbae.shin@samsung.com>
Tue, 25 Jul 2017 07:08:06 +0000 (07:08 +0000)
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 <jeongyeon.kim@samsung.com>
Change-Id: Ibdd9c4d93273d1a0874eee4cdb5056ff5ae35407

packaging/capi-media-audio-io.spec
src/cpp/CAudioInput.cpp
src/cpp/CAudioOutput.cpp
src/cpp/cpp_audio_io.cpp

index 0710006..3a543ee 100644 (file)
@@ -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
index 8b64493..2e46907 100644 (file)
@@ -15,6 +15,8 @@
  */
 
 
+#include <new>
+
 #include <pulse/pulseaudio.h>
 #include "CAudioIODef.h"
 #include <sched.h>
@@ -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");
     }
 }
 
index df64c48..54b921d 100644 (file)
@@ -15,6 +15,8 @@
  */
 
 
+#include <new>
+
 #include "CAudioIODef.h"
 #include <sched.h>
 
@@ -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");
     }
 }
 
index 50a2408..f0a6052 100644 (file)
@@ -15,6 +15,8 @@
  */
 
 
+#include <new>
+
 #include "cpp_audio_io.h"
 #include <sound_manager_internal.h>
 #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);
     }