Skip to content

pcommon.Map.MoveTo() clears destination map when source and destination are the same #12887

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

Closed
rockdaboot opened this issue Apr 17, 2025 · 3 comments · Fixed by #12898
Closed
Assignees
Labels
bug Something isn't working

Comments

@rockdaboot
Copy link
Contributor

Component(s)

pdata

What happened?

Describe the bug
If m and dst are equal and not empty, m.MoveTo(dst) results in empty maps dst and m.

Code:

func (m Map) MoveTo(dest Map) {

Expectation
MoveTo() should be a no-op if m and dst or the underlying internal maps are equal (as in "using same memory region").

Collector version

main branch

Environment information

No response

OpenTelemetry Collector configuration

Log output

Additional context

No response

@rockdaboot rockdaboot added the bug Something isn't working label Apr 17, 2025
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@dmitryax
Copy link
Member

dmitryax commented Apr 17, 2025

If m and dst are equal and not empty, m.MoveTo(dst) results in empty maps dst and m.

I can reproduce this with the following snippet

package main

import (
	"fmt"

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

func main() {
        src := pcommon.NewMap()
        src.PutStr("hello", "world")

        dst := src

        fmt.Printf("BEFORE  MoveTo → src=%v dst=%v\n", src.AsRaw(), dst.AsRaw())

        src.MoveTo(dst)

        fmt.Printf("AFTER  MoveTo → src=%v dst=%v\n", src.AsRaw(), dst.AsRaw())
}

It returns:

BEFORE  MoveTo → src=map[hello:world] dst=map[hello:world]
AFTER  MoveTo → src=map[] dst=map[]

MoveTo() should be a no-op if m and dst or the underlying internal maps are equal (as in "using same memory region").

The issue sounds like an UB case, but we can see if this can be achieved without any performance penalty.

@rockdaboot do you want to submit a PR?

@bogdandrutu
Copy link
Member

@dmitryax this affects all MoveTo funcs, sent a fix.

@bogdandrutu bogdandrutu self-assigned this Apr 21, 2025
github-merge-queue bot pushed a commit that referenced this issue Apr 22, 2025
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Moving the data between same source and destination causes the
destination to be cleared.

<!-- Issue number if applicable -->
#### Link to tracking issue
Fixes #12887

Signed-off-by: Bogdan Drutu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants