* [PATCH v1] hash: handle invalid algorithms when formatting object IDs
@ 2026-11-03 4:37 shrimech
0 siblings, 0 replies; 2+ messages in thread
From: shrimech @ 2026-11-03 4:37 UTC (permalink / raw)
To: git; +Cc: shrimech
ObjectID::algo is publicly mutable, so an ObjectID may contain an algorithm
identifier that is not recognized by HashAlgorithm. Display previously
unwrapped as_slice(), which returns an error for such identifiers, causing
formatting to panic.
Format the full hash storage for invalid identifiers and append an
invalid-hash-algo marker containing the unsupported identifier. Add a
regression test covering this case while preserving the existing output for
valid algorithms.
Signed-off-by: shrimech <eddinen77@gmail.com>
---
Notes:
Base Ref: v2.56.0
Web-Diff: https://github.com/shrimech/git/commit/c95530cf38
Checkout: git fetch https://github.com/shrimech/git fix/objectid-display-invalid-algo-v1 && git checkout c95530cf38
src/hash.rs | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/src/hash.rs b/src/hash.rs
index e1f2d31fc3..87242d58b1 100644
--- a/src/hash.rs
+++ b/src/hash.rs
@@ -78,13 +78,19 @@ impl ObjectID {
}
impl Display for ObjectID {
- /// Format this object ID as a hex object ID.
+ /// Format this object ID as a hex object ID, or mark it if its algorithm is invalid.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- let hash = self.as_slice().unwrap();
+ let hash = match self.as_slice() {
+ Ok(hash) => hash,
+ Err(_) => &self.hash,
+ };
for x in hash {
write!(f, "{:02x}", x)?;
}
- Ok(())
+ match self.algo() {
+ Ok(_) => Ok(()),
+ Err(e) => write!(f, ":invalid-hash-algo-{}", e.0),
+ }
}
}
@@ -366,7 +372,7 @@ pub mod c {
#[cfg(test)]
mod tests {
- use super::{CryptoDigest, HashAlgorithm, ObjectID};
+ use super::{CryptoDigest, HashAlgorithm, ObjectID, GIT_MAX_RAWSZ};
use std::io::Write;
fn all_algos() -> &'static [HashAlgorithm] {
@@ -439,6 +445,19 @@ mod tests {
}
}
+ #[test]
+ fn invalid_object_ids_format_without_panicking() {
+ let oid = ObjectID {
+ hash: [0xab; GIT_MAX_RAWSZ],
+ algo: 0,
+ };
+
+ assert_eq!(
+ format!("{}", oid),
+ format!("{}:invalid-hash-algo-0", "ab".repeat(GIT_MAX_RAWSZ))
+ );
+ }
+
#[test]
fn hasher_works_correctly() {
for algo in all_algos() {
base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH v1] hash: handle invalid algorithms when formatting object IDs
@ 2026-11-03 4:20 shrimech
0 siblings, 0 replies; 2+ messages in thread
From: shrimech @ 2026-11-03 4:20 UTC (permalink / raw)
To: git; +Cc: shrimech
ObjectID::algo is publicly mutable, so an ObjectID may contain an algorithm
identifier that is not recognized by HashAlgorithm. Display previously
unwrapped as_slice(), which returns an error for such identifiers, causing
formatting to panic.
Format the full hash storage for invalid identifiers and append an
invalid-hash-algo marker containing the unsupported identifier. Add a
regression test covering this case while preserving the existing output for
valid algorithms.
Signed-off-by: shrimech <eddinen77@gmail.com>
---
Notes:
Base Ref: v2.56.0
Web-Diff: https://github.com/larsxschneider/git/commit/c95530cf38
Checkout: git fetch https://github.com/larsxschneider/git fix/objectid-display-invalid-algo-v1 && git checkout c95530cf38
src/hash.rs | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/src/hash.rs b/src/hash.rs
index e1f2d31fc3..87242d58b1 100644
--- a/src/hash.rs
+++ b/src/hash.rs
@@ -78,13 +78,19 @@ impl ObjectID {
}
impl Display for ObjectID {
- /// Format this object ID as a hex object ID.
+ /// Format this object ID as a hex object ID, or mark it if its algorithm is invalid.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- let hash = self.as_slice().unwrap();
+ let hash = match self.as_slice() {
+ Ok(hash) => hash,
+ Err(_) => &self.hash,
+ };
for x in hash {
write!(f, "{:02x}", x)?;
}
- Ok(())
+ match self.algo() {
+ Ok(_) => Ok(()),
+ Err(e) => write!(f, ":invalid-hash-algo-{}", e.0),
+ }
}
}
@@ -366,7 +372,7 @@ pub mod c {
#[cfg(test)]
mod tests {
- use super::{CryptoDigest, HashAlgorithm, ObjectID};
+ use super::{CryptoDigest, HashAlgorithm, ObjectID, GIT_MAX_RAWSZ};
use std::io::Write;
fn all_algos() -> &'static [HashAlgorithm] {
@@ -439,6 +445,19 @@ mod tests {
}
}
+ #[test]
+ fn invalid_object_ids_format_without_panicking() {
+ let oid = ObjectID {
+ hash: [0xab; GIT_MAX_RAWSZ],
+ algo: 0,
+ };
+
+ assert_eq!(
+ format!("{}", oid),
+ format!("{}:invalid-hash-algo-0", "ab".repeat(GIT_MAX_RAWSZ))
+ );
+ }
+
#[test]
fn hasher_works_correctly() {
for algo in all_algos() {
base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-03 4:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-03 4:37 [PATCH v1] hash: handle invalid algorithms when formatting object IDs shrimech
-- strict thread matches above, loose matches on Subject: below --
2026-11-03 4:20 shrimech
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox