@@ -74,7 +74,7 @@ pub mod xdp;
7474
7575use std:: {
7676 borrow:: Cow ,
77- ffi:: CString ,
77+ ffi:: { CString , NulError } ,
7878 io,
7979 os:: fd:: { AsFd , BorrowedFd } ,
8080 path:: { Path , PathBuf } ,
@@ -178,6 +178,16 @@ pub enum ProgramError {
178178 name : String ,
179179 } ,
180180
181+ /// The network interface name is invalid.
182+ #[ error( "invalid network interface name {name}" ) ]
183+ InvalidInterfaceName {
184+ /// interface name
185+ name : String ,
186+ /// source error
187+ #[ source]
188+ source : NulError ,
189+ } ,
190+
181191 /// The program is not of the expected type.
182192 #[ error( "unexpected program type" ) ]
183193 UnexpectedProgramType ,
@@ -218,11 +228,24 @@ pub enum ProgramError {
218228 #[ error( transparent) ]
219229 Btf ( #[ from] BtfError ) ,
220230
221- /// The program is not attached .
231+ /// The program name is invalid .
222232 #[ error( "the program name `{name}` is invalid" ) ]
223233 InvalidName {
224234 /// program name
225235 name : String ,
236+ /// source error
237+ #[ source]
238+ source : NulError ,
239+ } ,
240+
241+ /// The path is invalid.
242+ #[ error( "the path `{path}` is invalid" ) ]
243+ InvalidPath {
244+ /// path
245+ path : String ,
246+ /// source error
247+ #[ source]
248+ source : NulError ,
226249 } ,
227250
228251 /// An error occurred while working with IO.
@@ -587,8 +610,12 @@ impl<T: Link> ProgramData<T> {
587610 ) -> Result < Self , ProgramError > {
588611 use std:: os:: unix:: ffi:: OsStrExt as _;
589612
590- // TODO: avoid this unwrap by adding a new error variant.
591- let path_string = CString :: new ( path. as_ref ( ) . as_os_str ( ) . as_bytes ( ) ) . unwrap ( ) ;
613+ let path_string = CString :: new ( path. as_ref ( ) . as_os_str ( ) . as_bytes ( ) ) . map_err ( |source| {
614+ ProgramError :: InvalidPath {
615+ path : path. as_ref ( ) . display ( ) . to_string ( ) ,
616+ source,
617+ }
618+ } ) ?;
592619 let fd = bpf_get_object ( & path_string) . map_err ( |io_error| SyscallError {
593620 call : "bpf_obj_get" ,
594621 io_error,
@@ -685,16 +712,15 @@ fn load_program<T: Link>(
685712 . unwrap_or ( 0 )
686713 } ) ;
687714
688- let prog_name = if let Some ( name) = name. as_deref ( ) {
689- let prog_name = CString :: new ( name) . map_err ( |err @ std:: ffi:: NulError { .. } | {
690- let name = err. into_vec ( ) ;
691- let name = unsafe { String :: from_utf8_unchecked ( name) } ;
692- ProgramError :: InvalidName { name }
693- } ) ?;
694- Some ( prog_name)
695- } else {
696- None
697- } ;
715+ let prog_name = name
716+ . as_deref ( )
717+ . map ( |name| {
718+ CString :: new ( name) . map_err ( |source| {
719+ let name = name. to_owned ( ) ;
720+ ProgramError :: InvalidName { name, source }
721+ } )
722+ } )
723+ . transpose ( ) ?;
698724
699725 let attr = EbpfLoadProgramAttrs {
700726 name : prog_name,
0 commit comments