Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3780052

Browse files
danmoseleyjkotas
authored andcommitted
Fix path length 255 (dotnet/coreclr#21838)
* Fix path length 255 * Fix assert * Dead code Signed-off-by: dotnet-bot <[email protected]>
1 parent ca9550b commit 3780052

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Common/src/CoreLib/Interop/Unix/System.Native/Interop.ReadDir.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)