Skip to content

Commit

Permalink
[website] Clarify issue typeVECTOR_INVALIDATION example with pointe…
Browse files Browse the repository at this point in the history
…r assignment (#1830)

Summary:
This PR updates the VECTOR_INVALIDATION documentation example to include a missing line of code that explicitly assigns a pointer. The added line `int* y = elt;` clarifies the example by showing the exact pointer that is dereferenced after the vector modification. The missing line comes from the testcase file `infer/tests/codetoanalyze/cpp/pulse/vector.cpp`

## Motivation
The current documentation example might be confusing for readers, as it dereferences a pointer `y` which was not explicitly assigned in the code snippet. Including the pointer assignment makes the example clearer and demonstrates the potential invalidation issue more effectively.

## Changes
- Added `int* y = elt;` in the VECTOR_INVALIDATION example.
- Updated across four documentation files to maintain consistency.

Thank you for considering this documentation improvement.

Pull Request resolved: #1830

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

$ make doc-publish
website$ yarn start

Reviewed By: ngorogiannis

Differential Revision: D56309428

Pulled By: skcho

fbshipit-source-id: 3f2c1f30b6c4c1339735c78875fb26811c4b12c1
  • Loading branch information
shenjunjiekoda authored and facebook-github-bot committed Apr 19, 2024
1 parent 2570f82 commit 731d945
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion infer/documentation/issues/INEFFICIENT_KEYSET_ITERATOR.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This issue is raised when
- iterating over a HashMap with `ketSet()` iterator
- iterating over a HashMap with `keySet()` iterator
- looking up the key each time

Example:
Expand Down
2 changes: 1 addition & 1 deletion infer/documentation/issues/PARAMETER_NOT_NULL_CHECKED.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ as an argument. Therefore it is only a warning. For example:

```objectivec
-(int) foo:(A* a) {
B b* = [a foo]; // sending a message with receiver nil returns nil
B *b = [a foo]; // sending a message with receiver nil returns nil
return b->x; // dereferencing b, potential NPE if you pass nil as the argument a.
}
```
Expand Down
3 changes: 2 additions & 1 deletion infer/documentation/issues/VECTOR_INVALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ For example:
```cpp
void deref_vector_element_after_push_back_bad(std::vector<int>& vec) {
int* elt = &vec[1];
int* y = elt;
vec.push_back(42); // if the array backing the vector was full already, this
// will re-allocate it and copy the previous contents
// into the new array, then delete the previous array
std::cout << *y << "\n"; // bad: elt might be invalid
std::cout << *y << "\n"; // bad: y might be invalid
}
```
5 changes: 3 additions & 2 deletions website/docs/all-issue-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ void makeAllZero_impure(ArrayList<Foo> list) {
Reported as "Inefficient Keyset Iterator" by [inefficient-keyset-iterator](/docs/next/checker-inefficient-keyset-iterator).

This issue is raised when
- iterating over a HashMap with `ketSet()` iterator
- iterating over a HashMap with `keySet()` iterator
- looking up the key each time

Example:
Expand Down Expand Up @@ -2931,10 +2931,11 @@ For example:
```cpp
void deref_vector_element_after_push_back_bad(std::vector<int>& vec) {
int* elt = &vec[1];
int* y = elt;
vec.push_back(42); // if the array backing the vector was full already, this
// will re-allocate it and copy the previous contents
// into the new array, then delete the previous array
std::cout << *y << "\n"; // bad: elt might be invalid
std::cout << *y << "\n"; // bad: y might be invalid
}
```
Expand Down
9 changes: 5 additions & 4 deletions website/versioned_docs/version-1.0.0/all-issue-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ void makeAllZero_impure(ArrayList<Foo> list) {
Reported as "Inefficient Keyset Iterator" by [inefficient-keyset-iterator](/docs/1.0.0/checker-inefficient-keyset-iterator).

This issue is raised when
- iterating over a HashMap with `ketSet()` iterator
- iterating over a HashMap with `keySet()` iterator
- looking up the key each time

Instead, it is more efficient to iterate over the loop with `entrySet` which returns key-vaue pairs and gets rid of the hashMap lookup.
Expand Down Expand Up @@ -1070,7 +1070,7 @@ parameter is `nil`. For example:

```objectivec
-(int) foo {
B b* = [self->_a foo]; // sending a message with receiver nil returns nil
B *b = [self->_a foo]; // sending a message with receiver nil returns nil
return b->x; // dereferencing b, potential NPE if you pass nil as the argument a.
}
```
Expand Down Expand Up @@ -1323,7 +1323,7 @@ as an argument. Therefore it is only a warning. For example:

```objectivec
-(int) foo:(A* a) {
B b* = [a foo]; // sending a message with receiver nil returns nil
B *b = [a foo]; // sending a message with receiver nil returns nil
return b->x; // dereferencing b, potential NPE if you pass nil as the argument a.
}
```
Expand Down Expand Up @@ -2112,10 +2112,11 @@ For example:
```C++
void deref_vector_element_after_push_back_bad(std::vector<int>& vec) {
int* elt = &vec[1];
int* y = elt;
vec.push_back(42); // if the array backing the vector was full already, this
// will re-allocate it and copy the previous contents
// into the new array, then delete the previous array
std::cout << *y << "\n"; // bad: elt might be invalid
std::cout << *y << "\n"; // bad: y might be invalid
}
```
Expand Down
9 changes: 5 additions & 4 deletions website/versioned_docs/version-1.1.0/all-issue-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ void makeAllZero_impure(ArrayList<Foo> list) {
Reported as "Inefficient Keyset Iterator" by [inefficient-keyset-iterator](/docs/checker-inefficient-keyset-iterator).

This issue is raised when
- iterating over a HashMap with `ketSet()` iterator
- iterating over a HashMap with `keySet()` iterator
- looking up the key each time

Instead, it is more efficient to iterate over the loop with `entrySet` which returns key-vaue pairs and gets rid of the hashMap lookup.
Expand Down Expand Up @@ -1154,7 +1154,7 @@ parameter is `nil`. For example:

```objectivec
-(int) foo {
B b* = [self->_a foo]; // sending a message with receiver nil returns nil
B *b = [self->_a foo]; // sending a message with receiver nil returns nil
return b->x; // dereferencing b, potential NPE if you pass nil as the argument a.
}
```
Expand Down Expand Up @@ -1481,7 +1481,7 @@ as an argument. Therefore it is only a warning. For example:
```objectivec
-(int) foo:(A* a) {
B b* = [a foo]; // sending a message with receiver nil returns nil
B *b = [a foo]; // sending a message with receiver nil returns nil
return b->x; // dereferencing b, potential NPE if you pass nil as the argument a.
}
```
Expand Down Expand Up @@ -2275,10 +2275,11 @@ For example:
```C++
void deref_vector_element_after_push_back_bad(std::vector<int>& vec) {
int* elt = &vec[1];
int* y = elt;
vec.push_back(42); // if the array backing the vector was full already, this
// will re-allocate it and copy the previous contents
// into the new array, then delete the previous array
std::cout << *y << "\n"; // bad: elt might be invalid
std::cout << *y << "\n"; // bad: y might be invalid
}
```
Expand Down

0 comments on commit 731d945

Please sign in to comment.