Skip to content

fboss2: guard against unset USER env var in getUnixname#1293

Open
atulya-singh wants to merge 1 commit into
facebook:mainfrom
atulya-singh:fix-getunixname-null-deref
Open

fboss2: guard against unset USER env var in getUnixname#1293
atulya-singh wants to merge 1 commit into
facebook:mainfrom
atulya-singh:fix-getunixname-null-deref

Conversation

@atulya-singh

@atulya-singh atulya-singh commented Jun 13, 2026

Copy link
Copy Markdown

Pre-submission checklist

  • I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running pip install -r requirements-dev.txt && pre-commit install
  • pre-commit run

Summary

getUnixname() in fboss/cli/fboss2/utils/CmdUtilsCommon.cpp calls getenv("USER") and passes the result directly to strcmp():

auto envuser = getenv("USER");
if (strcmp(envuser, "root") != 0 && strcmp(envuser, "netops") != 0) {

getenv returns nullptr when the variable is not set, so strcmp(nullptr, ...) is a null-pointer dereference and crashes the CLI. This is inconsistent with getUserInfo() directly above, which already null-checks the result of getenv.

This change guards for nullptr and empty string before the strcmp calls. An unset or empty USER now falls through to the existing interactive prompt instead of crashing.

Test Plan

  • pre-commit run --files fboss/cli/fboss2/utils/CmdUtilsCommon.cpp -> clang-format ... Passed, all hooks pass.
  • Behavior:
    • USER=alice -> returns alice (unchanged)
    • USER=root -> falls through to interactive prompt (unchanged)
    • USER unset / empty -> falls through to interactive prompt (previously crashed)

getenv("USER") can return nullptr (e.g. when USER is not set in the
environment), and getUnixname() passed the result straight to strcmp(),
causing a null-pointer dereference / crash. Guard for nullptr and empty
string before the strcmp calls, matching the handling already done in
getUserInfo() above. An empty/unset USER now falls through to the
interactive prompt.
@atulya-singh atulya-singh requested a review from a team as a code owner June 13, 2026 19:27
@meta-cla meta-cla Bot added the CLA Signed label Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant