scummvm/scummvm — pull request #417 by digitall (2013-12-15 08:31:13+00:00)
Title: ALL: Fix optimization unstable code on checking for null after new.

This Pull Request is to discuss whether this solution is safe across all ports and the impact on the codebase of this issue... especially with respect to exception usage in new operator calls.

These issues were identified by the STACK tool.

By default, the C++ new operator will throw an exception on allocation
failure, rather than returning a null pointer.

The result is that testing the returned pointer for null is redundant
and _may_ be removed by the compiler.

However, we do not use exceptions as they are not supported by all
compilers and may be disabled.

The resulting "pattern" is thus optimization unstable and may result
in unexpected or errorneous behaviour.

To make this stable without removing the null check, you can qualify
the new operator call with std::nothrow to indicate that this should
return a null, rather than throwing an exception.

It is unclear the behaviour of new when -fno-exceptions is active on
the compiler and whether we should instead be redefining "new" as
"new (std::nothrow)" and adding explicit tests for null and handling
code in all cases.