File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,29 @@ macro_rules! assert_eq_fmt {
5959 } ;
6060}
6161
62+ /// Assert that a format string fails to parse. This checks that both
63+ /// C's `asprintf` and `printf_compat::format` return -1.
64+ fn assert_fmt_err ( fmt : & CStr ) {
65+ let mut ptr = null_mut ( ) ;
66+ let bytes_written = unsafe { asprintf ( & mut ptr, fmt. as_ptr ( ) ) } ;
67+ assert_eq ! ( bytes_written, -1 , "asprintf parse unexpectedly succeeded" ) ;
68+
69+ unsafe extern "C" fn format ( str : * const c_char , args: ...) -> c_int {
70+ let mut s = String :: new ( ) ;
71+ let bytes_written = printf_compat:: format (
72+ str,
73+ args. clone ( ) . as_va_list ( ) ,
74+ printf_compat:: output:: fmt_write ( & mut s) ,
75+ ) ;
76+ bytes_written
77+ }
78+ let bytes_written = unsafe { format ( fmt. as_ptr ( ) ) } ;
79+ assert_eq ! (
80+ bytes_written, -1 ,
81+ "printf_compat::output parse unexpectedly succeeded"
82+ ) ;
83+ }
84+
6285#[ test]
6386fn test_plain ( ) {
6487 unsafe {
@@ -229,3 +252,9 @@ fn test_char() {
229252 assert_eq_fmt ! ( c"%-10c" , b'a' as c_int => "a " ) ;
230253 }
231254}
255+
256+ #[ test]
257+ fn test_errors ( ) {
258+ assert_fmt_err ( c"%" ) ;
259+ assert_fmt_err ( c"%1" ) ;
260+ }
You can’t perform that action at this time.
0 commit comments