Skip to content

Commit

Permalink
Bfry/no panic (#42)
Browse files Browse the repository at this point in the history
* mark all valid panic points

* OpCode no longer panic!()

* Changelog message for #37
  • Loading branch information
bluejekyll committed Aug 27, 2016
1 parent 43f9348 commit 0de76b2
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## unreleased
### Fixed
- Randomized ports for client connections and message ids, #23
- OpCode::From for u8 removed, added OpCode::from_u8(), #36

### Changed
- Cleaned up the Server implementation to isolate connection handlers
Expand Down
8 changes: 4 additions & 4 deletions src/authority/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl Authority {
if let &RData::SOA(ref soa_rdata) = soa.get_rdata() {
soa_rdata.get_serial()
} else {
panic!("This was not an SOA record");
panic!("This was not an SOA record"); // valid panic, never should happen
}
}

Expand All @@ -248,7 +248,7 @@ impl Authority {
soa_rdata.increment_serial();
soa_rdata.get_serial()
} else {
panic!("This was not an SOA record");
panic!("This was not an SOA record"); // valid panic, never should happen
};

self.upsert(soa, serial);
Expand Down Expand Up @@ -1158,7 +1158,7 @@ pub mod authority_tests {
assert_eq!(result.first().unwrap().get_dns_class(), DNSClass::IN);
assert_eq!(result.first().unwrap().get_rdata(), &RData::A(Ipv4Addr::new(93,184,216,34)));
} else {
panic!("expected a result");
panic!("expected a result"); // valid panic, in test
}
}

Expand All @@ -1177,7 +1177,7 @@ pub mod authority_tests {
assert_eq!(result.first().unwrap().get_dns_class(), DNSClass::IN);
assert_eq!(result.first().unwrap().get_rdata(), &RData::A(Ipv4Addr::new(93,184,216,34)));
} else {
panic!("expected a result");
panic!("expected a result"); // valid panic, in test
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/authority/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Journal {
match self.version + 1 {
0 => self.version = try!(self.init_up()),
1 => self.version = try!(self.records_up()),
_ => panic!("incorrect version somewhere"),
_ => panic!("incorrect version somewhere"), // valid panic, non-recoverable state
}

try!(self.update_schema_version(self.version));
Expand Down
2 changes: 1 addition & 1 deletion src/authority/rr_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl RRSet {
return false;
}
},
rdata @ _ => panic!("wrong rdata: {:?}", rdata),
rdata @ _ => panic!("wrong rdata: {:?}", rdata), // valid panic, never should happen
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ impl<C: ClientConnection> Client<C> {
if let &RData::SIG(ref sig) = rrsig.get_rdata() { sig.get_type_covered() } else { RecordType::NULL });
}
} else {
panic!("this should be a DNSKEY")
panic!("this should be a DNSKEY") // valid panic, never should happen
}
}
} else {
panic!("expected RRSIG: {:?}", rrsig.get_rr_type());
panic!("expected RRSIG: {:?}", rrsig.get_rr_type()); // valid panic, never should happen
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ impl<C: ClientConnection> Client<C> {
return Ok(proof)
}
} else {
panic!("expected DS: {:?}", ds.get_rr_type());
panic!("expected DS: {:?}", ds.get_rr_type()); // valid panic, never should happen
}
}

Expand Down Expand Up @@ -334,8 +334,6 @@ impl<C: ClientConnection> Client<C> {
// NSEC and RRSIG bits in an NSEC RR.
fn verify_nsec(&self, query_name: &domain::Name, query_type: RecordType,
_: DNSClass, nsecs: Vec<&Record>) -> ClientResult<()> {
debug!("verifying nsec");

// first look for a record with the same name
// if they are, then the query_type should not exist in the NSEC record.
// if we got an NSEC record of the same name, but it is listed in the NSEC types,
Expand All @@ -344,7 +342,7 @@ impl<C: ClientConnection> Client<C> {
if let &RData::NSEC(ref rdata) = r.get_rdata() {
!rdata.get_type_bit_maps().contains(&query_type)
} else {
panic!("expected NSEC was {:?}", r.get_rr_type())
panic!("expected NSEC was {:?}", r.get_rr_type()) // valid panic, never should happen
}
}) { return Ok(()) }

Expand All @@ -353,7 +351,7 @@ impl<C: ClientConnection> Client<C> {
if let &RData::NSEC(ref rdata) = r.get_rdata() {
query_name < rdata.get_next_domain_name()
} else {
panic!("expected NSEC was {:?}", r.get_rr_type())
panic!("expected NSEC was {:?}", r.get_rr_type()) // valid panic, never should happen
}
}) { return Ok(()) }

Expand Down
2 changes: 1 addition & 1 deletion src/op/edns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a> From<&'a Record> for Edns {
},
_ => {
// this should be a coding error, as opposed to a parsing error.
panic!("rr_type doesn't match the RData: {:?}", value.get_rdata());
panic!("rr_type doesn't match the RData: {:?}", value.get_rdata()); // valid panic, never should happen
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/op/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl BinSerializable<Header> for Header {
// if the first bit is set
let message_type = if (0x80 & q_opcd_a_t_r) == 0x80 { MessageType::Response } else { MessageType::Query };
// the 4bit opcode, masked and then shifted right 3bits for the u8...
let op_code: OpCode = ((0x78 & q_opcd_a_t_r) >> 3).into();
let op_code: OpCode = try!(OpCode::from_u8((0x78 & q_opcd_a_t_r) >> 3));
let authoritative = (0x4 & q_opcd_a_t_r) == 0x4;
let truncation = (0x2 & q_opcd_a_t_r) == 0x2;
let recursion_desired = (0x1 & q_opcd_a_t_r) == 0x1;
Expand Down
31 changes: 15 additions & 16 deletions src/op/op_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

use std::convert::From;

use ::error::*;

/// Operation code for queries, updates, and responses
///
/// [RFC 1035, DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION, November 1987](https://tools.ietf.org/html/rfc1035)
Expand Down Expand Up @@ -57,11 +59,11 @@ pub enum OpCode {
/// use std::convert::From;
/// use trust_dns::op::op_code::OpCode;
///
/// let var: OpCode = From::from(0);
/// assert_eq!(OpCode::Query, var);
/// let var: u8 = From::from(OpCode::Query);
/// assert_eq!(0, var);
///
/// let var: OpCode = 0.into();
/// assert_eq!(OpCode::Query, var);
/// let var: u8 = OpCode::Query.into();
/// assert_eq!(0, var);
/// ```
impl From<OpCode> for u8 {
fn from(rt: OpCode) -> Self {
Expand All @@ -83,20 +85,17 @@ impl From<OpCode> for u8 {
/// use std::convert::From;
/// use trust_dns::op::op_code::OpCode;
///
/// let var: u8 = From::from(OpCode::Query);
/// assert_eq!(0, var);
///
/// let var: u8 = OpCode::Query.into();
/// assert_eq!(0, var);
/// let var: OpCode = OpCode::from_u8(0).unwrap();
/// assert_eq!(OpCode::Query, var);
/// ```
impl From<u8> for OpCode {
fn from(value: u8) -> Self {
impl OpCode {
pub fn from_u8(value: u8) -> DecodeResult<Self> {
match value {
0 => OpCode::Query,
2 => OpCode::Status,
4 => OpCode::Notify,
5 => OpCode::Update,
_ => panic!("unimplemented code: {}", value),
0 => Ok(OpCode::Query),
2 => Ok(OpCode::Status),
4 => Ok(OpCode::Notify),
5 => Ok(OpCode::Update),
_ => Err(DecodeErrorKind::Msg(format!("unknown OpCode: {}", value)).into()),
}
}
}
22 changes: 13 additions & 9 deletions src/rr/dnssec/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::str::FromStr;

use openssl::crypto::pkey::{PKey, Role};
use openssl::crypto::rsa::RSA;
use openssl::bn::BigNum;
Expand Down Expand Up @@ -108,7 +110,7 @@ pub enum Algorithm {

impl Algorithm {
pub fn sign(&self, private_key: &PKey, data: &[u8]) -> Vec<u8> {
if !private_key.can(Role::Sign) { panic!("This key cannot be used for signing") }
assert!(private_key.can(Role::Sign), "This key cannot be used for signing");

// calculate the hash...
let hash = DigestType::from(*self).hash(data);
Expand All @@ -118,7 +120,7 @@ impl Algorithm {
}

pub fn verify(&self, public_key: &PKey, data: &[u8], signature: &[u8]) -> bool {
if !public_key.can(Role::Verify) { panic!("This key cannot be used to verify signature") }
assert!(public_key.can(Role::Verify), "This key cannot be used to verify signature");

// calculate the hash on the local data
let hash = DigestType::from(*self).hash(data);
Expand Down Expand Up @@ -247,16 +249,18 @@ impl BinSerializable<Algorithm> for Algorithm {
}
}

impl From<&'static str> for Algorithm {
fn from(s: &'static str) -> Algorithm {
impl FromStr for Algorithm {
type Err = DecodeError;

fn from_str(s: &str) -> DecodeResult<Algorithm> {
match s {
"RSASHA1" => Algorithm::RSASHA1,
"RSASHA256" => Algorithm::RSASHA256,
"RSASHA1-NSEC3-SHA1" => Algorithm::RSASHA1NSEC3SHA1,
"RSASHA512" => Algorithm::RSASHA512,
"RSASHA1" => Ok(Algorithm::RSASHA1),
"RSASHA256" => Ok(Algorithm::RSASHA256),
"RSASHA1-NSEC3-SHA1" => Ok(Algorithm::RSASHA1NSEC3SHA1),
"RSASHA512" => Ok(Algorithm::RSASHA512),
// "ECDSAP256SHA256" => Algorithm::ECDSAP256SHA256,
// "ECDSAP384SHA384" => Algorithm::ECDSAP384SHA384,
_ => panic!("unrecognized string {}", s),
_ => Err(DecodeErrorKind::Msg(format!("unrecognized string {}", s)).into()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rr/dnssec/digest_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl From<DigestType> for u8 {
DigestType::SHA256 => 2,
// DigestType::GOSTR34_11_94 => 3,
DigestType::SHA384 => 4,
_ => panic!("No code for this type: {:?}", a)
DigestType::SHA512 => 255,
}
}
}
24 changes: 12 additions & 12 deletions src/rr/record_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,23 @@ impl RData {
let rdata = match record_type {
RecordType::A => RData::A(try!(rdata::a::parse(tokens))),
RecordType::AAAA => RData::AAAA(try!(rdata::aaaa::parse(tokens))),
RecordType::ANY => panic!("parsing ANY doesn't make sense"),
RecordType::AXFR => panic!("parsing AXFR doesn't make sense"),
RecordType::ANY => panic!("parsing ANY doesn't make sense"), // valid panic, never should happen
RecordType::AXFR => panic!("parsing AXFR doesn't make sense"), // valid panic, never should happen
RecordType::CNAME => RData::CNAME(try!(rdata::name::parse(tokens, origin))),
RecordType::KEY => panic!("KEY should be dynamically generated"),
RecordType::DNSKEY => panic!("DNSKEY should be dynamically generated"),
RecordType::DS => panic!("DS should be dynamically generated"),
RecordType::IXFR => panic!("parsing IXFR doesn't make sense"),
RecordType::KEY => panic!("KEY should be dynamically generated"), // valid panic, never should happen
RecordType::DNSKEY => panic!("DNSKEY should be dynamically generated"), // valid panic, never should happen
RecordType::DS => panic!("DS should be dynamically generated"), // valid panic, never should happen
RecordType::IXFR => panic!("parsing IXFR doesn't make sense"), // valid panic, never should happen
RecordType::MX => RData::MX(try!(rdata::mx::parse(tokens, origin))),
RecordType::NULL => RData::NULL(try!(rdata::null::parse(tokens))),
RecordType::NS => RData::NS(try!(rdata::name::parse(tokens, origin))),
RecordType::NSEC => panic!("NSEC should be dynamically generated"),
RecordType::NSEC3 => panic!("NSEC3 should be dynamically generated"),
RecordType::NSEC3PARAM => panic!("NSEC3PARAM should be dynamically generated"),
RecordType::OPT => panic!("parsing OPT doesn't make sense"),
RecordType::NSEC => panic!("NSEC should be dynamically generated"), // valid panic, never should happen
RecordType::NSEC3 => panic!("NSEC3 should be dynamically generated"), // valid panic, never should happen
RecordType::NSEC3PARAM => panic!("NSEC3PARAM should be dynamically generated"), // valid panic, never should happen
RecordType::OPT => panic!("parsing OPT doesn't make sense"), // valid panic, never should happen
RecordType::PTR => RData::PTR(try!(rdata::name::parse(tokens, origin))),
RecordType::RRSIG => panic!("RRSIG should be dynamically generated"),
RecordType::SIG => panic!("parsing SIG doesn't make sense"),
RecordType::RRSIG => panic!("RRSIG should be dynamically generated"), // valid panic, never should happen
RecordType::SIG => panic!("parsing SIG doesn't make sense"), // valid panic, never should happen
RecordType::SOA => RData::SOA(try!(rdata::soa::parse(tokens, origin))),
RecordType::SRV => RData::SRV(try!(rdata::srv::parse(tokens, origin))),
RecordType::TXT => RData::TXT(try!(rdata::txt::parse(tokens))),
Expand Down
2 changes: 1 addition & 1 deletion src/serialize/txt/master_lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ mod lex_test {

fn next_token(lexer: &mut Lexer) -> Option<Token> {
let result = lexer.next_token();
if result.is_err() { panic!("{:?}", result) }
assert!(!result.is_err(), "{:?}", result);
result.unwrap()
}

Expand Down
18 changes: 9 additions & 9 deletions src/serialize/txt/txt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ venera A 10.1.0.52
assert_eq!(3600000, soa.get_expire());
assert_eq!(60, soa.get_minimum());
} else {
panic!("Not an SOA record!!!")
panic!("Not an SOA record!!!") // valid panic, test code
}

// NS
Expand All @@ -84,7 +84,7 @@ venera A 10.1.0.52
if let RData::NS( ref nsdname ) = *record.get_rdata() {
assert_eq!(name, nsdname);
} else {
panic!("Not an NS record!!!")
panic!("Not an NS record!!!") // valid panic, test code
}
}

Expand All @@ -109,7 +109,7 @@ venera A 10.1.0.52
assert_eq!(num, rdata.get_preference());
assert_eq!(name, rdata.get_exchange());
} else {
panic!("Not an NS record!!!")
panic!("Not an NS record!!!") // valid panic, test code
}
}

Expand All @@ -122,7 +122,7 @@ venera A 10.1.0.52
if let RData::A(ref address) = *a_record.get_rdata() {
assert_eq!(&Ipv4Addr::new(26u8,3u8,0u8,103u8), address);
} else {
panic!("Not an A record!!!")
panic!("Not an A record!!!") // valid panic, test code
}

// AAAA
Expand All @@ -131,7 +131,7 @@ venera A 10.1.0.52
if let RData::AAAA(ref address) = *aaaa_record.get_rdata() {
assert_eq!(&Ipv6Addr::from_str("4321:0:1:2:3:4:567:89ab").unwrap(), address);
} else {
panic!("Not a AAAA record!!!")
panic!("Not a AAAA record!!!") // valid panic, test code
}

// SHORT
Expand All @@ -141,7 +141,7 @@ venera A 10.1.0.52
if let RData::A(ref address) = *short_record.get_rdata() {
assert_eq!(&Ipv4Addr::new(26u8,3u8,0u8,104u8), address);
} else {
panic!("Not an A record!!!")
panic!("Not an A record!!!") // valid panic, test code
}

// TXT
Expand All @@ -165,7 +165,7 @@ venera A 10.1.0.52
if let RData::TXT(ref rdata) = *record.get_rdata() {
assert_eq!(vector as &[String], rdata.get_txt_data());
} else {
panic!("Not a TXT record!!!")
panic!("Not a TXT record!!!") // valid panic, test code
}
}

Expand All @@ -174,7 +174,7 @@ venera A 10.1.0.52
if let RData::PTR( ref ptrdname ) = *ptr_record.get_rdata() {
assert_eq!(&Name::new().label("a").label("isi").label("edu"), ptrdname);
} else {
panic!("Not a PTR record!!!")
panic!("Not a PTR record!!!") // valid panic, test code
}

// SRV
Expand All @@ -185,6 +185,6 @@ venera A 10.1.0.52
assert_eq!(rdata.get_port(), 3);
assert_eq!(rdata.get_target(), &Name::new().label("short").label("isi").label("edu"));
} else {
panic!("Not an SRV record!!!")
panic!("Not an SRV record!!!") // valid panic, test code
}
}
2 changes: 1 addition & 1 deletion src/udp/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl UdpHandler {
Ok(UdpState::Writing)
}
},
UdpState::Done => panic!("This handler should have been removed or reset"),
UdpState::Done => panic!("This handler should have been removed or reset"), // valid panic, never should happen
}
}
}
Expand Down

1 comment on commit 0de76b2

@bluejekyll
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #36

Please sign in to comment.