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

Hotfix alloc crash, when no read data avail. #24

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Sources/SwiftTreeSitter/Input.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ private func readFunction(payload: UnsafeMutableRawPointer?, byteIndex: UInt32,
// get our self reference
let wrapper: Input = Unmanaged.fromOpaque(payload!).takeUnretainedValue()

// call our Swift-friendly reader block
guard let data = wrapper.readBlock(Int(byteIndex), Point(internalPoint: position)) else {
bytesRead?.pointee = 0
return nil
// call our Swift-friendly reader block, or early out if there's no data to copy.
guard let data = wrapper.readBlock(Int(byteIndex), Point(internalPoint: position)),
data.count > 0
else
{
bytesRead?.pointee = 0
return nil
}

// copy the data into an internally-managed buffer with a lifetime of wrapper
let buffer = Input.Buffer.allocate(capacity: data.count)
let buffer = Input.Buffer.allocate(capacity: data.count)
let copiedLength = data.copyBytes(to: buffer)
precondition(copiedLength == data.count)

Expand Down
Loading