{"thread":{"id":"66752","subject":"[PATCH v1] hash: handle invalid algorithms when formatting object IDs","startedAt":"2026-11-03T04:37:56Z","lastAt":"2026-11-09T22:45:49Z","messageCount":5,"participants":["shrimech","Tanga Joris","brian m. carlson"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"557263","messageId":"20261103043756.61627-1-eddinen77@gmail.com","threadId":"66752","inReplyTo":null,"subject":"[PATCH v1] hash: handle invalid algorithms when formatting object IDs","fromName":"shrimech","fromEmail":"eddinen77@gmail.com","sentAt":"2026-11-03T04:37:56Z","receivedAt":"2026-11-03T04:37:56Z","isPatch":true,"sender":{"key":"eddinen77@gmail.com","avatar":null},"body":"ObjectID::algo is publicly mutable, so an ObjectID may contain an algorithm\nidentifier that is not recognized by HashAlgorithm. Display previously\nunwrapped as_slice(), which returns an error for such identifiers, causing\nformatting to panic.\n\nFormat the full hash storage for invalid identifiers and append an\ninvalid-hash-algo marker containing the unsupported identifier. Add a\nregression test covering this case while preserving the existing output for\nvalid algorithms.\n\nSigned-off-by: shrimech <eddinen77@gmail.com>\n---\n\nNotes:\n    Base Ref: v2.56.0\n    Web-Diff: https://github.com/shrimech/git/commit/c95530cf38\n    Checkout: git fetch https://github.com/shrimech/git fix/objectid-display-invalid-algo-v1 && git checkout c95530cf38\n\n src/hash.rs | 27 +++++++++++++++++++++++----\n 1 file changed, 23 insertions(+), 4 deletions(-)\n\ndiff --git a/src/hash.rs b/src/hash.rs\nindex e1f2d31fc3..87242d58b1 100644\n--- a/src/hash.rs\n+++ b/src/hash.rs\n@@ -78,13 +78,19 @@ impl ObjectID {\n }\n \n impl Display for ObjectID {\n-    /// Format this object ID as a hex object ID.\n+    /// Format this object ID as a hex object ID, or mark it if its algorithm is invalid.\n     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {\n-        let hash = self.as_slice().unwrap();\n+        let hash = match self.as_slice() {\n+            Ok(hash) => hash,\n+            Err(_) => &self.hash,\n+        };\n         for x in hash {\n             write!(f, \"{:02x}\", x)?;\n         }\n-        Ok(())\n+        match self.algo() {\n+            Ok(_) => Ok(()),\n+            Err(e) => write!(f, \":invalid-hash-algo-{}\", e.0),\n+        }\n     }\n }\n \n@@ -366,7 +372,7 @@ pub mod c {\n \n #[cfg(test)]\n mod tests {\n-    use super::{CryptoDigest, HashAlgorithm, ObjectID};\n+    use super::{CryptoDigest, HashAlgorithm, ObjectID, GIT_MAX_RAWSZ};\n     use std::io::Write;\n \n     fn all_algos() -> &'static [HashAlgorithm] {\n@@ -439,6 +445,19 @@ mod tests {\n         }\n     }\n \n+    #[test]\n+    fn invalid_object_ids_format_without_panicking() {\n+        let oid = ObjectID {\n+            hash: [0xab; GIT_MAX_RAWSZ],\n+            algo: 0,\n+        };\n+\n+        assert_eq!(\n+            format!(\"{}\", oid),\n+            format!(\"{}:invalid-hash-algo-0\", \"ab\".repeat(GIT_MAX_RAWSZ))\n+        );\n+    }\n+\n     #[test]\n     fn hasher_works_correctly() {\n         for algo in all_algos() {\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\n-- \n2.47.3\n\n\n"},{"id":"557553","messageId":"CAKrRMNrLvPck5psPpKX1eWe26To5srfFSLFObZ4zTX09RE551w@mail.gmail.com","threadId":"66752","inReplyTo":"20261103043756.61627-1-eddinen77@gmail.com","subject":"Re: [PATCH v1] hash: handle invalid algorithms when formatting object IDs","fromName":"Tanga Joris","fromEmail":"tangajoris@gmail.com","sentAt":"2026-11-05T14:47:00Z","receivedAt":"2026-11-05T14:47:00Z","isPatch":true,"sender":{"key":"tangajoris@gmail.com","avatar":null},"body":"Hello shrimech,\n\nThank you for the patch.\n\nI tried it on top of master (0ffa3e35c7). \"cargo test\" passes, and\nthe new test fails without your change to fmt(), so it does catch\nthe problem.\n\nOn Tue, 3 Nov 2026 at 07:42, shrimech <eddinen77@gmail.com> wrote:\n> +            Err(e) => write!(f, \":invalid-hash-algo-{}\", e.0),\n\nI have one question. In the C code, algo 0 is not treated as an\nerror when printing: hash_to_hex_algop_r() in hex.c falls back to\nthe default hash (\"Our struct object_id has been memset to 0, so\ndefault to printing using the default hash\"). Your test uses algo 0\nas the invalid case, so for algo 0 the Rust output would be\ndifferent from C. Is that intended?\n\nbrian m. carlson wrote most of src/hash.rs, so I have added him on\nCc, I hope that is okay:)\n\nAlso the first patch versions can be sent as [PATCH] instead of\n[PATCH v1]. Either way works. The v1 format is less common though.\n\nThanks,\nTanga\n\n"},{"id":"557639","messageId":"au0ZOsuyo0YYyZsa@fruit.crustytoothpaste.net","threadId":"66752","inReplyTo":"CAKrRMNrLvPck5psPpKX1eWe26To5srfFSLFObZ4zTX09RE551w@mail.gmail.com","subject":"Re: [PATCH v1] hash: handle invalid algorithms when formatting object IDs","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-06T00:03:07Z","receivedAt":"2026-11-06T00:03:07Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-11-05 at 14:47:00, Tanga Joris wrote:\n> Hello shrimech,\n> \n> Thank you for the patch.\n> \n> I tried it on top of master (0ffa3e35c7). \"cargo test\" passes, and\n> the new test fails without your change to fmt(), so it does catch\n> the problem.\n> \n> On Tue, 3 Nov 2026 at 07:42, shrimech <eddinen77@gmail.com> wrote:\n> > +            Err(e) => write!(f, \":invalid-hash-algo-{}\", e.0),\n> \n> I have one question. In the C code, algo 0 is not treated as an\n> error when printing: hash_to_hex_algop_r() in hex.c falls back to\n> the default hash (\"Our struct object_id has been memset to 0, so\n> default to printing using the default hash\"). Your test uses algo 0\n> as the invalid case, so for algo 0 the Rust output would be\n> different from C. Is that intended?\n\nThis is not a valid configuration.  I have a patch on my sha256-interop\nbranch, 5b208e4a3d34ffba0fbeafe991643d6d47a5caa0, that should make the\nmembers not public.  It depends partially on things other in that\nseries, but it should be fine to drop those pieces if you want to pick\nthat up and send it in.\n\nIf you pass the structure from C, it's intended that it panics if algo\nis zero.  That's because we don't want to rely on the behaviour of\ndepending on a mutable global in Rust, since that's unsafe and leads to\naction at a distance.  We really should just drop that fallback and fix\nall of the relevant callers at some point.\n\n> brian m. carlson wrote most of src/hash.rs, so I have added him on\n> Cc, I hope that is okay:)\n\nMinor nit: I use they/them pronouns.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\nSpeaking for myself only\n"},{"id":"557814","messageId":"CAKrRMNqBCiH06xsre-YafBJiqPTiBgPnNZ3xPrhjTQWU3czwHg@mail.gmail.com","threadId":"66752","inReplyTo":"au0ZOsuyo0YYyZsa@fruit.crustytoothpaste.net","subject":"Re: [PATCH v1] hash: handle invalid algorithms when formatting object IDs","fromName":"Tanga Joris","fromEmail":"tangajoris@gmail.com","sentAt":"2026-11-07T20:22:01Z","receivedAt":"2026-11-07T20:22:01Z","isPatch":true,"sender":{"key":"tangajoris@gmail.com","avatar":null},"body":"Hi brian,\n\nThank you for jumping in.\n\nOn Fri, 6 Nov 2026 at 03:03, brian m. carlson\n<sandals@crustytoothpaste.net> wrote:\n> Minor nit: I use they/them pronouns.\n\nI'm really sorry for getting your pronouns wrong.\n\n> This is not a valid configuration.  I have a patch on my sha256-interop\n> branch, 5b208e4a3d34ffba0fbeafe991643d6d47a5caa0, that should make the\n> members not public.  It depends partially on things other in that\n> series, but it should be fine to drop those pieces if you want to pick\n> that up and send it in.\n>\n> If you pass the structure from C, it's intended that it panics if algo\n> is zero.  That's because we don't want to rely on the behaviour of\n> depending on a mutable global in Rust, since that's unsafe and leads to\n> action at a distance.  We really should just drop that fallback and fix\n> all of the relevant callers at some point.\n\nThat makes sense. I didn't know algo 0 coming from C is meant to\npanic in Rust, rather than depend on the default hash.\n\nSince this is shrimech's thread, I think shrimech could pick up\n5b208e4a3d (making the ObjectID members private), and I'd be happy\nto review it.\n\nCould you also push 5b208e4a3d somewhere public? I could not fetch\nit from https://github.com/bk2204/git, whose sha256-interop branch\nseems to have an older version (c38d0c8475, \"rust: don't expose the\ninternals of ObjectID\"). That would make it easier for shrimech to\npick up.\n\nThanks,\nTanga\n\n"},{"id":"558008","messageId":"avJNHSZLiZVffCjJ@fruit.crustytoothpaste.net","threadId":"66752","inReplyTo":"CAKrRMNqBCiH06xsre-YafBJiqPTiBgPnNZ3xPrhjTQWU3czwHg@mail.gmail.com","subject":"Re: [PATCH v1] hash: handle invalid algorithms when formatting object IDs","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-09T22:45:49Z","receivedAt":"2026-11-09T22:45:49Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-11-07 at 20:22:01, Tanga Joris wrote:\n> Hi brian,\n\nHey,\n\n> > This is not a valid configuration.  I have a patch on my sha256-interop\n> > branch, 5b208e4a3d34ffba0fbeafe991643d6d47a5caa0, that should make the\n> > members not public.  It depends partially on things other in that\n> > series, but it should be fine to drop those pieces if you want to pick\n> > that up and send it in.\n> >\n> > If you pass the structure from C, it's intended that it panics if algo\n> > is zero.  That's because we don't want to rely on the behaviour of\n> > depending on a mutable global in Rust, since that's unsafe and leads to\n> > action at a distance.  We really should just drop that fallback and fix\n> > all of the relevant callers at some point.\n> \n> That makes sense. I didn't know algo 0 coming from C is meant to\n> panic in Rust, rather than depend on the default hash.\n> \n> Since this is shrimech's thread, I think shrimech could pick up\n> 5b208e4a3d (making the ObjectID members private), and I'd be happy\n> to review it.\n> \n> Could you also push 5b208e4a3d somewhere public? I could not fetch\n> it from https://github.com/bk2204/git, whose sha256-interop branch\n> seems to have an older version (c38d0c8475, \"rust: don't expose the\n> internals of ObjectID\"). That would make it easier for shrimech to\n> pick up.\n\nYeah, it's in the history of the temp-sha256-interop branch there.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\nSpeaking for myself only\n"}]}