File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -563,6 +563,15 @@ impl Stater {
563563 if let Some ( ( field_width, offset) ) = format_str[ j..] . scan_num :: < usize > ( ) {
564564 width = field_width;
565565 j += offset;
566+
567+ // Reject directives like `%<NUMBER>` by checking if width has been parsed.
568+ if j >= bound || chars[ j] == '%' {
569+ let invalid_directive: String = chars[ old..=j. min ( bound - 1 ) ] . iter ( ) . collect ( ) ;
570+ return Err ( USimpleError :: new (
571+ 1 ,
572+ format ! ( "{}: invalid directive" , invalid_directive. quote( ) ) ,
573+ ) ) ;
574+ }
566575 }
567576 check_bound ( format_str, bound, old, j) ?;
568577
Original file line number Diff line number Diff line change @@ -412,3 +412,20 @@ fn test_printf_bel_etc() {
412412 . succeeds ( )
413413 . stdout_is_bytes ( expected_stdout) ;
414414}
415+
416+ #[ test]
417+ fn test_printf_invalid_directive ( ) {
418+ let ts = TestScenario :: new ( util_name ! ( ) ) ;
419+
420+ ts. ucmd ( )
421+ . args ( & [ "--printf=%9" , "." ] )
422+ . fails ( )
423+ . code_is ( 1 )
424+ . stderr_contains ( "'%9': invalid directive" ) ;
425+
426+ ts. ucmd ( )
427+ . args ( & [ "--printf=%9%" , "." ] )
428+ . fails ( )
429+ . code_is ( 1 )
430+ . stderr_contains ( "'%9%': invalid directive" ) ;
431+ }
You can’t perform that action at this time.
0 commit comments