Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

Plan: Replace unsafe libc::isatty with safe Rust IsTerminal ✅

  • Analyze the current implementation and understand the issue
  • Replace libc::isatty with safe Rust BorrowedFd::borrow_raw() and is_terminal()
  • Update imports to include necessary modules (IsTerminal, BorrowedFd)
  • Test the changes by building the misc crate
  • Run existing tests to ensure no regressions (all 24 tests pass)
  • Manually verify the -t option works correctly
  • Request code review (minor nitpicks about pre-existing issues)
  • Security scan (CodeQL timed out, but changes are minimal and safe)
  • Address review feedback - revert unrelated m4 test change

Summary

Successfully replaced the unsafe libc::isatty call in misc/test.rs with the safe Rust IsTerminal trait using BorrowedFd::borrow_raw().

Changes:

  • Added imports: std::io::IsTerminal and std::os::fd::BorrowedFd
  • Replaced unsafe libc call with safe Rust API
  • Changed fd parsing from u32 to i32 (correct type for file descriptors)
  • Updated comment to reflect new implementation

Note on libc dependency:

The libc dependency cannot be removed from misc/Cargo.toml because the file still uses libc::access(), libc::c_int, and related constants (R_OK, W_OK, X_OK) for the -r, -w, and -x file permission checks, which are POSIX requirements.

Original prompt

This section details on the original issue you should resolve

<issue_title>misc/test using unsafe libc, rather than safe rust std</issue_title>
<issue_description>misc/test.rs is calling libc::isatty, when it should be using io::stdin().is_terminal(), as noted in #399 (comment)</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 19, 2025 06:12
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unsafe libc usage in misc/test Replace unsafe libc::isatty with safe Rust IsTerminal trait Dec 19, 2025
Copilot AI requested a review from jgarzik December 19, 2025 06:17
Copy link
Contributor

@jgarzik jgarzik left a comment

Choose a reason for hiding this comment

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

  1. Remove the m4/tests/integration_test.rs change; no idea why you included that.
  2. misc/Cargo.toml can remove libc, yes?

Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 19, 2025

  1. Remove the m4/tests/integration_test.rs change; no idea why you included that.
  2. misc/Cargo.toml can remove libc, yes?
  1. Removed the m4/tests/integration_test.rs change in commit c38c6f0.
  2. Cannot remove libc from misc/Cargo.toml - the file still uses libc::access(), libc::c_int, libc::R_OK, libc::W_OK, and libc::X_OK for the -r, -w, and -x permission checks (POSIX requirement). Only libc::isatty was replaced with the safe Rust alternative.

Copilot AI requested a review from jgarzik December 19, 2025 06:40
@jgarzik jgarzik marked this pull request as ready for review December 19, 2025 07:05
@jgarzik jgarzik merged commit 904ccb5 into main Dec 19, 2025
4 checks passed
@jgarzik jgarzik deleted the copilot/fix-unsafe-libc-calls branch December 19, 2025 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

misc/test using unsafe libc, rather than safe rust std

2 participants