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

M5-0-3, M5-0-7, M5-0-8, M5-0-9: Casted argument of function call is wrongly considered as a cvalue #602

Open
nbusser opened this issue Jun 1, 2024 · 3 comments · May be fixed by #778
Open
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-Medium Standard-AUTOSAR user-report Issue reported by an end user of CodeQL Coding Standards

Comments

@nbusser
Copy link

nbusser commented Jun 1, 2024

Affected rules

  • M5-0-3
  • M5-0-7
  • M5-0-8
  • M5-0-9

Description

Return value of static_cast seems to be treated as a cvalue interferring with several MISRA rules:

M-0-8

When upcasting variable using static_cast and rightaway using the result in another expression, it triggers a M5-0-9 warning (illustrated in example function false_positive).

It forces the user to create a intermediate variable containing the result of the static_cast, then using this intermediate variable in the expression (illustrated in example function true_negative).

M-0-9

When changing variable's signedness using static_cast and rightaway using the result in another expression, it triggers a M5-0-9 warning (illustrated in example function false_positive).

It forces the user to create a intermediate variable containing the result of the static_cast, then using this intermediate variable in the expression (illustrated in example function true_negative).

Example

M-0-8

void false_positive() { 
    std::vector<std::uint8_t> v{0};

    std::uint32_t u32{0};
    v.at(static_cast<std::size_t>(u32)); // Triggers a M5-0-8 warning
}

void true_negative() {
    std::vector<std::uint8_t> v{0};

    std::size_t st = static_cast<std::size_t>(u32);
    v.at(st); // Does not trigger a M5-0-8 warning
}

M-0-9

void false_positive() { 
  std::vector<std::uint8_t> v{0};

  std::int32_t s32{0};
  v.at(static_cast<std::size_t>(s32)); // Triggers a M5-0-9 warning
}

void true_negative() {
  std::vector<std::uint8_t> v{0};

  std::size_t st = static_cast<std::size_t>(s32);
  v.at(st); // Does not trigger a M5-0-9 warning
}
@nbusser nbusser added the false positive/false negative An issue related to observed false positives or false negatives. label Jun 1, 2024
@nbusser nbusser changed the title M5-0-9: Return value of static_cast is wrongly considered as a cvalue M5-0-9 + M5-0-8: Return value of static_cast is wrongly considered as a cvalue Jun 1, 2024
@nbusser nbusser changed the title M5-0-9 + M5-0-8: Return value of static_cast is wrongly considered as a cvalue M5-0-8 - M5-0-9: Return value of static_cast is wrongly considered as a cvalue Jun 1, 2024
@lcartey lcartey added the user-report Issue reported by an end user of CodeQL Coding Standards label Oct 15, 2024
@lcartey lcartey added Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address Impact-Medium labels Oct 23, 2024
@lcartey lcartey moved this from Reported to Triaged in Coding Standards Public Development Board Oct 23, 2024
@lcartey
Copy link
Collaborator

lcartey commented Oct 23, 2024

Thanks! This happens because function argument expressions are considered cvalues in MISRA, and we (incorrectly, in this case) consider the s32 to be an function argument in v.at(static_cast<std::size_t>(s32)). This is because conversions, such as casts, are represented separately in our model.

@lcartey lcartey changed the title M5-0-8 - M5-0-9: Return value of static_cast is wrongly considered as a cvalue M5-0-3, M5-0-7, M5-0-8, M5-0-9: Casted argument of function call is wrongly considered as a cvalue Oct 23, 2024
@lcartey lcartey self-assigned this Oct 23, 2024
@lcartey lcartey moved this from Triaged to Ready for review in Coding Standards Public Development Board Oct 23, 2024
@lcartey lcartey linked a pull request Oct 23, 2024 that will close this issue
30 tasks
@lcartey
Copy link
Collaborator

lcartey commented Oct 23, 2024

This also affects a few other M5-0-* rules. It also affects return values.

Fix has been created here:
#778

@nbusser-sr
Copy link

Thank you, I was about to report other affected M5-0-* rules

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-Medium Standard-AUTOSAR user-report Issue reported by an end user of CodeQL Coding Standards
Projects
Status: Ready for review
Development

Successfully merging a pull request may close this issue.

4 participants