diff --git a/internal/tspath/path.go b/internal/tspath/path.go index 43d7bb6ea4..29a6ef8702 100644 --- a/internal/tspath/path.go +++ b/internal/tspath/path.go @@ -4,6 +4,7 @@ import ( "cmp" "strings" "unicode" + "unsafe" "github.com/microsoft/typescript-go/internal/stringutil" ) @@ -606,7 +607,17 @@ func ToFileNameLowerCase(fileName string) string { } b[i] = c } - return string(b) + // SAFETY: We construct a string that aliases b’s backing array without copying. + // (1) Lifetime: The address of b’s elements escapes via the returned string, + // so escape analysis allocates b’s backing array on the heap. The string + // header points to that heap allocation, ensuring it remains live for the + // string’s lifetime. + // (2) Initialization: We assign to every b[i] before creating the string. + // (Note: Go zeroes all allocated memory, so “uninitialized” bytes cannot occur.) + // (3) Immutability: We do not modify b after this point, so the string view + // observes immutable data. + // (4) Non-empty: On this path len(b) > 0, so &b[0] is a valid, non-nil pointer. + return unsafe.String(&b[0], len(b)) } return strings.Map(func(r rune) rune {