diff --git a/infer/models/cpp/src/throw_wrappers.cpp b/infer/models/cpp/src/throw_wrappers.cpp new file mode 100644 index 000000000..9f63f8ec4 --- /dev/null +++ b/infer/models/cpp/src/throw_wrappers.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +#include +#include +#include +#include +#include +#include + +namespace std { + +void __throw_bad_alloc() { throw bad_alloc(); } +void __throw_bad_cast() { throw bad_cast(); } +void __throw_bad_exception() { throw bad_exception(); } +void __throw_bad_function_call() { throw bad_function_call(); } +void __throw_bad_typeid() { throw bad_typeid(); } +void __throw_domain_error(const char* s) { throw domain_error(s); } +void __throw_invalid_argument(const char* s) { throw invalid_argument(s); } +void __throw_ios_failure(const char* s) { throw ios_base::failure(s); } +void __throw_length_error(const char* s) { throw length_error(s); } +void __throw_logic_error(const char* s) { throw logic_error(s); } +void __throw_out_of_range(const char* s) { throw out_of_range(s); } +void __throw_overflow_error(const char* s) { throw overflow_error(s); } +void __throw_range_error(const char* s) { throw range_error(s); } +void __throw_runtime_error(const char* s) { throw runtime_error(s); } +void __throw_system_error(int c) { + throw system_error(error_code(c, generic_category())); +} +void __throw_underflow_error(const char* s) { throw underflow_error(s); } +} diff --git a/infer/tests/codetoanalyze/cpp/errors/issues.exp b/infer/tests/codetoanalyze/cpp/errors/issues.exp index 0086142d6..cfea36f93 100644 --- a/infer/tests/codetoanalyze/cpp/errors/issues.exp +++ b/infer/tests/codetoanalyze/cpp/errors/issues.exp @@ -19,6 +19,7 @@ codetoanalyze/cpp/errors/memory_leaks/raii_malloc.cpp, memory_leak, 0, MEMORY_LE codetoanalyze/cpp/errors/models/move.cpp, move::div0_moved_from, 3, DIVIDE_BY_ZERO, [start of procedure move::div0_moved_from(),start of procedure X,return from a call to move::X_X,start of procedure X,return from a call to move::X_X] codetoanalyze/cpp/errors/models/move.cpp, move::div0_moved_to, 3, DIVIDE_BY_ZERO, [start of procedure move::div0_moved_to(),start of procedure X,return from a call to move::X_X,start of procedure X,return from a call to move::X_X] codetoanalyze/cpp/errors/models/swap.cpp, swap_null_bad, 4, NULL_DEREFERENCE, [start of procedure swap_null_bad()] +codetoanalyze/cpp/errors/models/throw_wrapper.cpp, nothrow_if_null_bad, 4, NULL_DEREFERENCE, [start of procedure nothrow_if_null_bad(),start of procedure get_null(),return from a call to get_null,Condition is true,start of procedure do_nothing(),return from a call to do_nothing] codetoanalyze/cpp/errors/mutex/std_mutex.cpp, alarm1, 2, DOUBLE_LOCK, [start of procedure alarm1()] codetoanalyze/cpp/errors/mutex/std_mutex.cpp, alarm2, 2, DOUBLE_LOCK, [start of procedure alarm2()] codetoanalyze/cpp/errors/mutex/std_mutex.cpp, alarm2, 2, DOUBLE_LOCK, [start of procedure alarm2()] diff --git a/infer/tests/codetoanalyze/cpp/errors/models/throw_wrapper.cpp b/infer/tests/codetoanalyze/cpp/errors/models/throw_wrapper.cpp new file mode 100644 index 000000000..63803ef83 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/errors/models/throw_wrapper.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +// std::__throw_bad_alloc() is not a standarized function. It could be defined +// in many places, so we need to consider all possibilities. +#include +#include + +static int* get_null() { return nullptr; } + +static void do_nothing() {} + +int throw_if_null_ok() { + int* p = get_null(); + if (p == nullptr) + std::__throw_bad_alloc(); + return *p; +} + +int nothrow_if_null_bad() { + int* p = get_null(); + if (p == nullptr) + do_nothing(); + return *p; +} diff --git a/infer/tests/codetoanalyze/cpp/shared/types/typeid_expr.cpp.dot b/infer/tests/codetoanalyze/cpp/shared/types/typeid_expr.cpp.dot index cd04bd0bb..f809aca2d 100644 --- a/infer/tests/codetoanalyze/cpp/shared/types/typeid_expr.cpp.dot +++ b/infer/tests/codetoanalyze/cpp/shared/types/typeid_expr.cpp.dot @@ -358,21 +358,15 @@ digraph iCFG { "Person#Person#{_ZN6PersonC1ERKS_|constexpr}.723fccb56b807554fd33d1118dcb83e1_2" [label="2: Exit Person_Person \n " color=yellow style=filled] -"bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_1" [label="1: Start std::bad_exception_bad_exception\nFormals: this:std::bad_exception*\nLocals: \n DECLARE_LOCALS(&return); [line 103]\n " color=yellow style=filled] +"bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_1" [label="1: Start std::bad_exception_bad_exception\nFormals: this:std::bad_exception*\nLocals: \n " color=yellow style=filled] - "bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_1" -> "bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_3" ; "bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_2" [label="2: Exit std::bad_exception_bad_exception \n " color=yellow style=filled] -"bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_3" [label="3: Constructor Init \n n$0=*&this:std::bad_exception* [line 103]\n _fun_std::exception_exception(n$0:std::bad_exception*) [line 103]\n " shape="box"] +"exception#exception#std#{_ZNSt9exceptionC1Ev}.5226a0e6cc026fc29eb750a66d588910_1" [label="1: Start std::exception_exception\nFormals: this:std::exception*\nLocals: \n " color=yellow style=filled] - "bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_3" -> "bad_exception#bad_exception#std#{_ZNSt13bad_exceptionC1Ev}.9b3ad9f8b08e34cb77dd347cfc0925a2_2" ; -"exception#exception#std#{_ZNSt9exceptionC1Ev}.5226a0e6cc026fc29eb750a66d588910_1" [label="1: Start std::exception_exception\nFormals: this:std::exception*\nLocals: \n DECLARE_LOCALS(&return); [line 94]\n " color=yellow style=filled] - - - "exception#exception#std#{_ZNSt9exceptionC1Ev}.5226a0e6cc026fc29eb750a66d588910_1" -> "exception#exception#std#{_ZNSt9exceptionC1Ev}.5226a0e6cc026fc29eb750a66d588910_2" ; "exception#exception#std#{_ZNSt9exceptionC1Ev}.5226a0e6cc026fc29eb750a66d588910_2" [label="2: Exit std::exception_exception \n " color=yellow style=filled]