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

pcommon.Map.EnsureCapacity clears the map as a side effect #7955

Closed
krdln opened this issue Jun 22, 2023 · 2 comments
Closed

pcommon.Map.EnsureCapacity clears the map as a side effect #7955

krdln opened this issue Jun 22, 2023 · 2 comments
Labels
area:pdata pdata module related issues bug Something isn't working priority:p2 Medium

Comments

@krdln
Copy link

krdln commented Jun 22, 2023

Describe the bug
pcommon.Map.EnsureCapacity clears the map as a side effect

Steps to reproduce

package main

import "go.opentelemetry.io/collector/pdata/pcommon"

func main() {
	m := pcommon.NewMap()
	m.PutStr("foo", "bar")
	m.EnsureCapacity(2)
	if m.Len() != 1 {
		panic("?")
	}
}

What did you expect to see?
No panic – map should contain "foo".

What did you see instead?
Map is empty.

What version did you use?
v0.79.0

Additional context

// EnsureCapacity increases the capacity of this Map instance, if necessary,
// to ensure that it can hold at least the number of elements specified by the capacity argument.
func (m Map) EnsureCapacity(capacity int) {
if capacity <= cap(*m.getOrig()) {
return
}
oldOrig := *m.getOrig()
*m.getOrig() = make([]otlpcommon.KeyValue, 0, capacity)
copy(*m.getOrig(), oldOrig)
}

The copy will copy zero elements, because copy

returns the number of elements copied, which will be the minimum of len(src) and len(dst).

@krdln krdln added the bug Something isn't working label Jun 22, 2023
@mx-psi mx-psi added priority:p2 Medium area:pdata pdata module related issues labels Jun 22, 2023
@mx-psi
Copy link
Member

mx-psi commented Jun 22, 2023

cc @dmitryax @bogdandrutu

@krdln
Copy link
Author

krdln commented Dec 11, 2023

Seems to be fixed by #8040

@krdln krdln closed this as completed Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pdata pdata module related issues bug Something isn't working priority:p2 Medium
Projects
None yet
Development

No branches or pull requests

2 participants