Refactor error handling for memory allocation failure
- Dominant language
- C++
- Stars
- 16
- Forks
- 17
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the refactoring action**
The codebase uses C++'s `new` but handles allocation failures as if it were C's `malloc` by checking if the returned pointer is NULL, while `new` in C++ throws a [std::bad_alloc](https://en.cppreference.com/w/cpp/memory/new/bad_alloc) if it fails, unless the call is marked with [std::no_throw](https://en.cppreference.com/w/cpp/memory/new/nothrow).
**Possible actions**
* Do nothing: If we leave the NULL checking code as is then it just stays as dead code. This is not a particularly high-severity issue IMO
* Add `std::no_throw` to the calls to `new`. This will keep the current error handling code as new will return NULL on failure.
* Remove the error handling code: Allocation failure means something else has gone terribly wrong and there is little that can be done to handle a bad_alloc [[1]](https://stackoverflow.com/questions/1308052/policy-with-catching-stdbad-alloc) [[2]](https://stackoverflow.com/questions/9456728/how-to-deal-with-bad-alloc-in-c), so might as well let it go uncaught and terminate the program. We can replace some of the calls to new with smart pointers where the API permits.
**An overview of misuses:**
Collected by a Python script, may not be complete
```
./src/jni/jni.cpp:
[line:356] pInstance
./src/utils/deployCppService.hpp:
[line:848] singleton_pLogger
./src/utils/runAECpp.cpp:
[line:382] pBuffer
./src/utils/ActiveMQAnalysisEngineService.cpp:
[line:620] connection
[line:638] connection
[line:1290] newConnection
[line:1353] newListener
[line:1378] iv_pgetMetaConnection
[line:1396] newListener
./src/framework/engine.cpp:
[line:215] pResult
./src/framework/taespecifierbuilder.cpp:
[line:186] iv_pXMLErrorHandler
[line:232] iv_pXMLErrorHandler
[line:271] iv_pXMLErrorHandler
[line:313] iv_pXMLErrorHandler
[line:1898] iv_pXMLErrorHandler
[line:1980] iv_pXMLErrorHandler
./src/framework/annotator_context.cpp:
[line:169] iv_pCasPool
./src/framework/internal_primitive_engine.cpp:
[line:102] iv_pAnnotator
./src/framework/resmgr.cpp:
[line:581] pDllFile
[line:670] cv_pclSingletonInstance->iv_fileLogger
./examples/src/ExampleApplication.cpp:
[line:201] pBuffer
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.