Skip to content

Conversation

@zeroshade
Copy link
Member

@zeroshade zeroshade commented Dec 7, 2022

@zeroshade zeroshade requested a review from pitrou December 7, 2022 18:45
@github-actions
Copy link

github-actions bot commented Dec 7, 2022

for i := range arr.Data().Buffers() {
buf := arr.Data().Buffers()[i]
if buf == nil {
if buf == nil || buf.Len() == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem mandatory either.

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason for this is line 374 below. In the case where buf.Len() == 0 there is no allocated data to grab a pointer to via &buf.Bytes()[0] and you end up panic'ing with an error of attempting to get index 0 of a 0 length slice. Thus if we create an empty array, it's better to just use a nil pointer.

The alternative here would be to use &buf.Buf()[0] instead, which points at the reserved bytes (since creating a new buffer will, by default, automatically reserve 64 bytes if it wasn't expanded) but I thought it better to not force us to keep that memory around for a 0 length array.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds ok to me.

@zeroshade zeroshade merged commit 5d2d2be into apache:master Dec 12, 2022
@zeroshade zeroshade deleted the gh-14876-cdata-go branch December 12, 2022 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Go] Address Crashes for empty C Data arrays with nil buffers

2 participants