Return empty string instead of throwing when getpwuid_r fails unexpectedly - #132396
Open
HarnageaGabriel wants to merge 1 commit into
Open
Return empty string instead of throwing when getpwuid_r fails unexpectedly#132396HarnageaGabriel wants to merge 1 commit into
HarnageaGabriel wants to merge 1 commit into
Conversation
…tedly GetUserNameFromPasswd's doc contract already states it returns an empty string on failure, matching the existing behavior when the current user has no passwd entry. But any other getpwuid_r error (e.g. ENOENT when /etc/passwd is missing, as in restricted sandboxes/containers) fell through to an IOException, crashing callers like Environment.UserName and, transitively, dotnet build/dotnet new. Fixes dotnet#119216
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Author
|
@dotnet-policy-service agree |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #119216
Problem
GetUserNameFromPasswd's XML doc already documents that it returns an empty string on failure. That contract already holds when/etc/passwdis readable but the current UID isn't present (getpwuid_rreturns-1/ENOENT-equivalent →TryGetUserNameFromPasswdreturnsnull).But when
getpwuid_rfails for any other reason — notably when/etc/passwditself doesn't exist, as can happen in restricted sandboxes/containers —TryGetUserNameFromPasswdthrew anIOExceptioninstead. That exception propagates up throughEnvironment.UserNameand crashes the calling process (e.g.dotnet build,dotnet new) instead of degrading gracefully.Repro (from the issue)
Fix
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetPwUid.cs: inTryGetUserNameFromPasswd, the fallback branch after theERANGE(buffer-too-small) check no longer throws — it now setsusername = nulland returnstrue, same as the "current user not found" path, soGetUserNameFromPasswdreturnsstring.Emptyinstead of propagating the exception. TheERANGEretry-with-larger-buffer behavior is unchanged.Also removed the now-unused
using System.IO;.Testing
This is a small interop fix in
src/libraries/Common(shared source, no dedicated isolated test project); given the size of the dotnet/runtime repo I did not run a full runtime build. I verified the change by inspecting the diff and confirming no other caller relies on the previously-thrownIOException(checkedEnvironment.UserNameinEnvironment.Unix.cs, the only caller).