Skip to content

daemon.cc: Clean up PeerInfo by using std::optional#14723

Merged
edolstra merged 2 commits intomasterfrom
peer-info
Dec 8, 2025
Merged

daemon.cc: Clean up PeerInfo by using std::optional#14723
edolstra merged 2 commits intomasterfrom
peer-info

Conversation

@edolstra
Copy link
Member

@edolstra edolstra commented Dec 5, 2025

Motivation

Taken from #5265.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added the new-cli Relating to the "nix" command label Dec 5, 2025
struct passwd * pw = peer.uidKnown ? getpwuid(peer.uid) : 0;
std::string user = pw ? pw->pw_name : std::to_string(peer.uid);
auto pw = peer.uid ? getpwuid(*peer.uid) : nullptr;
std::string user = pw ? pw->pw_name : peer.uid ? std::to_string(*peer.uid) : "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty string case seems suspicious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better than the previous behavior where it would call std::to_string() on an uninitialized uid field. The empty string won't match with anything in trusted-user except *.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But all the code paths from getPeerInfo initialized all members or failed with a SysError (which gets removed now). What's the rationale for it? That was needed for the TCP store somehow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is in, shouldn't the unix socket keep getting the same error handling treatment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPeerInfo() doesn't necessarily initialize all members, e.g. on macOS it only fills in uid.

I've replaced the empty string with an std::optional.

@Ericson2314
Copy link
Member

Ericson2314 commented Dec 5, 2025

Yay thanks for extracting this!

Comment on lines +222 to +226
if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == 0) {
peer.pid = cred.pid;
peer.uid = cred.uid;
peer.gid = cred.gid;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this will fail silent, but it will fail in a deauthorizing manner.

If we had a "disallow users" this would not be the case, but thankfully we don't.

@edolstra edolstra added this pull request to the merge queue Dec 8, 2025
Merged via the queue into master with commit a95580e Dec 8, 2025
20 checks passed
@edolstra edolstra deleted the peer-info branch December 8, 2025 18:22
@edolstra edolstra mentioned this pull request Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-cli Relating to the "nix" command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants