From 1a3c87f9f19ec43579b3342e2e95e3f49de1bfc1 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 20 Aug 2023 11:23:17 -0400 Subject: [PATCH] fix nul termination --- veilid-core/src/intf/native/system.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/veilid-core/src/intf/native/system.rs b/veilid-core/src/intf/native/system.rs index ef8cbddc..19efca93 100644 --- a/veilid-core/src/intf/native/system.rs +++ b/veilid-core/src/intf/native/system.rs @@ -80,14 +80,14 @@ pub async fn txt_lookup>(host: S) -> EyreResult> { if #[cfg(target_os = "windows")] { use core::ffi::c_void; use windows::core::{PSTR,PCSTR}; - use std::ffi::CStr; + use std::ffi::{CStr, CString}; use windows::Win32::NetworkManagement::Dns::{DnsQuery_UTF8, DnsFree, DNS_TYPE_TEXT, DNS_QUERY_STANDARD, DNS_RECORDA, DnsFreeRecordList}; let mut out = Vec::new(); unsafe { let mut p_query_results: *mut DNS_RECORDA = core::ptr::null_mut(); - let host = host.as_ref().to_string(); - DnsQuery_UTF8(PCSTR::from_raw(host.as_ptr()), DNS_TYPE_TEXT, DNS_QUERY_STANDARD, None, &mut p_query_results as *mut *mut DNS_RECORDA, None).wrap_err("Failed to resolve TXT record")?; + let host = CString::new(host.as_ref()).wrap_err("invalid host string")?; + DnsQuery_UTF8(PCSTR::from_raw(host.as_bytes_with_nul().as_ptr()), DNS_TYPE_TEXT, DNS_QUERY_STANDARD, None, &mut p_query_results as *mut *mut DNS_RECORDA, None).wrap_err("Failed to resolve TXT record")?; let mut p_record: *mut DNS_RECORDA = p_query_results; while !p_record.is_null() { @@ -138,7 +138,7 @@ pub async fn ptr_lookup(ip_addr: IpAddr) -> EyreResult { if #[cfg(target_os = "windows")] { use core::ffi::c_void; use windows::core::{PSTR,PCSTR}; - use std::ffi::CStr; + use std::ffi::{CStr, CString}; use windows::Win32::NetworkManagement::Dns::{DnsQuery_UTF8, DnsFree, DNS_TYPE_PTR, DNS_QUERY_STANDARD, DNS_RECORDA, DnsFreeRecordList}; let host = match ip_addr { @@ -157,7 +157,8 @@ pub async fn ptr_lookup(ip_addr: IpAddr) -> EyreResult { unsafe { let mut p_query_results: *mut DNS_RECORDA = core::ptr::null_mut(); - DnsQuery_UTF8(PCSTR::from_raw(host.as_ptr()), DNS_TYPE_PTR, DNS_QUERY_STANDARD, None, &mut p_query_results as *mut *mut DNS_RECORDA, None).wrap_err("Failed to resolve PTR record")?; + let host = CString::new(host).wrap_err("invalid host string")?; + DnsQuery_UTF8(PCSTR::from_raw(host.as_bytes_with_nul().as_ptr()), DNS_TYPE_PTR, DNS_QUERY_STANDARD, None, &mut p_query_results as *mut *mut DNS_RECORDA, None).wrap_err("Failed to resolve PTR record")?; let mut p_record: *mut DNS_RECORDA = p_query_results; while !p_record.is_null() {