Skip to content

Rx1513/loop underflows/v4#14831

Draft
catenacyber wants to merge 1 commit intoOISF:mainfrom
catenacyber:Rx1513/loop-underflows/v4
Draft

Rx1513/loop underflows/v4#14831
catenacyber wants to merge 1 commit intoOISF:mainfrom
catenacyber:Rx1513/loop-underflows/v4

Conversation

@catenacyber
Copy link
Contributor

Previous PR: #14804

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:

  • clang-format
  • use for instead of last while

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.
@catenacyber catenacyber force-pushed the Rx1513/loop-underflows/v4 branch from 60f7be0 to da3e6f6 Compare February 18, 2026 08:51
@catenacyber
Copy link
Contributor Author

@jasonish what do you think about the clang-format report ? Looks wrong to us

diff --git a/src/detect.c b/src/detect.c
index beafe60f9c..2f7890516a 100644
--- a/src/detect.c
+++ b/src/detect.c
@@ -892,7 +892,7 @@ next:
         if (break_out_of_packet_filter) {
             break;
         }
-    }
+}

@catenacyber
Copy link
Contributor Author

@Rx513 can you check what I have done wrong in my changes ?

@catenacyber catenacyber marked this pull request as draft February 18, 2026 10:04
Comment on lines -894 to -895

continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

@catenacyber missing continue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the hidden effect of continue; before '}' closing the loop ?

Copy link
Contributor

@Rx1513 Rx1513 Feb 18, 2026

Choose a reason for hiding this comment

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

Turns out:

for (; match_cnt > 0; match_cnt--) {
}

not equivalent to:

while (match_cnt) {
match_cnt--;
}

As in for expression executed after iteration. So either match_cnt should be reduced by one before for or loop expression should be nested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please do a new PR with a better commit title and this fix :-)

@Rx1513
Copy link
Contributor

Rx1513 commented Feb 18, 2026

Also I think commit title should be changed to something more suitable.

@catenacyber
Copy link
Contributor Author

Also I think commit title should be changed to something more suitable.

Please do a new version with a better commit title :-)

@jasonish
Copy link
Member

@jasonish what do you think about the clang-format report ? Looks wrong to us

diff --git a/src/detect.c b/src/detect.c
index beafe60f9c..2f7890516a 100644
--- a/src/detect.c
+++ b/src/detect.c
@@ -892,7 +892,7 @@ next:
         if (break_out_of_packet_filter) {
             break;
         }
-    }
+}

Yeah, appears the formatter is confused.

@catenacyber
Copy link
Contributor Author

Yeah, appears the formatter is confused.

So, we should ignore it ?

@jasonish
Copy link
Member

Yeah, appears the formatter is confused.

So, we should ignore it ?

Probably, unless someone can investigate it. Should only be an error in this PR, and not continue failing.

@suricata-qa
Copy link

ERROR:

ERROR: QA failed on ASAN_TLPR1_suri.

Pipeline = 29809

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.

4 participants