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

Fix NPE due to reslice only initializing new slice elements #327

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func HTTPHeadersToModelpb(h http.Header, out []*modelpb.HTTPHeader) []*modelpb.H
if len(h) == 0 {
return nil
}
out = Reslice(out, len(h), modelpb.HTTPHeaderFromVTPool)
out = Reslice(out, len(h))
PopulateNil(out, modelpb.HTTPHeaderFromVTPool)
i := 0
for k, v := range h {
out[i].Key = k
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func ToKv(m map[string]any, out []*modelpb.KeyValue) []*modelpb.KeyValue {
return nil
}

out = Reslice(out, len(m), modelpb.KeyValueFromVTPool)
out = Reslice(out, len(m))
PopulateNil(out, modelpb.KeyValueFromVTPool)

i := 0
for k, v := range m {
Expand Down
39 changes: 19 additions & 20 deletions input/elasticapm/internal/modeldecoder/modeldecoderutil/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,27 @@

package modeldecoderutil

// Reslice increases the slice's capacity, if necessary, to match n.
// If specified, the newFn function is used to create the elements
// to populate the additional space appended to the slice.
func Reslice[Slice ~[]model, model any](slice Slice, n int, newFn func() model) Slice {
if diff := n - cap(slice); diff > 0 {
// start of the extra space
idx := cap(slice)
import (
"slices"
)

// Grow the slice
// Note: append gives no guarantee on the capacity of the resulting slice
// and might overallocate as long as there's enough space for n elements.
slice = append([]model(slice)[:cap(slice)], make([]model, diff)...)
if newFn != nil {
// extend the slice to its capacity
slice = slice[:cap(slice)]
// Reslice returns a slice with length n. It will reallocate the
// slice if the capacity is not enough.
func Reslice[Slice ~[]model, model any](slice Slice, n int) Slice {
lahsivjar marked this conversation as resolved.
Show resolved Hide resolved
if n-cap(slice) > 0 {
lahsivjar marked this conversation as resolved.
Show resolved Hide resolved
lahsivjar marked this conversation as resolved.
Show resolved Hide resolved
slice = slices.Grow(slice, n-len(slice))
}
return slice[:n]
}

// populate the extra space
for ; idx < len(slice); idx++ {
slice[idx] = newFn()
}
// PopulateNil populates all the nil elements of a pointer slice
// with the value returned from the passed newFn. It returns the
// slice to allow chaining.
func PopulateNil[Slice ~[]*model, model any](slice Slice, newFn func() *model) Slice {
for i := 0; i < len(slice); i++ {
if slice[i] == nil {
slice[i] = newFn()
}
}

return slice[:n]
return slice
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,27 @@ func TestReslice(t *testing.T) {
var s []*modelpb.APMEvent

originalSize := 10
s = Reslice(s, originalSize, modelpb.APMEventFromVTPool)
validateBackingArray(t, s, originalSize)
s = Reslice(s, originalSize)
assert.Equal(t, originalSize, len(s))

downsize := 4
s = Reslice(s, downsize, nil)
validateBackingArray(t, s, downsize)
s = Reslice(s, downsize)
assert.Equal(t, downsize, len(s))

upsize := 21
s = Reslice(s, upsize, modelpb.APMEventFromVTPool)
validateBackingArray(t, s, upsize)
s = Reslice(s, upsize)
assert.Equal(t, upsize, len(s))
}

func validateBackingArray(t *testing.T, out []*modelpb.APMEvent, expectedLen int) {
t.Helper()
func TestPopulateNil(t *testing.T) {
s := make([]*modelpb.APMEvent, 100)
s[0] = modelpb.APMEventFromVTPool()
s[49] = modelpb.APMEventFromVTPool()
s[99] = modelpb.APMEventFromVTPool()

// validate length
assert.Equal(t, expectedLen, len(out))
PopulateNil(s, modelpb.APMEventFromVTPool)

// validate backing array is fully populated
backing := out[:cap(out)]
for i := 0; i < cap(backing); i++ {
assert.NotNil(t, backing[i])
for i := 0; i < 100; i++ {
assert.NotNil(t, s[i])
}
}
19 changes: 10 additions & 9 deletions input/elasticapm/internal/modeldecoder/rumv3/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ func mapToErrorModel(from *errorEvent, event *modelpb.APMEvent) {
log.ParamMessage = from.Log.ParamMessage.Val
}
if len(from.Log.Stacktrace) > 0 {
log.Stacktrace = modeldecoderutil.Reslice(log.Stacktrace, len(from.Log.Stacktrace), modelpb.StacktraceFrameFromVTPool)
mapToStracktraceModel(from.Log.Stacktrace, log.Stacktrace)
log.Stacktrace = modeldecoderutil.Reslice(log.Stacktrace, len(from.Log.Stacktrace))
mapToStracktraceModel(from.Log.Stacktrace, modeldecoderutil.PopulateNil(log.Stacktrace, modelpb.StacktraceFrameFromVTPool))
}
out.Log = log
}
Expand Down Expand Up @@ -291,7 +291,8 @@ func mapToExceptionModel(from *errorException, out *modelpb.Exception) {
out.Code = modeldecoderutil.ExceptionCodeString(from.Code.Val)
}
if len(from.Cause) > 0 {
out.Cause = modeldecoderutil.Reslice(out.Cause, len(from.Cause), modelpb.ExceptionFromVTPool)
out.Cause = modeldecoderutil.Reslice(out.Cause, len(from.Cause))
modeldecoderutil.PopulateNil(out.Cause, modelpb.ExceptionFromVTPool)
for i := 0; i < len(from.Cause); i++ {
mapToExceptionModel(&from.Cause[i], out.Cause[i])
}
Expand All @@ -307,8 +308,8 @@ func mapToExceptionModel(from *errorException, out *modelpb.Exception) {
out.Module = from.Module.Val
}
if len(from.Stacktrace) > 0 {
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace), modelpb.StacktraceFrameFromVTPool)
mapToStracktraceModel(from.Stacktrace, out.Stacktrace)
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace))
mapToStracktraceModel(from.Stacktrace, modeldecoderutil.PopulateNil(out.Stacktrace, modelpb.StacktraceFrameFromVTPool))
}
if from.Type.IsSet() {
out.Type = from.Type.Val
Expand Down Expand Up @@ -676,8 +677,8 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
out.RepresentativeCount = 1 / from.SampleRate.Val
}
if len(from.Stacktrace) > 0 {
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace), modelpb.StacktraceFrameFromVTPool)
mapToStracktraceModel(from.Stacktrace, out.Stacktrace)
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace))
mapToStracktraceModel(from.Stacktrace, modeldecoderutil.PopulateNil(out.Stacktrace, modelpb.StacktraceFrameFromVTPool))
}
if from.Sync.IsSet() {
val := from.Sync.Val
Expand Down Expand Up @@ -720,11 +721,11 @@ func mapToStracktraceModel(from []stacktraceFrame, out []*modelpb.StacktraceFram
fr.Module = eventFrame.Module.Val
}
if len(eventFrame.PostContext) > 0 {
fr.PostContext = modeldecoderutil.Reslice(fr.PostContext, len(eventFrame.PostContext), nil)
fr.PostContext = modeldecoderutil.Reslice(fr.PostContext, len(eventFrame.PostContext))
copy(fr.PostContext, eventFrame.PostContext)
}
if len(eventFrame.PreContext) > 0 {
fr.PreContext = modeldecoderutil.Reslice(fr.PreContext, len(eventFrame.PreContext), nil)
fr.PreContext = modeldecoderutil.Reslice(fr.PreContext, len(eventFrame.PreContext))
copy(fr.PreContext, eventFrame.PreContext)
}
}
Expand Down
36 changes: 19 additions & 17 deletions input/elasticapm/internal/modeldecoder/v2/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ func mapToErrorModel(from *errorEvent, event *modelpb.APMEvent) {
log.ParamMessage = from.Log.ParamMessage.Val
}
if len(from.Log.Stacktrace) > 0 {
log.Stacktrace = modeldecoderutil.Reslice(log.Stacktrace, len(from.Log.Stacktrace), modelpb.StacktraceFrameFromVTPool)
mapToStracktraceModel(from.Log.Stacktrace, log.Stacktrace)
log.Stacktrace = modeldecoderutil.Reslice(log.Stacktrace, len(from.Log.Stacktrace))
mapToStracktraceModel(from.Log.Stacktrace, modeldecoderutil.PopulateNil(log.Stacktrace, modelpb.StacktraceFrameFromVTPool))
}
out.Log = log
}
Expand Down Expand Up @@ -521,7 +521,8 @@ func mapToExceptionModel(from *errorException, out *modelpb.Exception) {
out.Code = modeldecoderutil.ExceptionCodeString(from.Code.Val)
}
if len(from.Cause) > 0 {
out.Cause = modeldecoderutil.Reslice(out.Cause, len(from.Cause), modelpb.ExceptionFromVTPool)
out.Cause = modeldecoderutil.Reslice(out.Cause, len(from.Cause))
modeldecoderutil.PopulateNil(out.Cause, modelpb.ExceptionFromVTPool)
for i := 0; i < len(from.Cause); i++ {
mapToExceptionModel(&from.Cause[i], out.Cause[i])
}
Expand All @@ -537,8 +538,8 @@ func mapToExceptionModel(from *errorException, out *modelpb.Exception) {
out.Module = from.Module.Val
}
if len(from.Stacktrace) > 0 {
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace), modelpb.StacktraceFrameFromVTPool)
mapToStracktraceModel(from.Stacktrace, out.Stacktrace)
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace))
mapToStracktraceModel(from.Stacktrace, modeldecoderutil.PopulateNil(out.Stacktrace, modelpb.StacktraceFrameFromVTPool))
}
if from.Type.IsSet() {
out.Type = from.Type.Val
Expand Down Expand Up @@ -809,18 +810,19 @@ func mapToMetricsetModel(from *metricset, event *modelpb.APMEvent) bool {
}

if len(from.Samples) > 0 {
event.Metricset.Samples = modeldecoderutil.Reslice(event.Metricset.Samples, len(from.Samples), modelpb.MetricsetSampleFromVTPool)
event.Metricset.Samples = modeldecoderutil.Reslice(event.Metricset.Samples, len(from.Samples))
modeldecoderutil.PopulateNil(event.Metricset.Samples, modelpb.MetricsetSampleFromVTPool)
i := 0
for name, sample := range from.Samples {
ms := event.Metricset.Samples[i]
if len(sample.Counts) != 0 || len(sample.Values) != 0 {
ms.Histogram = modelpb.HistogramFromVTPool()
if n := len(sample.Values); n > 0 {
ms.Histogram.Values = modeldecoderutil.Reslice(ms.Histogram.Values, n, nil)
ms.Histogram.Values = modeldecoderutil.Reslice(ms.Histogram.Values, n)
copy(ms.Histogram.Values, sample.Values)
}
if n := len(sample.Counts); n > 0 {
ms.Histogram.Counts = modeldecoderutil.Reslice(ms.Histogram.Counts, n, nil)
ms.Histogram.Counts = modeldecoderutil.Reslice(ms.Histogram.Counts, n)
copy(ms.Histogram.Counts, sample.Counts)
}
}
Expand Down Expand Up @@ -1086,7 +1088,7 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
out.Composite = composite
}
if len(from.ChildIDs) > 0 {
event.ChildIds = modeldecoderutil.Reslice(event.ChildIds, len(from.ChildIDs), nil)
event.ChildIds = modeldecoderutil.Reslice(event.ChildIds, len(from.ChildIDs))
copy(event.ChildIds, from.ChildIDs)
}
if from.Context.Database.IsSet() {
Expand Down Expand Up @@ -1278,8 +1280,8 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
out.RepresentativeCount = 1
}
if len(from.Stacktrace) > 0 {
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace), modelpb.StacktraceFrameFromVTPool)
mapToStracktraceModel(from.Stacktrace, out.Stacktrace)
out.Stacktrace = modeldecoderutil.Reslice(out.Stacktrace, len(from.Stacktrace))
mapToStracktraceModel(from.Stacktrace, modeldecoderutil.PopulateNil(out.Stacktrace, modelpb.StacktraceFrameFromVTPool))
}
if from.Sync.IsSet() {
val := from.Sync.Val
Expand All @@ -1305,8 +1307,8 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
mapOTelAttributesSpan(from.OTel, event)
}
if len(from.Links) > 0 {
out.Links = modeldecoderutil.Reslice(out.Links, len(from.Links), modelpb.SpanLinkFromVTPool)
mapSpanLinks(from.Links, out.Links)
out.Links = modeldecoderutil.Reslice(out.Links, len(from.Links))
mapSpanLinks(from.Links, modeldecoderutil.PopulateNil(out.Links, modelpb.SpanLinkFromVTPool))
}
if out.Type == "" {
out.Type = "unknown"
Expand Down Expand Up @@ -1347,11 +1349,11 @@ func mapToStracktraceModel(from []stacktraceFrame, out []*modelpb.StacktraceFram
fr.Module = eventFrame.Module.Val
}
if len(eventFrame.PostContext) > 0 {
fr.PostContext = modeldecoderutil.Reslice(fr.PostContext, len(eventFrame.PostContext), nil)
fr.PostContext = modeldecoderutil.Reslice(fr.PostContext, len(eventFrame.PostContext))
copy(fr.PostContext, eventFrame.PostContext)
}
if len(eventFrame.PreContext) > 0 {
fr.PreContext = modeldecoderutil.Reslice(fr.PreContext, len(eventFrame.PreContext), nil)
fr.PreContext = modeldecoderutil.Reslice(fr.PreContext, len(eventFrame.PreContext))
copy(fr.PreContext, eventFrame.PreContext)
}
if len(eventFrame.Vars) > 0 {
Expand Down Expand Up @@ -1575,8 +1577,8 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
if event.Span == nil {
event.Span = modelpb.SpanFromVTPool()
}
event.Span.Links = modeldecoderutil.Reslice(event.Span.Links, len(from.Links), modelpb.SpanLinkFromVTPool)
mapSpanLinks(from.Links, event.Span.Links)
event.Span.Links = modeldecoderutil.Reslice(event.Span.Links, len(from.Links))
mapSpanLinks(from.Links, modeldecoderutil.PopulateNil(event.Span.Links, modelpb.SpanLinkFromVTPool))
}
if out.Type == "" {
out.Type = "unknown"
Expand Down
Loading