Summary: Infer used to report null dereference when field was accessed later: ``` vector<int> v; int& a = v[0]; // should be EMPTY_VECTOR_ACCESS here, but it wasn't reported int b = a; // was NULL_DEREFERENCE here ``` To avoid this problem, model all accesses to vector as dereference of its internal `beginPtr` field. Reviewed By: jberdine Differential Revision: D4481942 fbshipit-source-id: 2142894master
parent
5c12d98d37
commit
40c84077d9
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 <vector>
|
||||
|
||||
// has copy constructor
|
||||
struct WithCopy {
|
||||
int f1;
|
||||
int f2;
|
||||
};
|
||||
|
||||
// no copy constructor - important to catch wrong implementations of std::vector
|
||||
// model
|
||||
struct WithoutCopy {
|
||||
WithoutCopy() = default;
|
||||
WithoutCopy(WithoutCopy&& x) = default;
|
||||
WithoutCopy(const WithoutCopy& x) = delete;
|
||||
WithoutCopy& operator=(const WithoutCopy&) = delete;
|
||||
int f1;
|
||||
int f2;
|
||||
};
|
||||
|
||||
int getIntPtr(int id, std::vector<int*>& v) {
|
||||
int x = v.size();
|
||||
int** res = &v[id];
|
||||
return **res;
|
||||
}
|
||||
|
||||
int getWithCopy(int id, std::vector<WithCopy>& v) {
|
||||
int x = v.size();
|
||||
WithCopy* res = &v[id];
|
||||
return res->f1;
|
||||
}
|
||||
|
||||
int getWithCopyPtr(int id, std::vector<WithCopy*>& v) {
|
||||
int x = v.size();
|
||||
WithCopy** res = &v[id];
|
||||
return (*res)->f1;
|
||||
}
|
||||
|
||||
int getWithoutCopy(int id, std::vector<WithoutCopy>& v) {
|
||||
int x = v.size();
|
||||
WithoutCopy* res = &v[id];
|
||||
return res->f1;
|
||||
}
|
||||
|
||||
int getWithoutCopyPtr(int id, std::vector<WithoutCopy*>& v) {
|
||||
int x = v.size();
|
||||
WithoutCopy** res = &v[id];
|
||||
return (*res)->f1;
|
||||
}
|
Loading…
Reference in new issue