Avoid cgo bug by use C.calloc to allocate memory in C instead of C.ma…#61
Merged
valyala merged 1 commit intovalyala:masterfrom May 20, 2024
waldoweng:master
Merged
Avoid cgo bug by use C.calloc to allocate memory in C instead of C.ma…#61valyala merged 1 commit intovalyala:masterfrom waldoweng:master
valyala merged 1 commit intovalyala:masterfrom
waldoweng:master
Conversation
…lloc, See https://pkg.go.dev/cmd/cgo: Note: the current implementation has a bug. While Go code is permitted to write nil or a C pointer (but not a Go pointer) to C memory, the current implementation may sometimes cause a runtime error if the contents of the C memory appear to be a Go pointer. Therefore, avoid passing uninitialized C memory to Go code if the Go code is going to store pointer values in it. Zero out the memory in C before passing it to Go.
Collaborator
|
Hello, do you have any examples with panic related to this lib? As far as I understand current issue with CGO. It's only possible to get a panic, if program makes allocation of uninitialized objects via malloc and shadows it with GO allocations. Usual example for it is slice resizing via append. |
Collaborator
|
Related issue golang/go#19928 |
valyala
approved these changes
May 20, 2024
Owner
|
@waldoweng , thanks for the fix! |
nilz3000
pushed a commit
to nilz3000/gozstd
that referenced
this pull request
Jul 30, 2024
…lloc, (valyala#61) See https://pkg.go.dev/cmd/cgo: Note: the current implementation has a bug. While Go code is permitted to write nil or a C pointer (but not a Go pointer) to C memory, the current implementation may sometimes cause a runtime error if the contents of the C memory appear to be a Go pointer. Therefore, avoid passing uninitialized C memory to Go code if the Go code is going to store pointer values in it. Zero out the memory in C before passing it to Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…lloc,
See https://pkg.go.dev/cmd/cgo:
I also find similar approach use by Dgraph according to this blog post:
https://dgraph.io/blog/post/manual-memory-management-golang-jemalloc/