From b3a9d2057c8cd919f4b22b92cc4da6beb25b5735 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Wed, 5 Jun 2024 20:20:55 +0100 Subject: [PATCH] c-ares 1.30 checks dns strings --- build.rs | 5 +++++ src/caa.rs | 4 ++-- src/naptr.rs | 8 ++++---- src/uri.rs | 4 ++-- src/utils.rs | 10 ++++++++++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index 10d1a7320..ba0718473 100644 --- a/build.rs +++ b/build.rs @@ -48,5 +48,10 @@ fn main() { if version >= 0x1_1d_00 { println!("cargo:rustc-cfg=cares1_29"); } + + println!("cargo::rustc-check-cfg=cfg(cares1_30)"); + if version >= 0x1_1e_00 { + println!("cargo:rustc-cfg=cares1_30"); + } } } diff --git a/src/caa.rs b/src/caa.rs index ec6162102..e333b0ce9 100644 --- a/src/caa.rs +++ b/src/caa.rs @@ -6,7 +6,7 @@ use itertools::Itertools; use crate::error::{Error, Result}; use crate::panic; -use crate::utils::c_string_as_str_checked; +use crate::utils::dns_string_as_str; /// The result of a successful CAA lookup. #[derive(Debug)] @@ -104,7 +104,7 @@ impl<'a> CAAResult<'a> { /// The property represented by this `CAAResult`. pub fn property(self) -> &'a str { - unsafe { c_string_as_str_checked(self.caa_reply.property.cast()) } + unsafe { dns_string_as_str(self.caa_reply.property.cast()) } } /// The value represented by this `CAAResult`. diff --git a/src/naptr.rs b/src/naptr.rs index b8591fb86..2748a2935 100644 --- a/src/naptr.rs +++ b/src/naptr.rs @@ -8,7 +8,7 @@ use itertools::Itertools; use crate::error::{Error, Result}; use crate::panic; -use crate::utils::{c_string_as_str_checked, hostname_as_str}; +use crate::utils::{dns_string_as_str, hostname_as_str}; /// The result of a successful NAPTR lookup. #[derive(Debug)] @@ -100,17 +100,17 @@ unsafe impl<'a> Sync for NAPTRResultsIter<'a> {} impl<'a> NAPTRResult<'a> { /// Returns the flags in this `NAPTRResult`. pub fn flags(self) -> &'a str { - unsafe { c_string_as_str_checked(self.naptr_reply.flags.cast()) } + unsafe { dns_string_as_str(self.naptr_reply.flags.cast()) } } /// Returns the service name in this `NAPTRResult`. pub fn service_name(self) -> &'a str { - unsafe { c_string_as_str_checked(self.naptr_reply.service.cast()) } + unsafe { dns_string_as_str(self.naptr_reply.service.cast()) } } /// Returns the regular expression in this `NAPTRResult`. pub fn reg_exp(self) -> &'a str { - unsafe { c_string_as_str_checked(self.naptr_reply.regexp.cast()) } + unsafe { dns_string_as_str(self.naptr_reply.regexp.cast()) } } /// Returns the replacement pattern in this `NAPTRResult`. diff --git a/src/uri.rs b/src/uri.rs index 96521408d..d86813aec 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -8,7 +8,7 @@ use itertools::Itertools; use crate::error::{Error, Result}; use crate::panic; -use crate::utils::c_string_as_str_checked; +use crate::utils::dns_string_as_str; /// The result of a successful URI lookup. #[derive(Debug)] @@ -111,7 +111,7 @@ impl<'a> URIResult<'a> { /// Returns the uri in this `URIResult`. pub fn uri(self) -> &'a str { - unsafe { c_string_as_str_checked(self.uri_reply.uri) } + unsafe { dns_string_as_str(self.uri_reply.uri) } } /// Returns the time-to-live in this `URIResult`. diff --git a/src/utils.rs b/src/utils.rs index 0a77c1781..c853f4159 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -175,6 +175,16 @@ pub unsafe fn hostname_as_str<'a>(hostname: *const c_char) -> &'a str { c_string_as_str_unchecked(hostname) } +#[cfg(not(cares1_30))] +pub unsafe fn dns_string_as_str<'a>(hostname: *const c_char) -> &'a str { + c_string_as_str_checked(hostname) +} + +#[cfg(cares1_30)] +pub unsafe fn dns_string_as_str<'a>(hostname: *const c_char) -> &'a str { + c_string_as_str_unchecked(hostname) +} + /// Get the version number of the underlying `c-ares` library. /// /// The version is returned as both a string and an integer. The integer is built up as 24bit