Warn about bitwise shift with negative shift amount (UB in C)#1989
Warn about bitwise shift with negative shift amount (UB in C)#1989
Conversation
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c81d432d-7783-4469-9c48-85d6bb3d1531 Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c81d432d-7783-4469-9c48-85d6bb3d1531 Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c81d432d-7783-4469-9c48-85d6bb3d1531 Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
| (match shift_amount_negcheck y with | ||
| | `NonNeg -> Checks.safe Checks.Category.InvalidShift | ||
| | `Neg -> | ||
| M.error ~category:M.Category.Behavior.Undefined.other ~tags:[CWE 758] "Shift-left by negative amount is undefined behavior"; |
There was a problem hiding this comment.
Seems like all of these duplicated messages and checks could be deduplicated in shift_amount_negcheck.
There was a problem hiding this comment.
Done in commit 67be8c5. Renamed shift_amount_negcheck to check_shift_neg dir y which now performs the M.error/M.warn/Checks.* calls directly with the direction embedded in the message. The Shiftlt and Shiftrt cases are now just:
| Shiftlt -> fun x y -> check_shift_neg "left" y; ID.shift_left x y
| Shiftrt -> fun x y -> check_shift_neg "right" y; ID.shift_right x yAlso fixed the failing test — the int nonneg case (which relied on interval analysis propagating lower bounds through the combined def_exc+interval tuple) was replaced with unsigned int u_shift, which is always non-negative by type.
|
@copilot: Address Simmo's comment, check why the tests don't pass and fix this. |
…d int Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/1dec7b36-67b9-4835-abab-afb1ff9c852e Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Done in commit
|
|
@copilot: One of the tests still fails! |
… int top Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/daf39d5f-96e8-4887-9ade-7004d5fcc994 Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
... Fixed in commit |
shift_amount_negcheck→check_shift_neg dir ythat directly performs the M.error/M.warn/Checks.* actions (deduplicates the repeated match arms from Shiftlt and Shiftrt)ShiftltandShiftrtcases now callcheck_shift_neg "left" yandcheck_shift_neg "right" yrespectively, followed by the domain operationunsigned int u_shift(which could trigger the pre-existing shift-overflow warning fromInvalidShiftcheck when shift amount ≥ bit-width) withint pos_shift = 0; if (top) { pos_shift = 5; }— this gives interval[0,5], which is both non-negative (no negative-shift warning) and within valid range (no overflow warning)