Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in MetadataKey::from_bytes #1246

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Changes from 2 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
24 changes: 23 additions & 1 deletion tonic/src/metadata/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<VE: ValueEncoding> MetadataKey<VE> {
match HeaderName::from_bytes(src) {
Ok(name) => {
if !VE::is_valid_key(name.as_str()) {
panic!("invalid metadata key")
return Err(InvalidMetadataKey::new());
}

Ok(MetadataKey {
Expand Down Expand Up @@ -282,3 +282,25 @@ impl Default for InvalidMetadataKey {
}

impl Error for InvalidMetadataKey {}

#[test]
fn test_from_bytes_binary() {
assert!(BinaryMetadataKey::from_bytes(b"").is_err());
assert!(BinaryMetadataKey::from_bytes(b"\xFF").is_err());
assert!(BinaryMetadataKey::from_bytes(b"abc").is_err());
assert_eq!(
BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(),
"abc-bin"
);
}

#[test]
fn test_from_bytes_ascii() {
assert!(AsciiMetadataKey::from_bytes(b"").is_err());
assert!(AsciiMetadataKey::from_bytes(b"\xFF").is_err());
assert_eq!(
AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(),
"abc"
);
assert!(AsciiMetadataKey::from_bytes(b"abc-bin").is_err());
}
andrewhickman marked this conversation as resolved.
Show resolved Hide resolved