mono / mono.posix

POSIX/Unix interface for Mono, .NET and .NET Core. Provides functionality for managed code to access POSIX/Unix features not accessible via the BCL. This repository supersedes the older code in https://github.com/mono/mono
MIT License
42 stars 10 forks source link

Syscall.readlink is returning incorrect length value in certain cases #45

Closed austinv900 closed 6 months ago

austinv900 commented 6 months ago

Code example

StringBuilder str = StringPool.Take();
int count = Syscall.readlink(path, str);

if (count == -1)
{
    Errno err = Stdlib.GetLastError();

    switch (err)
    {
        case Errno.EINVAL:
            return;

        default:
            throw new IOException($"Unable to process symlink | {err}", (int)err);
    }
}

string realPath = str.ToString(0, count);

System information Distributor ID: Debian Description: Debian GNU/Linux 12 (bookworm) Release: 12 Codename: bookworm

austinv900 commented 6 months ago

So it's actually returning 0 for the return value.

akoeplinger commented 6 months ago

What does StringPool.Take() do? Can you try passing in a StringBuilder with enough capacity like this:

StringBuilder str = new StringBuilder(1000);
austinv900 commented 6 months ago

That ended up being the problem, StringPool.Take just grabbed a pooled StringBuilder which is just instantiated with the default constructor. Settings Capacity to like 4096 solved the issue.

akoeplinger commented 6 months ago

Perfect, thanks.