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

refactor: replace reflect.StringHeader with unsafe.StringData #1162

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Oct 2, 2024

Thank you for contributing to Coraza WAF, your effort is greatly appreciated
Before submitting check if what you want to add to coraza list meets quality standards before sending pull request. Thanks!

Make sure that you've checked the boxes below before you submit PR:

Thanks for your contribution ❤️


Closes #1147.

The Godoc of reflect.StringHeader states 1:

the Data field is not sufficient to guarantee the data it references will not be garbage collected, so programs must keep a separate, correctly typed pointer to the underlying data.

And the Godoc of unsafe.Pointer states 2:

(2) Conversion of a Pointer to a uintptr (but not back to Pointer)
...
A uintptr is an integer, not a reference. ... Even if a uintptr holds
the address of some object, the garbage collector will not update that uintptr's value if the object moves, nor will that uintptr keep the object from being reclaimed.

The replacement unsafe.StringData is a more correct way to get the pointer to the string data. The original proposal can be seen in golang/go#53003 (comment).

Footnotes

  1. https://pkg.go.dev/reflect#StringHeader

  2. https://pkg.go.dev/unsafe#Pointer

Closes #1147.

The Godoc of `reflect.StringHeader` says [1]:

  "the Data field is not sufficient to guarantee the data it references
will not be garbage collected, so programs must keep a separate,
correctly typed pointer to the underlying data."

And the Godoc of `unsafe.Pointer` says [2]:

  "A uintptr is an integer, not a reference. ... Even if a uintptr holds
the address of some object, the garbage collector will not update that
uintptr's value if the object moves, nor will that uintptr keep the
object from being reclaimed."

The replacement `unsafe.StringData` is a more correct way to get the
pointer to the string data. The original proposal can be seen in
golang/go#53003 (comment).

[1]: https://pkg.go.dev/reflect#StringHeader
[2]: https://pkg.go.dev/unsafe#Pointer

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@Juneezee Juneezee requested a review from a team as a code owner October 2, 2024 16:19
Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.92%. Comparing base (4ff1f76) to head (142970e).
Report is 91 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1162      +/-   ##
==========================================
+ Coverage   82.72%   83.92%   +1.20%     
==========================================
  Files         162      166       +4     
  Lines        9080     7894    -1186     
==========================================
- Hits         7511     6625     -886     
+ Misses       1319     1019     -300     
  Partials      250      250              
Flag Coverage Δ
default 83.92% <100.00%> (+6.08%) ⬆️
examples 83.92% <100.00%> (+57.49%) ⬆️
ftw 83.92% <100.00%> (+36.55%) ⬆️
ftw-multiphase 83.92% <100.00%> (+34.38%) ⬆️
tinygo 83.92% <100.00%> (+8.52%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jcchavezs
Copy link
Member

The change looks very reasonable to me.

Copy link
Member

@fzipi fzipi left a comment

Choose a reason for hiding this comment

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

:shipit:

@fzipi fzipi merged commit 459f05c into corazawaf:main Oct 4, 2024
8 checks passed
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.

Revisit usage of deprecated reflect.StringHeader
3 participants