From 6d2e060deb924a1634a01a2483913a2471d1d688 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 9 Jan 2023 11:40:32 +1100 Subject: [PATCH] waf: add -fcheck-new to g++ build this ensures the compiler doesn't assume that new always returns a non-NULL value. Without this the compiler may remove the error path in code like this: ``` MyObject *x = new MyObject; if (x == nullptr) { ::printf("Alloc failed\n"); } ``` the reason it can do this is the new operator is marked as throwing an exception on failure, which means the error path is unreachable. As we don't have C++ exceptions in ArduPilot could (and do!) have code that ends up losing protection against allocation failures --- Tools/ardupilotwaf/boards.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tools/ardupilotwaf/boards.py b/Tools/ardupilotwaf/boards.py index 2c2aa89ad7..832cd187dd 100644 --- a/Tools/ardupilotwaf/boards.py +++ b/Tools/ardupilotwaf/boards.py @@ -249,6 +249,9 @@ class Board: env.CXXFLAGS += [ '-Werror=implicit-fallthrough', ] + env.CXXFLAGS += [ + '-fcheck-new', + ] if cfg.env.DEBUG: env.CFLAGS += [