Skip to content

Commit

Permalink
fix: pitfall in nil compare
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Nov 23, 2023
1 parent 97c61b2 commit 741da1f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cmd/oras/internal/display/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -39,6 +40,8 @@ func init() {

// Set sets the output writer for printing.
func Set(template string, tty io.Writer) {
printLock.Lock()
defer printLock.Unlock()
if template != "" {
to = tty
}
Expand All @@ -49,11 +52,12 @@ type PrintFunc func(ocispec.Descriptor) error

// Print objects to display concurrent-safely.
func Print(a ...any) error {
if to == nil {
return nil
}
printLock.Lock()
defer printLock.Unlock()

if reflect.ValueOf(to).IsNil() {
return nil
}
_, err := fmt.Fprintln(to, a...)
return err
}
Expand Down

0 comments on commit 741da1f

Please sign in to comment.