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

Commit c8d62ac

Browse files
tmdsstephentoub
authored andcommitted
readlink: don't special case too small buffer in native code (#25558)
1 parent e7010ea commit c8d62ac

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

src/Common/src/Interop/Unix/System.Native/Interop.ReadLink.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static partial class Sys
1818
/// <param name="buffer">The buffer to hold the output path</param>
1919
/// <param name="bufferSize">The size of the buffer</param>
2020
/// <returns>
21-
/// Returns the number of bytes placed into the buffer on success; 0 if the buffer is too small; and -1 on error.
21+
/// Returns the number of bytes placed into the buffer on success; bufferSize if the buffer is too small; and -1 on error.
2222
/// </returns>
2323
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadLink", SetLastError = true)]
2424
private static extern unsafe int ReadLink(string path, byte[] buffer, int bufferSize);
@@ -39,16 +39,16 @@ public static string ReadLink(string path)
3939
try
4040
{
4141
int resultLength = Interop.Sys.ReadLink(path, buffer, buffer.Length);
42-
if (resultLength > 0)
43-
{
44-
// success
45-
return Encoding.UTF8.GetString(buffer, 0, resultLength);
46-
}
47-
else if (resultLength < 0)
42+
if (resultLength < 0)
4843
{
4944
// error
5045
return null;
5146
}
47+
else if (resultLength < buffer.Length)
48+
{
49+
// success
50+
return Encoding.UTF8.GetString(buffer, 0, resultLength);
51+
}
5252
}
5353
finally
5454
{

src/Native/Unix/System.Native/pal_io.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,6 @@ int32_t SystemNative_ReadLink(const char* path, char* buffer, int32_t bufferSize
10131013
ssize_t count = readlink(path, buffer, (size_t)bufferSize);
10141014
assert(count >= -1 && count <= bufferSize);
10151015

1016-
// Check if buffer may be truncated.
1017-
if (count == bufferSize)
1018-
{
1019-
return 0;
1020-
}
1021-
10221016
return (int32_t)count;
10231017
}
10241018

0 commit comments

Comments
 (0)