Skip to content

src: replace postfix decremental loops#14911

Closed
Rx1513 wants to merge 1 commit intoOISF:mainfrom
Rx1513:loop-underflows/v5
Closed

src: replace postfix decremental loops#14911
Rx1513 wants to merge 1 commit intoOISF:mainfrom
Rx1513:loop-underflows/v5

Conversation

@Rx1513
Copy link
Copy Markdown
Contributor

@Rx1513 Rx1513 commented Feb 26, 2026

Previous PR: #14831

Contribution style:

Our Contribution agreements:

Changes:

Link to ticket: https://redmine.openinfosecfoundation.org/issues/8184

Describe changes:

  • Replace decremental "while" loops with incremental "for" loops.

Changes from last PR:

  • Nest decrement counter for DetectRulePacketRules.
  • Different commit title.
  • Rebase to current main.

Attaching little reference unit tests, which may be used to inspect difference in generated assembly and results given by changes:
loop_checks.c

During fuzzing, UBSan discovered that postfix increments/decrements
inside a loop condition are executed after the condition is met.

While in most cases loop counter is dropped right after it's been used
in some cases where it doesn't it may lead to unexpected behaviour.

Yet in both cases loop peforms extra addition/subtraction, which would
be nice to eliminate.

So the solution is to switch to incremental "for" loop or nest such
counters inside loops so they are executed only if loop termination
condition isn't met.
@Rx1513 Rx1513 requested review from a team and victorjulien as code owners February 26, 2026 14:37
@catenacyber catenacyber mentioned this pull request Mar 1, 2026
3 tasks
@catenacyber
Copy link
Copy Markdown
Contributor

Could you resubmit a rebased version to get green CI please ?

@catenacyber
Copy link
Copy Markdown
Contributor

Also, the commit message needs to mention Ticket: 8184

@catenacyber
Copy link
Copy Markdown
Contributor

Besides that, code looks good for me

Comment thread src/detect.c
Signature **match_array = det_ctx->match_array;
SigIntId previous_id = (SigIntId)-1;
while (final_cnt-- > 0) {
for (uint32_t i = 0; i < final_cnt; i++) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can now get rid of final_cnt and use det_ctx->pmq.rule_id_array_cnt directly, i think

Comment thread src/detect.c
}
while (match_cnt--) {
while (match_cnt) {
--match_cnt;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this, can we do a simple for loop here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without rewriting whole logic - no. I think it's too much effort for such minor issue, especially if it's drastically reduces readability.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @Rx1513

We cannot do a simple loop
match_cnt gets updated in this loop with match_cnt++; and with match_cnt -= skipped;

This simple change keeps the functionality and avoids the ubsan problem

Copy link
Copy Markdown
Member

@victorjulien victorjulien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see inline

@Rx1513
Copy link
Copy Markdown
Contributor Author

Rx1513 commented Mar 6, 2026

Replaced by #14966.

@Rx1513 Rx1513 closed this Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants