Skip to content

crypto/x509/pkix: Name.String overwrites ExtraNames backing store #39873

@rsc

Description

@rsc

CL 229864 added code to Name.String that looks like:

if len(n.ExtraNames) == 0 {
	for _, atv := range n.Names {
		...
		n.ExtraNames = append(n.ExtraNames, atv)
	}
}

ahead of the existing

return n.ToRDNSequence().String()

This code has a subtle bug: if n.ExtraNames has len 0 but non-zero cap, this loop scribbles over n.ExtraName's backing store. By convention, String methods don't mutate the receiver, but this one does.

The code should add

n.ExtraNames = nil // avoid writing on caller's slice backing store

just before the for loop.

A test of Name.String would also be good.
(I'm surprised this package has no tests at all.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions