From 6f45aa9937602f3d03cd83cfb5fb3c541fac4adf Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 12 Dec 2024 11:31:51 +0000 Subject: fix: defend against empty`tags` prevent a panic when tags are of an unexpect length --- src/bin/git_remote_nostr/push.rs | 15 +++++++++++---- src/bin/git_remote_nostr/utils.rs | 6 +++--- src/bin/ngit/sub_commands/list.rs | 6 +++--- 3 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src/bin') diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 6116fe2..4066312 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -1010,7 +1010,8 @@ async fn get_merged_proposals_info( .iter() .filter(|e| { e.tags.iter().any(|t| { - t.as_slice()[0].eq("commit") + t.as_slice().len() > 1 + && t.as_slice()[0].eq("commit") && t.as_slice()[1].eq(&parent.id().to_string()) }) }) @@ -1034,7 +1035,9 @@ async fn get_merged_proposals_info( .iter() .filter(|e| { e.tags.iter().any(|t| { - t.as_slice()[0].eq("commit") && t.as_slice()[1].eq(&commit_hash.to_string()) + t.as_slice().len() > 1 + && t.as_slice()[0].eq("commit") + && t.as_slice()[1].eq(&commit_hash.to_string()) }) }) .collect::>() @@ -1228,7 +1231,11 @@ async fn get_proposal_and_revision_root_from_patch( git_repo: &Repo, patch: &Event, ) -> Result<(EventId, Option)> { - let proposal_or_revision = if patch.tags.iter().any(|t| t.as_slice()[1].eq("root")) { + let proposal_or_revision = if patch + .tags + .iter() + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) + { patch.clone() } else { let proposal_or_revision_id = EventId::parse( @@ -1260,7 +1267,7 @@ async fn get_proposal_and_revision_root_from_patch( if proposal_or_revision .tags .iter() - .any(|t| t.as_slice()[1].eq("revision-root")) + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("revision-root")) { Ok(( EventId::parse( diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index b54ce6b..5efb6e0 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -128,9 +128,9 @@ pub async fn get_open_proposals( .iter() .filter(|e| { status_kinds().contains(&e.kind) - && e.tags - .iter() - .any(|t| t.as_slice()[1].eq(&proposal.id.to_string())) + && e.tags.iter().any(|t| { + t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string()) + }) }) .collect::>() .first() diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs index b7bfd24..ee015ef 100644 --- a/src/bin/ngit/sub_commands/list.rs +++ b/src/bin/ngit/sub_commands/list.rs @@ -77,9 +77,9 @@ pub async fn launch() -> Result<()> { .iter() .filter(|e| { status_kinds().contains(&e.kind) - && e.tags - .iter() - .any(|t| t.as_slice()[1].eq(&proposal.id.to_string())) + && e.tags.iter().any(|t| { + t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string()) + }) }) .collect::>() .first() -- cgit v1.2.3