Skip to content

Commit

Permalink
fix: deepcopy nil fields as nil
Browse files Browse the repository at this point in the history
Without the fix the result of DeepCopy was empty slice of bytes, which
is similar, but not equal to `nil`.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
smira committed Dec 20, 2021
1 parent 9a63cba commit 6fa2d93
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,15 @@ func (p *PEMEncodedCertificateAndKey) DeepCopy() *PEMEncodedCertificateAndKey {

// DeepCopyInto implements DeepCopy interface.
func (p *PEMEncodedCertificateAndKey) DeepCopyInto(out *PEMEncodedCertificateAndKey) {
out.Crt = append([]byte(nil), p.Crt...)
out.Key = append([]byte(nil), p.Key...)
if p.Crt != nil {
out.Crt = make([]byte, len(p.Crt))
copy(out.Crt, p.Crt)
}

if p.Key != nil {
out.Key = make([]byte, len(p.Key))
copy(out.Key, p.Key)
}
}

// UnmarshalYAML implements the yaml.Unmarshaler interface for
Expand Down Expand Up @@ -1073,7 +1080,10 @@ func (p *PEMEncodedKey) DeepCopy() *PEMEncodedKey {

// DeepCopyInto implements DeepCopy interface.
func (p *PEMEncodedKey) DeepCopyInto(out *PEMEncodedKey) {
out.Key = append([]byte(nil), p.Key...)
if p.Key != nil {
out.Key = make([]byte, len(p.Key))
copy(out.Key, p.Key)
}
}

// NewCertficateAndKey is the NewCertificateAndKey with a typo in the name.
Expand Down

0 comments on commit 6fa2d93

Please sign in to comment.