Warn when printf arg count is not a multiple of format count

This commit is contained in:
Vidar Holen 2018-03-28 08:56:40 -07:00
parent ffed7caff4
commit 4aca1ff128
2 changed files with 4 additions and 1 deletions

View File

@ -18,6 +18,7 @@
### Changed ### Changed
- SC1073: 'else if' is now parsed correctly and not like 'elif' - SC1073: 'else if' is now parsed correctly and not like 'elif'
- SC2163: 'export $name' can now be silenced with 'export ${name?}' - SC2163: 'export $name' can now be silenced with 'export ${name?}'
- SC2183: Now warns when printf arg count is not a multiple of format count
## v0.4.7 - 2017-12-08 ## v0.4.7 - 2017-12-08
### Added ### Added

View File

@ -511,6 +511,8 @@ prop_checkPrintfVar8 = verifyNot checkPrintfVar "printf '%s %s %s' \"${var[@]}\"
prop_checkPrintfVar9 = verifyNot checkPrintfVar "printf '%s %s %s\\n' *.png" prop_checkPrintfVar9 = verifyNot checkPrintfVar "printf '%s %s %s\\n' *.png"
prop_checkPrintfVar10= verifyNot checkPrintfVar "printf '%s %s %s' foo bar baz" prop_checkPrintfVar10= verifyNot checkPrintfVar "printf '%s %s %s' foo bar baz"
prop_checkPrintfVar11= verifyNot checkPrintfVar "printf '%(%s%s)T' -1" prop_checkPrintfVar11= verifyNot checkPrintfVar "printf '%(%s%s)T' -1"
prop_checkPrintfVar12= verify checkPrintfVar "printf '%s %s\\n' 1 2 3"
prop_checkPrintfVar13= verifyNot checkPrintfVar "printf '%s %s\\n' 1 2 3 4"
checkPrintfVar = CommandCheck (Exactly "printf") (f . arguments) where checkPrintfVar = CommandCheck (Exactly "printf") (f . arguments) where
f (doubledash:rest) | getLiteralString doubledash == Just "--" = f rest f (doubledash:rest) | getLiteralString doubledash == Just "--" = f rest
f (dashv:var:rest) | getLiteralString dashv == Just "-v" = f rest f (dashv:var:rest) | getLiteralString dashv == Just "-v" = f rest
@ -536,7 +538,7 @@ checkPrintfVar = CommandCheck (Exactly "printf") (f . arguments) where
"This printf format string has no variables. Other arguments are ignored." "This printf format string has no variables. Other arguments are ignored."
when (vars > 0 when (vars > 0
&& length more < vars && ((length more) `mod` vars /= 0 || null more)
&& all (not . mayBecomeMultipleArgs) more) $ && all (not . mayBecomeMultipleArgs) more) $
warn (getId format) 2183 $ warn (getId format) 2183 $
"This format string has " ++ show vars ++ " variables, but is passed " ++ show (length more) ++ " arguments." "This format string has " ++ show vars ++ " variables, but is passed " ++ show (length more) ++ " arguments."