@@ -2,6 +2,7 @@ use std::{
22 collections:: { HashMap , HashSet } ,
33 ffi:: { OsStr , OsString } ,
44 os:: unix:: prelude:: OsStrExt ,
5+ path:: Path ,
56} ;
67
78use crate :: common:: { CommandAndArguments , Context , Error } ;
@@ -10,7 +11,17 @@ use crate::system::PATH_MAX;
1011
1112use super :: wildcard_match:: wildcard_match;
1213
13- const PATH_ZONEINFO : & str = env ! ( "PATH_ZONEINFO" ) ;
14+ fn path_zoneinfo ( ) -> Option < & ' static str > {
15+ [
16+ "/usr/share/zoneinfo" ,
17+ "/usr/share/lib/zoneinfo" ,
18+ "/usr/lib/zoneinfo" ,
19+ "/usr/lib/zoneinfo" ,
20+ ]
21+ . into_iter ( )
22+ . find ( |p| Path :: new ( p) . exists ( ) )
23+ }
24+
1425// TODO: use _PATH_STDPATH from paths.h
1526pub ( crate ) const PATH_DEFAULT : & str = "/usr/bin:/bin:/usr/sbin:/sbin" ;
1627
@@ -139,12 +150,9 @@ fn is_safe_tz(value: &[u8]) -> bool {
139150 } ;
140151
141152 if check_value. starts_with ( b"/" ) {
142- // clippy 1.79 wants to us to optimise this check away; but we don't know what this will always
143- // be possible; and the compiler is clever enough to do that for us anyway if it can be.
144- #[ allow( clippy:: const_is_empty) ]
145- if !PATH_ZONEINFO . is_empty ( ) {
146- if !check_value. starts_with ( PATH_ZONEINFO . as_bytes ( ) )
147- || check_value. get ( PATH_ZONEINFO . len ( ) ) != Some ( & b'/' )
153+ if let Some ( path_zoneinfo) = path_zoneinfo ( ) {
154+ if !check_value. starts_with ( path_zoneinfo. as_bytes ( ) )
155+ || check_value. get ( path_zoneinfo. len ( ) ) != Some ( & b'/' )
148156 {
149157 return false ;
150158 }
@@ -256,7 +264,7 @@ where
256264
257265#[ cfg( test) ]
258266mod tests {
259- use super :: { is_safe_tz, should_keep , PATH_ZONEINFO } ;
267+ use super :: { is_safe_tz, path_zoneinfo , should_keep } ;
260268 use std:: { collections:: HashSet , ffi:: OsStr } ;
261269
262270 struct TestConfiguration {
@@ -318,25 +326,27 @@ mod tests {
318326 #[ allow( clippy:: bool_assert_comparison) ]
319327 #[ test]
320328 fn test_tzinfo ( ) {
329+ let path_zoneinfo = path_zoneinfo ( ) . unwrap ( ) ;
321330 assert_eq ! ( is_safe_tz( "Europe/Amsterdam" . as_bytes( ) ) , true ) ;
322331 assert_eq ! (
323- is_safe_tz( format!( "{PATH_ZONEINFO }/Europe/London" ) . as_bytes( ) ) ,
332+ is_safe_tz( format!( "{path_zoneinfo }/Europe/London" ) . as_bytes( ) ) ,
324333 true
325334 ) ;
326335 assert_eq ! (
327- is_safe_tz( format!( ":{PATH_ZONEINFO }/Europe/Amsterdam" ) . as_bytes( ) ) ,
336+ is_safe_tz( format!( ":{path_zoneinfo }/Europe/Amsterdam" ) . as_bytes( ) ) ,
328337 true
329338 ) ;
339+ assert_eq ! ( is_safe_tz( format!( "/Europe/Amsterdam" ) . as_bytes( ) ) , false ) ;
330340 assert_eq ! (
331341 is_safe_tz( format!( "/schaap/Europe/Amsterdam" ) . as_bytes( ) ) ,
332342 false
333343 ) ;
334344 assert_eq ! (
335- is_safe_tz( format!( "{PATH_ZONEINFO }/../Europe/London" ) . as_bytes( ) ) ,
345+ is_safe_tz( format!( "{path_zoneinfo }/../Europe/London" ) . as_bytes( ) ) ,
336346 false
337347 ) ;
338348 assert_eq ! (
339- is_safe_tz( format!( "{PATH_ZONEINFO }/../Europe/London" ) . as_bytes( ) ) ,
349+ is_safe_tz( format!( "{path_zoneinfo }/../Europe/London" ) . as_bytes( ) ) ,
340350 false
341351 ) ;
342352 }
0 commit comments