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

🐛 [Bug]: unsafeString2Bytes may not be future proof #2180

Closed
3 tasks done
pjebs opened this issue Oct 31, 2022 · 2 comments · Fixed by #2204
Closed
3 tasks done

🐛 [Bug]: unsafeString2Bytes may not be future proof #2180

pjebs opened this issue Oct 31, 2022 · 2 comments · Fixed by #2204

Comments

@pjebs
Copy link
Contributor

pjebs commented Oct 31, 2022

Bug Description

https://github.com/gofiber/fiber/blob/master/utils/convert.go#L24
https://github.com/gofiber/fiber/blob/master/internal/fasttemplate/unsafe.go#L15

See: https://stackoverflow.com/questions/59209493/how-to-use-unsafe-get-a-byte-slice-from-a-string-without-memory-copy

The current implementations may not be as future proof as it could be according to the discussion in the link above.

They propose:

func unsafeGetBytes(s string) []byte {
    if s == "" {
        return nil // or []byte{}
    }
    return (*[0x7fff0000]byte)(unsafe.Pointer(
        (*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
    )[:len(s):len(s)]
}

How to Reproduce

N/A

Expected Behavior

N/A

Fiber Version

N/A

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
@efectn
Copy link
Member

efectn commented Oct 31, 2022

Agree with you. I think we should use recommended solution.

@ReneWerner87
Copy link
Member

The initial functions were copies of the fasthttp functions, we should open a topic there too

Furthermore we should comment the line with the magic limit and determine this number outside on a constant one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants