@@ -2189,4 +2189,39 @@ mod gzprintf {
21892189 let _ = read;
21902190 }
21912191 }
2192+
2193+ #[ test]
2194+ fn test_gzprintf_buffer_too_large ( ) {
2195+ let _ = crate :: assert_eq_rs_ng!( {
2196+ // Use a small buffer so we can hit the limit easily.
2197+ let path = "gzprintf-too-large.gz" ;
2198+ let file = unsafe {
2199+ gzopen(
2200+ CString :: new( crate_path( path) ) . unwrap( ) . as_ptr( ) ,
2201+ CString :: new( "w" ) . unwrap( ) . as_ptr( ) ,
2202+ )
2203+ } ;
2204+ assert!( !file. is_null( ) ) ;
2205+
2206+ // Make the buffer just 16 bytes.
2207+ assert_eq!( unsafe { gzbuffer( file, 16 ) } , 0 ) ;
2208+
2209+ // This input does not fit, and hence gzprintf returns 0.
2210+ let too_large = CString :: new( "1234567890abcdef" ) . unwrap( ) ;
2211+ assert_eq!( too_large. as_bytes( ) . len( ) , 16 ) ;
2212+ let written = unsafe { gzprintf( file, too_large. as_ptr( ) ) } ;
2213+ assert_eq!( written, 0 ) ;
2214+
2215+ // Now write something that does fit (15 bytes).
2216+ let fits = CString :: new( "1234567890abcde" ) . unwrap( ) ;
2217+ assert_eq!( fits. as_bytes( ) . len( ) , 15 ) ;
2218+
2219+ let written_ok = unsafe { gzprintf( file, fits. as_ptr( ) ) } ;
2220+ assert_eq!( written_ok, 15 ) ;
2221+
2222+ assert_eq!( unsafe { gzclose( file) } , Z_OK ) ;
2223+
2224+ std:: fs:: read( crate_path( path) ) . unwrap( )
2225+ } ) ;
2226+ }
21922227}
0 commit comments