From 1edcbce35bfe64a8523b49e0a314268bc27512dd Mon Sep 17 00:00:00 2001 From: Jia Chen Date: Fri, 16 Jun 2017 17:14:15 -0700 Subject: [PATCH] Whitelist std::swap in C++ analyses Reviewed By: jeremydubreil Differential Revision: D5269267 fbshipit-source-id: 00a4ba8 --- infer/src/base/Config.ml | 1 + .../tests/codetoanalyze/cpp/errors/issues.exp | 1 + .../codetoanalyze/cpp/errors/models/swap.cpp | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 infer/tests/codetoanalyze/cpp/errors/models/swap.cpp diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 92197e594..5025eb09f 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -236,6 +236,7 @@ let whitelisted_cpp_methods = [ "std::forward"; "std::min"; "std::max"; + "std::swap"; "google::CheckNotNull"; ] diff --git a/infer/tests/codetoanalyze/cpp/errors/issues.exp b/infer/tests/codetoanalyze/cpp/errors/issues.exp index 1b075cdae..0086142d6 100644 --- a/infer/tests/codetoanalyze/cpp/errors/issues.exp +++ b/infer/tests/codetoanalyze/cpp/errors/issues.exp @@ -18,6 +18,7 @@ codetoanalyze/cpp/errors/memory_leaks/object_leak.cpp, object_leak, 0, MEMORY_LE codetoanalyze/cpp/errors/memory_leaks/raii_malloc.cpp, memory_leak, 0, MEMORY_LEAK, [start of procedure memory_leak()] 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/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/swap.cpp b/infer/tests/codetoanalyze/cpp/errors/models/swap.cpp new file mode 100644 index 000000000..37eb4fa45 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/errors/models/swap.cpp @@ -0,0 +1,23 @@ +/* + * 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 + +int swap_null_ok() { + int a = 0; + int *p = nullptr, *q = &a; + std::swap(p, q); + return *p; +} + +int swap_null_bad() { + int a = 0; + int *p = nullptr, *q = &a; + std::swap(p, q); + return *q; +}