diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 5d3180a8d..09381283e 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -208,6 +208,8 @@ let whitelisted_cpp_methods = [ let whitelisted_cpp_classes = [ ["std"; "__less"]; + ["std"; "__wrap_iter"]; (* libc++ internal name of vector iterator *) + ["std"; "__normal_iterator"]; (* libstdc++ internal name of vector iterator *) ] diff --git a/infer/tests/codetoanalyze/cpp/errors/issues.exp b/infer/tests/codetoanalyze/cpp/errors/issues.exp index 432383706..95600a4e9 100644 --- a/infer/tests/codetoanalyze/cpp/errors/issues.exp +++ b/infer/tests/codetoanalyze/cpp/errors/issues.exp @@ -128,6 +128,7 @@ codetoanalyze/cpp/errors/vector/empty_access.cpp, size_check0_empty, 2, EMPTY_VE codetoanalyze/cpp/errors/vector/empty_access.cpp, vector_as_param_by_value_empty, 2, EMPTY_VECTOR_ACCESS, [start of procedure vector_as_param_by_value_empty(),start of procedure vector_param_by_value_access()] codetoanalyze/cpp/errors/vector/empty_access.cpp, vector_as_param_clear, 3, EMPTY_VECTOR_ACCESS, [start of procedure vector_as_param_clear(),start of procedure vector_param_clear(),return from a call to vector_param_clear] codetoanalyze/cpp/errors/vector/empty_access.cpp, vector_as_param_empty, 2, EMPTY_VECTOR_ACCESS, [start of procedure vector_as_param_empty(),start of procedure vector_param_access()] +codetoanalyze/cpp/errors/vector/iterator_access.cpp, iterator_access::possible_npe, 4, NULL_DEREFERENCE, [start of procedure iterator_access::possible_npe(),Condition is true,Condition is true,Condition is true] codetoanalyze/cpp/shared/attributes/deprecated_hack.cpp, derefFirstArg2_null_deref, 2, NULL_DEREFERENCE, [start of procedure derefFirstArg2_null_deref()] codetoanalyze/cpp/shared/attributes/deprecated_hack.cpp, derefFirstArg3_null_deref, 2, NULL_DEREFERENCE, [start of procedure derefFirstArg3_null_deref(),start of procedure derefFirstArg3()] codetoanalyze/cpp/shared/attributes/deprecated_hack.cpp, derefFirstArg_null_deref, 2, NULL_DEREFERENCE, [start of procedure derefFirstArg_null_deref()] diff --git a/infer/tests/codetoanalyze/cpp/errors/vector/iterator_access.cpp b/infer/tests/codetoanalyze/cpp/errors/vector/iterator_access.cpp new file mode 100644 index 000000000..dc0dcb663 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/errors/vector/iterator_access.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 - 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 + +namespace iterator_access { +struct X { + int id; +}; + +int possible_npe(std::vector in) { + int* x = nullptr; + for (auto iter = in.begin(); iter != in.end(); ++iter) { + if (iter->id >= 0 && iter->id <= 0) { + return *x; + } + } + return 1; +} + +int impossible_npe(std::vector in) { + int* x = nullptr; + for (auto iter = in.begin(); iter != in.end(); ++iter) { + if (iter->id > 0 && iter->id <= 0) { + return *x; + } + } + return 1; +} +}