From de41a698f1da14ef623adaeed46fb02f084586a7 Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Thu, 27 Apr 2023 19:34:08 +0000 Subject: [PATCH] Differentiate between empty and nil input Previously, CopyOf on an empty list was returning nil. With the updates to SortedUniqueStrings and FirstUniqueStrings, we need to differentiate between empty lists and nil. Bug: 275313114 Test: m nothing (cherry picked from https://android-review.googlesource.com/q/commit:cc4da765113299fa11dcb1e651ec4ae33e6f8f9b) Merged-In: I91063ebbe5013cbda5d8f70efde4683c66581599 Change-Id: I91063ebbe5013cbda5d8f70efde4683c66581599 --- android/util.go | 6 +++++- android/util_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/android/util.go b/android/util.go index 38e0a4dbb..20d007d74 100644 --- a/android/util.go +++ b/android/util.go @@ -26,7 +26,11 @@ import ( // CopyOf returns a new slice that has the same contents as s. func CopyOf(s []string) []string { - return append([]string(nil), s...) + // If the input is nil, return nil and not an empty list + if s == nil { + return s + } + return append([]string{}, s...) } // Concat returns a new slice concatenated from the two input slices. It does not change the input diff --git a/android/util_test.go b/android/util_test.go index 5584b38f7..a2ef58958 100644 --- a/android/util_test.go +++ b/android/util_test.go @@ -381,6 +381,14 @@ func TestRemoveFromList(t *testing.T) { } } +func TestCopyOfEmptyAndNil(t *testing.T) { + emptyList := []string{} + copyOfEmptyList := CopyOf(emptyList) + AssertBoolEquals(t, "Copy of an empty list should be an empty list and not nil", true, copyOfEmptyList != nil) + copyOfNilList := CopyOf(nil) + AssertBoolEquals(t, "Copy of a nil list should be a nil list and not an empty list", true, copyOfNilList == nil) +} + func ExampleCopyOf() { a := []string{"1", "2", "3"} b := CopyOf(a)