Skip to content

Commit

Permalink
fix NPE when Buffer.Slice (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Dec 26, 2024
1 parent 31659c5 commit b5dadaf
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions pkg/tools/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,40 +256,54 @@ func (r *Buffer) Clean() {
r.endPosition = nil
}

// nolint
func (r *Buffer) Slice(validated bool, start, end *Position) *Buffer {
dataEvents := list.New()
detailEvents := list.New()
var firstDetailElement *list.Element
var lastBufferDataID = start.DataID()
for nextElement := start.element; nextElement != end.element; nextElement = nextElement.Next() {
if nextElement == nil {
if nextElement == nil || nextElement.Value == nil {
break
}
currentBuffer := nextElement.Value.(SocketDataBuffer)
// found first matches detail event
if detailEvents.Len() == 0 || firstDetailElement == nil {
for e := r.detailEvents.Front(); e != nil; e = e.Next() {
if e.Value.(SocketDataDetail).DataID() >= nextElement.Value.(SocketDataBuffer).DataID() {
if e.Value == nil {
continue
}
if e.Value.(SocketDataDetail).DataID() >= currentBuffer.DataID() {
detailEvents.PushBack(e.Value)
firstDetailElement = e
break
}
}
}
dataEvents.PushBack(nextElement.Value)
dataEvents.PushBack(currentBuffer)
lastBufferDataID = currentBuffer.DataID()
}
lastBuffer := end.element.Value.(SocketDataBuffer)
dataEvents.PushBack(&SocketDataEventLimited{SocketDataBuffer: lastBuffer, Size: end.bufIndex})

// if the first detail element been found, append the details until the last buffer data id
var lastBufferID = lastBufferDataID
if lastBuffer != nil {
lastBufferID = lastBuffer.DataID()
}
if firstDetailElement == nil && r.detailEvents != nil {
for e := r.detailEvents.Front(); e != nil; e = e.Next() {
if e.Value.(SocketDataDetail).DataID() == lastBuffer.DataID() {
if e.Value != nil && e.Value.(SocketDataDetail).DataID() == lastBufferID {
detailEvents.PushBack(e.Value)
break
}
}
} else if firstDetailElement != nil && firstDetailElement.Value.(SocketDataDetail).DataID() != lastBuffer.DataID() {
} else if firstDetailElement != nil && firstDetailElement.Value.(SocketDataDetail).DataID() != lastBufferID {
for tmp := firstDetailElement.Next(); tmp != nil; tmp = tmp.Next() {
if tmp.Value.(SocketDataDetail).DataID() > lastBuffer.DataID() {
if tmp.Value == nil {
continue
}
if tmp.Value.(SocketDataDetail).DataID() > lastBufferID {
break
}
detailEvents.PushBack(tmp.Value)
Expand Down

0 comments on commit b5dadaf

Please sign in to comment.