@@ -30,21 +30,22 @@ internal unsafe struct DirectoryEntry
3030 internal byte * Name ;
3131 internal int NameLength ;
3232 internal NodeType InodeType ;
33- internal const int NameBufferSize = 256 ;
33+ internal const int NameBufferSize = 256 ; // sizeof(dirent->d_name) == NAME_MAX + 1
3434
3535 internal ReadOnlySpan < char > GetName ( Span < char > buffer )
3636 {
37- Debug . Assert ( buffer . Length >= Encoding . UTF8 . GetMaxCharCount ( NameBufferSize - 1 ) , "should have enough space for the max file name" ) ;
37+ // -1 for null terminator (buffer will not include one),
38+ // and -1 because GetMaxCharCount pessimistically assumes the buffer may start with a partial surrogate
39+ Debug . Assert ( buffer . Length >= Encoding . UTF8 . GetMaxCharCount ( NameBufferSize - 1 - 1 ) ) ;
40+
3841 Debug . Assert ( Name != null , "should not have a null name" ) ;
3942
4043 ReadOnlySpan < byte > nameBytes = ( NameLength == - 1 )
4144 // In this case the struct was allocated via struct dirent *readdir(DIR *dirp);
42- ? new ReadOnlySpan < byte > ( Name , new ReadOnlySpan < byte > ( Name , NameBufferSize - 1 ) . IndexOf < byte > ( 0 ) )
45+ ? new ReadOnlySpan < byte > ( Name , new ReadOnlySpan < byte > ( Name , NameBufferSize ) . IndexOf < byte > ( 0 ) )
4346 : new ReadOnlySpan < byte > ( Name , NameLength ) ;
4447
4548 Debug . Assert ( nameBytes . Length > 0 , "we shouldn't have gotten a garbage value from the OS" ) ;
46- if ( nameBytes . Length == 0 )
47- return buffer . Slice ( 0 , 0 ) ;
4849
4950 int charCount = Encoding . UTF8 . GetChars ( nameBytes , buffer ) ;
5051 ReadOnlySpan < char > value = buffer . Slice ( 0 , charCount ) ;
0 commit comments