Skip to content

Refactor toHexIfNeeded for better string handling - #3743

Open
JakubOnderka wants to merge 1 commit into
owasp-modsecurity:v3/masterfrom
JakubOnderka:patch-2
Open

JakubOnderka wants to merge 1 commit into
owasp-modsecurity:v3/masterfrom
JakubOnderka:patch-2

Conversation

@JakubOnderka

@JakubOnderka JakubOnderka commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

what

  • refactor toHexIfNeeded function for improved performance and clarity

why

  • toHexIfNeeded is used in converting request and response bodies, that can be pretty big
  • current function performance is not optimal, the new approach is 5 times faster for 1 MB input
Input Original Optimized Speedup
All printable 119 MB/s 692 MB/s 5.8x
10% binary 95 MB/s 439 MB/s 4.6x
All binary 35 MB/s 175 MB/s 5.0x

Summary by CodeRabbit

  • Refactor
    • Updated how escaped text is formatted. Existing escaping rules and lowercase hexadecimal output are preserved; the change is internal and does not alter user-visible behavior.

Refactor toHexIfNeeded function for improved performance and clarity.
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0f81414c-03b1-4994-9b5a-cc44ecb314fd
Commits

Reviewing files that changed from the base of the PR and between 1ebdc39 and 8a7a55e.

Files selected for processing: 1
  • src/utils/string.h

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

toHexIfNeeded now builds escaped output directly in a std::string with a lowercase hexadecimal lookup table. Its escaping conditions and \x output format remain unchanged.

Changes: Hex escaping utility

Layer / File(s) Summary
Direct hexadecimal encoding
src/utils/string.h
toHexIfNeeded replaces std::stringstream formatting with direct string construction and lowercase hexadecimal lookup. The existing escaping conditions remain unchanged.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Refactor

Possibly related issues

  • owasp-modsecurity/ModSecurity#3601: The PR rewrites the same utility but preserves the escape_spec condition, so it does not address the issue's requested call-site changes.

Suggested labels: 3.x

Suggested reviewers: eduar-hte, airween

Merge Risk: Minimal · up to 8a7a5

The refactor preserves the existing output contract and is ready to merge.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: refactoring toHexIfNeeded to improve string handling.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

Copilot AI left a comment

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.

🟢 Approval recommended

The toHexIfNeeded refactor has no active review issues.

No open issues 🎉

What changed in this PR

Optimizes toHexIfNeeded, used when converting request and response bodies, by replacing stream formatting with direct string appends while preserving escaping rules and lowercase hexadecimal output.

Changes:

  • Uses a hexadecimal lookup table instead of stream manipulators.
  • Reserves initial output capacity based on the input size.
File Description
src/​utils/​string.h Refactors toHexIfNeeded to reduce string-formatting overhead.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants