Skip to content

fix: propagate error instead of panicking on non-UTF-8 reference name in checkout#4481

Open
kuishou68 wants to merge 1 commit into
TabbyML:mainfrom
kuishou68:fix/repo-unwrap-panic-on-non-utf8-ref
Open

fix: propagate error instead of panicking on non-UTF-8 reference name in checkout#4481
kuishou68 wants to merge 1 commit into
TabbyML:mainfrom
kuishou68:fix/repo-unwrap-panic-on-non-utf8-ref

Conversation

@kuishou68
Copy link
Copy Markdown

Problem

In crates/tabby-index/src/code/repository.rs, the call to reference.name().unwrap() can panic at runtime. reference.name() returns Option<&str>, which is None when the reference name contains non-UTF-8 bytes. Since git2 exposes raw bytes for ref names, this is a valid (if rare) code path that causes an unhandled panic.

Fix

Replace .unwrap() with .ok_or_else() to convert the Option into a Result and propagate the error through anyhow, which the function already returns:

// Before
repo.set_head(reference.name().unwrap())?;

// After
let ref_name = reference
    .name()
    .ok_or_else(|| anyhow::anyhow!("Reference name is not valid UTF-8"))?;
repo.set_head(ref_name)?;

This avoids the panic and allows callers to handle the error gracefully.

Signed-off-by: Cocoon-Break 54054995+kuishou68@users.noreply.github.com

… in checkout

When `reference.name()` returns `None` (i.e., the ref name is not valid
UTF-8), calling `.unwrap()` causes a panic. Since the enclosing function
returns `anyhow::Result`, convert the `Option` to a `Result` with
`ok_or_else` so the error is propagated to the caller instead.

Signed-off-by: Cocoon-Break <54054995+kuishou68@users.noreply.github.com>
@kortez-io
Copy link
Copy Markdown

Replacing unwrap() with proper error propagation on reference.name() is good Rust practice — a panic in index building would crash the whole repository indexing job rather than just skipping the invalid reference. Using ok_or(...) to convert the Option to a Result and propagating with ? is the right pattern. Out of curiosity, is non-UTF8 in reference names something that comes up with specific git hosting providers, or is it more of an edge case with local repos?

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.

2 participants