diff --git a/src/Tokstyle/C/Linter/SizeArg.hs b/src/Tokstyle/C/Linter/SizeArg.hs index eaf8412..17ffb53 100644 --- a/src/Tokstyle/C/Linter/SizeArg.hs +++ b/src/Tokstyle/C/Linter/SizeArg.hs @@ -27,7 +27,7 @@ import Tokstyle.C.TraverseAst (AstActions (..), astActions, checkArraySizes :: Ident -> [(ParamDecl, CExpr, Type)] -> Trav Env () checkArraySizes funId ((_, _, arrTy@(ArrayTypeSize arrSize)):(ParamName sizeParam, sizeArg, sizeTy):args) - | isIntegral sizeTy && any (`List.isInfixOf` sizeParam) ["size", "len"] = + | any (`List.isInfixOf` sizeParam) ["size", "len"] = -- Ignore any name lookup errors here. VLAs have locally defined -- array sizes, but we don't check VLAs. catchTravError (do diff --git a/test/Tokstyle/C/Linter/SizeArgSpec.hs b/test/Tokstyle/C/Linter/SizeArgSpec.hs index 8c33c54..5d647ae 100644 --- a/test/Tokstyle/C/Linter/SizeArgSpec.hs +++ b/test/Tokstyle/C/Linter/SizeArgSpec.hs @@ -140,6 +140,26 @@ spec = do ] ] + it "works on typedef array parameters" $ do + ast <- mustParse + [ "enum { CRYPTO_PUBLIC_KEY_SIZE = 32 };" + , "enum { EXTENDED_PUBLIC_KEY_SIZE = 64 };" + , "typedef char Public_Key[CRYPTO_PUBLIC_KEY_SIZE];" + , "typedef void consume_cb(char *arr, int size);" + , "void caller(consume_cb *consume, Public_Key pk) {" + , " consume(pk, EXTENDED_PUBLIC_KEY_SIZE);" + , "}" + ] + analyse allWarnings ast + `shouldBe` + [ Text.unlines + [ "test.c:6: (column 15) [ERROR] >>> Type mismatch" + , " size parameter `size` is passed constant value `EXTENDED_PUBLIC_KEY_SIZE` (= 64)," + , " which is greater than the array size of `char [CRYPTO_PUBLIC_KEY_SIZE]`," + , " potentially causing buffer overrun in `consume`" + ] + ] + it "warns about string literal overrun" $ do ast <- mustParse [ "void consume(char *arr, int size);" diff --git a/tokstyle.cabal b/tokstyle.cabal index 9efbce3..3e51d94 100644 --- a/tokstyle.cabal +++ b/tokstyle.cabal @@ -156,6 +156,7 @@ test-suite testsuite Tokstyle.Linter.CompoundInitSpec Tokstyle.Linter.ConstnessSpec Tokstyle.Linter.EnumDefinesSpec + Tokstyle.Linter.MallocCallSpec Tokstyle.Linter.ParensSpec Tokstyle.Linter.SwitchIfSpec Tokstyle.Linter.TypeCheckSpec