From 368a1ded01c8c72de742ba7a4bf6edb27df8c67b Mon Sep 17 00:00:00 2001 From: desertwitch <24509509+desertwitch@users.noreply.github.com> Date: Fri, 12 Sep 2025 16:27:59 +0200 Subject: [PATCH] fix: return nil ptr on error and safeguard embedding nil ptrs in returns Signed-off-by: desertwitch <24509509+desertwitch@users.noreply.github.com> --- sort_generic.go | 1 + sort_ordered.go | 3 +++ sort_sorttype_legacy.go | 4 ++++ sort_strings.go | 3 +++ 4 files changed, 11 insertions(+) diff --git a/sort_generic.go b/sort_generic.go index bdfbb01..d9d8ac3 100644 --- a/sort_generic.go +++ b/sort_generic.go @@ -164,6 +164,7 @@ func Generic[E any](input <-chan E, fromBytes FromBytesGeneric[E], toBytes ToByt s.mergeErrChan <- err close(s.mergeErrChan) close(s.mergeChunkChan) + return nil, s.mergeChunkChan, s.mergeErrChan } return s, s.mergeChunkChan, s.mergeErrChan } diff --git a/sort_ordered.go b/sort_ordered.go index 25a0057..055c167 100644 --- a/sort_ordered.go +++ b/sort_ordered.go @@ -69,6 +69,9 @@ func (s *OrderedSorter[T]) toBytesOrdered(d T) ([]byte, error) { func Ordered[T cmp.Ordered](input <-chan T, config *Config) (*OrderedSorter[T], <-chan T, <-chan error) { orderedSorter := newOrderedSorter[T]() s, output, errChan := Generic(input, orderedSorter.fromBytesOrdered, orderedSorter.toBytesOrdered, cmp.Compare, config) + if s == nil { + return nil, output, errChan + } orderedSorter.GenericSorter = *s return orderedSorter, output, errChan } diff --git a/sort_sorttype_legacy.go b/sort_sorttype_legacy.go index 84f49da..629c2db 100644 --- a/sort_sorttype_legacy.go +++ b/sort_sorttype_legacy.go @@ -77,6 +77,10 @@ func New(input <-chan SortType, fromBytes FromBytes, lessFunc CompareLessFunc, c compareGeneric := makeCompareSortType(lessFunc) genericSorter, output, errChan := Generic(input, fromBytesGeneric, sortTypeToBytes, compareGeneric, config) + if genericSorter == nil { + return nil, output, errChan + } + s := &SortTypeSorter{GenericSorter: *genericSorter} return s, output, errChan } diff --git a/sort_strings.go b/sort_strings.go index ef4251f..b7b670d 100644 --- a/sort_strings.go +++ b/sort_strings.go @@ -28,6 +28,9 @@ func toBytesString(s string) ([]byte, error) { // This function provides backward compatibility with the legacy string-specific API. func Strings(input <-chan string, config *Config) (*StringSorter, <-chan string, <-chan error) { genericSorter, output, errChan := Generic(input, fromBytesString, toBytesString, cmp.Compare, config) + if genericSorter == nil { + return nil, output, errChan + } s := &StringSorter{GenericSorter: *genericSorter} return s, output, errChan }