Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A8-4-7: Regression with reference types #82

Closed
jsinglet opened this issue Sep 12, 2022 · 5 comments · Fixed by #562
Closed

A8-4-7: Regression with reference types #82

jsinglet opened this issue Sep 12, 2022 · 5 comments · Fixed by #562
Assignees
Labels
Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Low

Comments

@jsinglet
Copy link
Contributor

jsinglet commented Sep 12, 2022

Affected rules

  • A8-4-7

Description

A false positive has been reported for the example below.

Additionally:

Example

class A8_4_7 {
 public:
  std::array<char, 20UL> values;
};
void output(const A8_4_7 &a847) noexcept { // trivial to copy but size is larger than 2 words. (false positive)
  std::cout << a847.values[0] << std::endl;
}```
@jsinglet jsinglet added the false positive/false negative An issue related to observed false positives or false negatives. label Sep 12, 2022
@jsinglet jsinglet self-assigned this Sep 12, 2022
@rcseacord
Copy link
Contributor

For background, this AUTOSAR rule seems poorly conceived. The rule is strictly a performance rule. The rule states that "passing an argument by value documents that the argument won't be modified" but unlike passing by reference to cost eliminates indirection in the function body. From a safety perspective, there is no real advantage in passing by value over passing by reference to const. I'm not sure enforcing this rule adds much if any value, and of course, changing code to comply with a rule always costs efforts and may introduce additional defects.

@rcseacord
Copy link
Contributor

The enforcement guidance on which this rule is based "F.16: For “in” parameters, pass cheaply-copied types by value and others by reference to const" says:

  • (Simple) ((Foundation)) Warn when a parameter being passed by value has a size greater than 2 * sizeof(void*). Suggest using a reference to const instead.
  • (Simple) ((Foundation)) Warn when a parameter passed by reference to const has a size less than 2 * sizeof(void*). Suggest passing by value instead.
  • (Simple) ((Foundation)) Warn when a parameter passed by reference to const is moved.

This is fairly different enforcement from what we currently have.

@rcseacord
Copy link
Contributor

In the example, while the array is trivially copyable it is likely larger than 2 * sizeof(void*) which is an implementation-defined value so this clearly seems to be a false positive. A larger question is if this required rule should be enforced at all.

@jsinglet
Copy link
Contributor Author

Agreed @rcseacord. Testing locally the computed size is 20 bytes and the word size is 8 bytes. So since it is longer than 2 words it is not eligible to be passed by value (under this rule) so it is clearly a false positive, trivially copyable or not.

Some more notes on what needs to be fixed:

  • Needs to factor in definition of trivial to copy / identify the cases where that makes a difference.
  • The calculation of size needs a call to v.getType().stripType().getSize() to ensure that the correct size is begin calculated.

Example:
Calling getType().getSize() on const A8_4_7 &a847 will yield 8, when what is expected is 20.

  • Reference types are explicitly excluded in the query -- There is an edge case that was introduced (the catch block clause) but the exclusion of all reference types is too broad since some will need to be flagged. For example:
struct A { std::uint32_t a; };

void f1(const A &a){} // this should be flagged 

@jsinglet jsinglet added Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address Impact-Low labels Sep 13, 2022
@knewbury01
Copy link
Contributor

this might have been solved in this PR , will check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Low
Projects
Development

Successfully merging a pull request may close this issue.

3 participants