Skip to content

object.ID: sortedTags contains empty elements #421

@oxzi

Description

@oxzi

The ID function in internal/object/object.go creates the sortedTags slice with len(tags) empty fields, before adding len(tags) populated fields.

sortedTags := make([]KV, len(tags))
for k, v := range tags {
sortedTags = append(sortedTags, KV{k, v})
}
sort.Slice(sortedTags, func(i, j int) bool { return sortedTags[i].K < sortedTags[j].K })
for _, kv := range sortedTags {
h.Write([]byte(kv.K))
h.Write([]byte{0})
h.Write([]byte(kv.V))
h.Write([]byte{0})
}

This is most likely a bug and the code should be make([]KV, 0, len(tags)) to create the slice with a predefined capacity. However, due to the function warning this is maybe not something we can just fix.

// ID generates a stable identifier based on a source ID and tags.
//
// TODO: the return value of this function must be stable like forever

A simplified demo is available on the Go Playground: https://go.dev/play/p/TtBg3FsIG0e

Thanks to @BastianLedererIcinga for finding this issue while working on a reimplementation on the web side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions