@@ -3096,6 +3096,59 @@ mod test {
30963096 ) ;
30973097 }
30983098
3099+ #[ test]
3100+ fn copy_invalid_allocator ( ) {
3101+ use crate :: c_api:: { uInt, voidpf} ;
3102+
3103+ {
3104+ unsafe extern "C" fn failing_allocator ( _: voidpf , _: uInt , _: uInt ) -> voidpf {
3105+ std:: ptr:: null_mut ( )
3106+ }
3107+
3108+ let mut stream = z_stream:: default ( ) ;
3109+ assert_eq ! ( init( & mut stream, DeflateConfig :: default ( ) ) , ReturnCode :: Ok ) ;
3110+
3111+ stream. zalloc = Some ( failing_allocator) ;
3112+ let Some ( stream) = ( unsafe { DeflateStream :: from_stream_mut ( & mut stream) } ) else {
3113+ unreachable ! ( )
3114+ } ;
3115+
3116+ let mut stream_copy = MaybeUninit :: < DeflateStream > :: zeroed ( ) ;
3117+
3118+ assert_eq ! ( copy( & mut stream_copy, stream) , ReturnCode :: MemError ) ;
3119+ }
3120+
3121+ {
3122+ unsafe extern "C" fn unreliable_allocator (
3123+ opaque : voidpf ,
3124+ items : uInt ,
3125+ size : uInt ,
3126+ ) -> voidpf {
3127+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
3128+
3129+ static X : AtomicUsize = AtomicUsize :: new ( 0 ) ;
3130+
3131+ if X . fetch_add ( 1 , Ordering :: Relaxed ) < 4 {
3132+ crate :: allocate:: zcalloc ( opaque, items, size)
3133+ } else {
3134+ std:: ptr:: null_mut ( )
3135+ }
3136+ }
3137+
3138+ let mut stream = z_stream:: default ( ) ;
3139+ assert_eq ! ( init( & mut stream, DeflateConfig :: default ( ) ) , ReturnCode :: Ok ) ;
3140+
3141+ stream. zalloc = Some ( unreliable_allocator) ;
3142+ let Some ( stream) = ( unsafe { DeflateStream :: from_stream_mut ( & mut stream) } ) else {
3143+ unreachable ! ( )
3144+ } ;
3145+
3146+ let mut stream_copy = MaybeUninit :: < DeflateStream > :: zeroed ( ) ;
3147+
3148+ assert_eq ! ( copy( & mut stream_copy, stream) , ReturnCode :: MemError ) ;
3149+ }
3150+ }
3151+
30993152 #[ test]
31003153 fn invalid_deflate_config ( ) {
31013154 let mut stream = z_stream:: default ( ) ;
0 commit comments