From 5847dd3fe9a17694878022e9114bacdbc77b88ed Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Mon, 7 Aug 2017 05:19:48 -0700 Subject: [PATCH] [C++] Fix model of std::vector::size Summary: It wasn't using code from `std::vector::empty` which recently was improved. Instead of inlining `std::vector::empty`, call it to know whether vector is empty or not. Reviewed By: jvillard Differential Revision: D5573379 fbshipit-source-id: e024a42 --- infer/models/cpp/include/infer_model/vector.h | 6 +++--- infer/tests/codetoanalyze/cpp/errors/vector/loop.cpp | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/infer/models/cpp/include/infer_model/vector.h b/infer/models/cpp/include/infer_model/vector.h index a25fcef6f..29188e4e0 100644 --- a/infer/models/cpp/include/infer_model/vector.h +++ b/infer/models/cpp/include/infer_model/vector.h @@ -237,10 +237,10 @@ class vector { const_reverse_iterator crend() const noexcept { return rend(); } size_type size() const noexcept { - if (beginPtr) { - return 10; + if (empty()) { + return 0; } - return 0; + return 10; } size_type capacity() const noexcept {} diff --git a/infer/tests/codetoanalyze/cpp/errors/vector/loop.cpp b/infer/tests/codetoanalyze/cpp/errors/vector/loop.cpp index 653c9b9d6..74bafe889 100644 --- a/infer/tests/codetoanalyze/cpp/errors/vector/loop.cpp +++ b/infer/tests/codetoanalyze/cpp/errors/vector/loop.cpp @@ -8,7 +8,7 @@ */ #include -void foreach_access_ok(std::vector& vec) { +void foreach_access1_ok(std::vector& vec) { if (vec.empty()) { // do nothing } @@ -17,6 +17,13 @@ void foreach_access_ok(std::vector& vec) { } } +void foreach_access2_ok(std::vector& vec) { + int s = vec.size(); + for (const auto& elem : vec) { + auto r = elem; + } +} + void iterator_for_access_ok(std::vector& vec) { if (vec.empty()) { // do nothing