Skip to content

Commit 75614a7

Browse files
authored
Merge pull request #3344 from e-kwsm/find
feat(SC2016,SC2033,SC2067,SC2150,SC2156): check `find -ok`/`-okdir`
2 parents 9d938e6 + 6db7f1e commit 75614a7

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/ShellCheck/Analytics.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,9 @@ checkFindExec _ cmd@(T_SimpleCommand _ _ t@(h:r)) | cmd `isCommand` "find" = do
768768
when v (mapM_ warnFor $ fromWord w)
769769
case getLiteralString w of
770770
Just "-exec" -> broken r True
771-
Just "-execdir" -> broken r True
771+
Just "-execdir" -> broken r True -- n.b. -execdir ;/+ is not defined in POSIX
772+
Just "-ok" -> broken r True -- n.b. -ok + is not defined in POSIX
773+
Just "-okdir" -> broken r True -- n.b. -okdir ;/+ is not defined in POSIX
772774
Just "+" -> broken r False
773775
Just ";" -> broken r False
774776
_ -> broken r v
@@ -1142,7 +1144,7 @@ checkSingleQuotedVariables params t@(T_SingleQuoted id s) =
11421144

11431145
getFindCommand (T_SimpleCommand _ _ words) =
11441146
let list = map getLiteralString words
1145-
cmd = dropWhile (\x -> x /= Just "-exec" && x /= Just "-execdir") list
1147+
cmd = dropWhile (\x -> x `notElem` map Just ["-exec", "-execdir", "-ok", "-okdir"]) list
11461148
in
11471149
case cmd of
11481150
(flag:cmd:rest) -> fromMaybe "find" cmd
@@ -2336,7 +2338,7 @@ prop_checkFunctionsUsedExternally2 =
23362338
prop_checkFunctionsUsedExternally2b =
23372339
verifyNotTree checkFunctionsUsedExternally "alias f='a'; find . -type f"
23382340
prop_checkFunctionsUsedExternally2c =
2339-
verifyTree checkFunctionsUsedExternally "alias f='a'; find . -type f -exec f +"
2341+
verifyTree checkFunctionsUsedExternally "alias f='a'; find . -type f -exec f {} +"
23402342
prop_checkFunctionsUsedExternally3 =
23412343
verifyNotTree checkFunctionsUsedExternally "f() { :; }; echo f"
23422344
prop_checkFunctionsUsedExternally4 =
@@ -2387,7 +2389,7 @@ checkFunctionsUsedExternally params t =
23872389
_ -> []
23882390
where
23892391
firstNonFlag = take 1 $ dropFlags argAndString
2390-
findExecFlags = ["-exec", "-execdir", "-ok"]
2392+
findExecFlags = ["-exec", "-execdir", "-ok", "-okdir"]
23912393
dropFlags = dropWhile (\x -> "-" `isPrefixOf` fst x)
23922394

23932395
functionsAndAliases = Map.union (functions t) (aliases t)

src/ShellCheck/Checks/Commands.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,9 @@ checkFindExecWithSingleArgument = CommandCheck (Basename "find") (f . arguments)
470470
termS <- getLiteralString term
471471
let cmdS = getLiteralStringDef " " arg
472472

473-
guard $ execS `elem` ["-exec", "-execdir"] && termS `elem` [";", "+"]
473+
guard $ execS `elem` ["-exec", "-execdir", "-ok", "-okdir"] && termS `elem` [";", "+"]
474474
guard $ cmdS `matches` commandRegex
475-
return $ warn (getId exec) 2150 "-exec does not invoke a shell. Rewrite or use -exec sh -c .. ."
475+
return $ warn (getId exec) 2150 (execS ++ " does not invoke a shell. Rewrite or use " ++ execS ++ " sh -c .. .")
476476
check _ = Nothing
477477
commandRegex = mkRegex "[ |;]"
478478

@@ -505,7 +505,7 @@ checkUnusedEchoEscapes = CommandCheck (Basename "echo") f
505505

506506
prop_checkInjectableFindSh1 = verify checkInjectableFindSh "find . -exec sh -c 'echo {}' \\;"
507507
prop_checkInjectableFindSh2 = verify checkInjectableFindSh "find . -execdir bash -c 'rm \"{}\"' ';'"
508-
prop_checkInjectableFindSh3 = verifyNot checkInjectableFindSh "find . -exec sh -c 'rm \"$@\"' _ {} \\;"
508+
prop_checkInjectableFindSh3 = verifyNot checkInjectableFindSh "find . -ok sh -c 'rm \"$@\"' _ {} \\;"
509509
checkInjectableFindSh = CommandCheck (Basename "find") (check . arguments)
510510
where
511511
check args = do
@@ -519,7 +519,7 @@ checkInjectableFindSh = CommandCheck (Basename "find") (check . arguments)
519519
match (p:tests) args
520520

521521
pattern = [
522-
(`elem` ["-exec", "-execdir"]),
522+
(`elem` ["-exec", "-execdir", "-ok", "-okdir"]),
523523
(`elem` ["sh", "bash", "dash", "ksh"]),
524524
(== "-c")
525525
]

0 commit comments

Comments
 (0)