Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/host_env/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ pub use libc::{AF_ALG, AF_CAN};
#[cfg(target_os = "linux")]
pub use libc::{sockaddr_alg, sockaddr_can};

/// Set the system's hostname from its filesystem-encoded bytes.
///
/// `socketmodule.c socket_sethostname` reads the argument as a buffer and
/// passes `buf.buf`/`buf.len` straight to the syscall, so a name is not
/// required to be UTF-8; taking `&[u8]` keeps that true here as well.
#[cfg(all(unix, not(target_os = "redox")))]
pub fn sethostname(hostname: &str) -> io::Result<()> {
nix::unistd::sethostname(hostname).map_err(io::Error::from)
pub fn sethostname(hostname: &[u8]) -> io::Result<()> {
use std::os::unix::ffi::OsStrExt;
nix::unistd::sethostname(std::ffi::OsStr::from_bytes(hostname)).map_err(io::Error::from)
}

#[cfg(unix)]
Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2325,8 +2325,8 @@ mod _socket {

#[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]
#[pyfunction]
fn sethostname(hostname: PyUtf8StrRef) -> std::io::Result<()> {
host_socket::sethostname(hostname.as_str())
fn sethostname(hostname: FsPath) -> std::io::Result<()> {
host_socket::sethostname(hostname.as_bytes())
Comment on lines +2328 to +2329
Comment on lines +2328 to +2329

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate relevant files =="
git ls-files | rg '(^|/)socket\.rs$|fspath\.rs$|fs\.rs$|path\.rs$|sethostname|host_socket|host_env' || true

echo "== socket.rs outline around sethostname =="
if [ -f crates/stdlib/src/socket.rs ]; then
  wc -l crates/stdlib/src/socket.rs
  rg -n "sethostname|FsPath|as_bytes|as_os_str|#\\[pyfunction\\]" crates/stdlib/src/socket.rs
  sed -n '2290,2345p' crates/stdlib/src/socket.rs
fi

echo "== fspath implementation outline/search =="
for f in $(git ls-files | rg 'fspath\.rs$'); do
  echo "--- $f"
  wc -l "$f"
  rg -n "struct FsPath|enum Fspath|impl .*FsPath|as_bytes|as_os_str|fsencode|surrog" "$f"
  sed -n '1,260p' "$f"
done

echo "== search sethostname in codebase =="
rg -n "fn sethostname|host_socket::sethostname|host_env::sethostname|PyUnicode_FSConverter|sethostname" .

Repository: RustPython/RustPython

Length of output: 12842


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== host_env socket sethostname =="
sed -n '1,90p' crates/host_env/src/socket.rs

echo "== fsencode implementation search =="
rg -n "fn fsencode|fsencode\\(" crates/vm/src crates/host_env/src | head -80
for f in $(git ls-files | rg 'crates/(vm|host_env)/src/.*'); do
  if rg -q "fn fsencode|fsencode\\(" "$f"; then
    echo "--- $f"
    rg -n -A 40 -B 5 "fn fsencode|fsencode\\(" "$f"
  fi
done

echo "== existing socket hostname tests =="
sed -n '1130,1180p' Lib/test/test_socket.py 2>/dev/null || true

echo "== sethostname call context in stdlib =="
sed -n '2200,2335p' crates/stdlib/src/socket.rs

echo "== import/return type context for sethostname =="
sed -n '1,35p' crates/stdlib/src/socket.rs

Repository: RustPython/RustPython

Length of output: 20484


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== encode_text implementation candidates =="
rg -n "encode_text|fs_encode_errors|codec_registry|FS_ERRORS|surrogate" crates/vm/src crates/host_env/src | head -120

echo "== codec registry outline/search =="
rg -n "struct CodecRegistry|fn encode_text|encode_text" crates/vm/src | head -80

for f in $(git ls-files | rg 'crates/vm/src/.*'); do
  if rg -q "encode_text\\(" "$f"; then
    echo "--- $f"
    rg -n -A 80 -B 5 "encode_text\\(|struct CodecRegistry|codec_registry" "$f"
  fi
done

echo "== fs decode/encode tests =="
rg -n "fsencode|fsdecode|surrogates|\\udc[a-f0-9][0-9a-f]{2}|\\\\udcf" crates/stdlib Lib test crates/vm | head -160 | sed -n '1,180p'

Repository: RustPython/RustPython

Length of output: 36215


Use filesystem encoding before forwarding hostname.

FsPath::as_bytes() returns raw string bytes for FsPath::Str, and host_socket::sethostname forwards those bytes directly to sethostname. A surrogate string such as "\udcff" therefore bypasses vm.fsencode; use hostname.as_os_str(vm)? and pass its Unix OsStr bytes to host_socket::sethostname. Add a root-gated regression test for the surrogate-string branch; the current string test only covers ASCII.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/stdlib/src/socket.rs` around lines 2328 - 2329, Update sethostname to
obtain the hostname bytes through hostname.as_os_str(vm)? before calling
host_socket::sethostname, ensuring filesystem encoding handles surrogate
strings. Add a root-gated regression test covering a surrogate-string hostname
in addition to the existing ASCII string test.

Source: MCP tools

}
Comment on lines +2328 to 2330

#[pyfunction]
Expand Down
Loading