C rules
tx0 checks .c and .h files with 132 rules, 50 of them in the default tier. Files that say a tool generated them are skipped. Each rule's page explains what it finds and what it leaves alone.
Default: 50 rules
Bugs and security problems with measured precision. They run without a tx0.toml.
-
tx0/c:bitwise-precedencedefaultA comparison is an operand of a bitwise operator without parentheses
-
tx0/c:char-compared-with-eofdefaultA
charvariable is compared withEOF, which acharcannot tell apart from a real character -
tx0/c:check-after-dereferencedefaultA pointer is tested for NULL after it was already dereferenced
-
tx0/c:comparison-result-as-onedefaultThe result of
strcmpormemcmpis compared with 1 or -1, though only its sign is defined -
tx0/c:constant-assignment-conditiondefaultA condition assigns a constant where a comparison was meant
-
tx0/c:constant-index-out-of-boundsdefaultAn array is indexed with a constant outside its bounds
-
tx0/c:constant-overflowdefaultArithmetic on
intconstants overflows the range ofint -
tx0/c:constant-size-exceeds-arraydefaultA constant size given with an array is larger than the array
-
tx0/c:copy-onto-itselfdefaultA buffer is copied onto itself with a function that requires them apart
-
tx0/c:discarded-allocationdefaultThe result of a function that allocates is discarded, so it can never be released
-
tx0/c:division-by-zerodefaultAn integer is divided by a constant zero
-
tx0/c:double-freedefaultMemory is freed twice in a row
-
tx0/c:duplicate-casedefaultTwo case labels have the same value
-
tx0/c:duplicate-conditiondefaultA condition of an if-else chain repeats an earlier one
-
tx0/c:empty-if-bodydefaultA semicolon right after the condition of an
ifis its whole body -
tx0/c:empty-loop-bodydefaultA semicolon right after the head of a loop is its whole body, and the block after it runs once
-
tx0/c:expression-without-effectdefaultAn expression statement computes a value and throws it away
-
tx0/c:format-argumentsdefaultA format string of
printfand its arguments do not match -
tx0/c:free-non-heapdefaultfreeis given memory that was not allocated on the heap -
tx0/c:free-pointer-offsetdefaultfreeis given a pointer computed by adding to or subtracting from another pointer -
tx0/c:getsdefaultgetsis called -
tx0/c:getwddefaultgetwdis called -
tx0/c:identical-operandsdefaultBoth operands of an operator are the same
-
tx0/c:impossible-conditiondefaultTwo comparisons of the same value with constants make a condition that is always false or always true
-
tx0/c:insecure-temporary-filedefaultA temporary file name is made with a function that leaves time for another process to take the name
-
tx0/c:loop-past-array-enddefaultA loop over an array runs one step past its end because its condition uses
<= -
tx0/c:memset-swappeddefaultmemsetis called with a length of zero, its last two arguments swapped -
tx0/c:memset-value-truncateddefaultmemsetis given a fill value that does not fit in a byte -
tx0/c:misleading-indentationdefaultA statement is indented as if it belonged to the body of an
ifor loop without braces, but runs after it -
tx0/c:nested-sizeofdefaultsizeofis applied to the result of anothersizeof -
tx0/c:null-dereferencedefaultA pointer is dereferenced where it is known to be NULL
-
tx0/c:open-create-without-modedefaultopenis asked to create a file but is not given the permissions for it -
tx0/c:release-mismatchdefaultA resource is released with the function for another kind of resource
-
tx0/c:remainder-by-onedefaultThe remainder of a division by one is taken, which is always zero
-
tx0/c:return-local-addressdefaultA function returns the address of one of its local variables
-
tx0/c:reversed-compound-assignmentdefaultAn assignment is written
=+or=-where+=or-=was probably meant -
tx0/c:scanf-unbounded-stringdefaultscanfreads a string into a buffer with no limit on its length -
tx0/c:self-assignmentdefaultA value is assigned to itself
-
tx0/c:shift-rangedefaultA shift count is negative or not less than the width of the value
-
tx0/c:sizeof-array-parameterdefaultsizeofis applied to a parameter declared as an array, which is a pointer -
tx0/c:sizeof-pointer-allocationdefaultMemory for what a pointer points to is allocated with the size of the pointer itself
-
tx0/c:sizeof-pointer-lengthdefaultThe length passed with a buffer is the size of the pointer to it
-
tx0/c:sprintf-reads-destinationdefaultsprintfreads from the buffer it writes to -
tx0/c:string-comparisondefaultStrings are compared with
==or!=, which compares their addresses -
tx0/c:string-literal-writedefaultMemory of a string literal is written through a pointer to it
-
tx0/c:strncat-sizedefaultstrncatis given the size of its destination as the number of characters to append -
tx0/c:uninitialized-readdefaultA local variable is read before any value is assigned to it
-
tx0/c:unsequenced-modificationdefaultA variable is changed twice, or changed and read, in one expression without a sequence point between
-
tx0/c:unsigned-zero-comparisondefaultAn unsigned value is tested for being negative
-
tx0/c:use-after-freedefaultMemory is used through a pointer after the pointer was freed
Recommended: 36 rules
Likely bugs, resource and performance problems.
-
tx0/c:alloca-callrecommendedMemory is taken from the stack with
alloca, which cannot report failure -
tx0/c:array-drops-terminatorrecommendedA character array is initialized with a string exactly as long as the array, leaving out the terminating zero
-
tx0/c:assert-side-effectrecommendedThe condition of
assertchanges a variable, which does not happen when assertions are off -
tx0/c:assignment-in-conditionrecommendedA condition is an assignment without parentheses that mark it as meant
-
tx0/c:check-then-openrecommendedA file is checked with
accessand opened by name afterwards, so it may have changed in between -
tx0/c:chroot-without-chdirrecommendedchrootis called without achdirafter it, so the working directory stays outside the jail -
tx0/c:duplicate-includerecommendedA header is included twice in the same file
-
tx0/c:erased-before-freerecommendedMemory is cleared with
memsetright before it is freed, which the compiler may drop -
tx0/c:errno-not-clearedrecommendederrnois tested after a conversion function without being set to zero before the call -
tx0/c:fallthroughrecommendedA case of a switch falls through into the next one without saying so
-
tx0/c:feof-looprecommendedA loop tests
feofbefore reading, so it runs once more after the last read fails -
tx0/c:floating-loop-counterrecommendedA loop counts with a floating variable, so rounding changes how often it runs
-
tx0/c:format-from-variablerecommendedA variable is used as the format of
printfwithout other arguments -
tx0/c:hardcoded-passwordrecommendedA password or secret is written into the source as a string literal
-
tx0/c:identical-branchesrecommendedBoth branches of an if-else or a conditional expression are the same
-
tx0/c:ignored-privilege-changerecommendedThe result of a call that drops privileges is ignored, so a failure leaves them in place
-
tx0/c:integer-division-to-floatingrecommendedAn integer division is stored in a floating variable, after its fraction is already lost
-
tx0/c:leak-on-early-returnrecommendedA function returns without freeing memory it allocated and still holds only in a local variable
-
tx0/c:malloc-strlenrecommendedMemory for a copy of a string is allocated without room for its terminating zero
-
tx0/c:not-precedencerecommendedA negation with
!is compared with a value that is not a truth value -
tx0/c:predictable-random-secretrecommendedA value that looks like a secret is made with
rand, whose output can be predicted -
tx0/c:printf-writes-countrecommendedA format of
printfhas the%nconversion, which writes to memory -
tx0/c:putenv-local-bufferrecommendedputenvis given a local array, which the environment keeps using after the function returns -
tx0/c:realloc-same-pointerrecommendedThe result of
reallocis assigned to the pointer it was given, which is lost if it fails -
tx0/c:shell-command-from-variablerecommendedA shell command is run from a string that is not a literal
-
tx0/c:signed-one-bit-fieldrecommendedA bit-field of one bit has a signed type, so it holds 0 and -1 rather than 0 and 1
-
tx0/c:sscanf-unbounded-stringrecommendedsscanfcopies a word of its input into a buffer with no limit on its length -
tx0/c:strncmp-short-lengthrecommendedstrncmpcompares fewer characters than the string literal it is given has -
tx0/c:strncpy-source-lengthrecommendedThe limit of
strncpyis the length of its source -
tx0/c:tls-verification-disabledrecommendedThe verification of TLS certificates is turned off
-
tx0/c:unchecked-allocation-userecommendedMemory from
mallocis used right away without a test that the allocation succeeded -
tx0/c:unreachable-coderecommendedA statement follows a
return,break,continueorgotoin the same block and never runs -
tx0/c:unused-labelrecommendedA label is never the target of a
goto -
tx0/c:void-mainrecommendedmainis defined to returnvoidinstead ofint -
tx0/c:weak-cryptographyrecommendedA hash or cipher that is broken for security purposes is used
-
tx0/c:world-writable-moderecommendedA file is given permissions that let every user write to it
Strict: 46 rules
Code that can be written more plainly.
-
tx0/c:allocation-size-productstrictThe size given to
mallocorreallocis a product that may overflow unchecked -
tx0/c:boolean-expression-complexitystrictA condition joins more
&&and||operators than the configured maximum -
tx0/c:boolean-literal-comparisonstrictA value is compared with
trueorfalse -
tx0/c:collapsible-ifstrictAn
ifwithoutelseholds only anotherifwithoutelse, which could be one condition -
tx0/c:comma-in-conditionstrictThe condition of an
ifor loop is a comma expression, so only its last part decides -
tx0/c:commented-out-codestrictA comment holds code that was switched off
-
tx0/c:constant-if-conditionstrictThe condition of an
ifis a constant, so one branch never runs -
tx0/c:ctype-plain-charstrictA plain
charis passed to a function ofctype.h, which is undefined for negative values -
tx0/c:cyclomatic-complexitystrictA function has more independent paths than the configured maximum
-
tx0/c:default-not-laststrictThe
defaultcase of aswitchis not its last case -
tx0/c:else-after-jumpstrictAn
elsefollows a branch that always returns or jumps -
tx0/c:empty-blockstrictThe body of an
if,elseor loop is an empty block without a comment -
tx0/c:empty-parameter-liststrictA function definition has empty parentheses instead of
(void) -
tx0/c:file-lengthstrictA file has more lines of code than the configured maximum
-
tx0/c:floating-equalitystrictFloating values are compared for exact equality
-
tx0/c:function-lengthstrictA function body has more lines of code than the configured maximum
-
tx0/c:goto-into-blockstrictA
gotojumps into a block past declarations that initialize variables -
tx0/c:local-address-in-globalstrictThe address of a local variable is stored in a global variable, where it outlives the variable
-
tx0/c:loop-counter-assignedstrictThe counter of a
forloop is also changed in the body of the loop -
tx0/c:lowercase-long-suffixstrictA literal has the suffix
l, which looks like the digit1 -
tx0/c:macro-body-unparenthesizedstrictA macro that stands for an expression with an operator has no parentheses around it
-
tx0/c:macro-parameter-unparenthesizedstrictA parameter of a macro is used next to an operator without parentheses around it
-
tx0/c:missing-include-guardstrictA header has no include guard and no
#pragma once -
tx0/c:mixed-declaratorsstrictOne declaration declares pointers and non-pointers together
-
tx0/c:negative-left-shiftstrictA negative value is shifted left
-
tx0/c:nested-conditionalstrictA conditional expression is nested in another one
-
tx0/c:nesting-depthstrictControl statements are nested deeper than the configured maximum
-
tx0/c:non-reentrant-functionstrictA library function that returns a pointer to shared static storage is called
-
tx0/c:old-style-definitionstrictA function is defined in the old style, with the types of its parameters declared after the parentheses
-
tx0/c:parameter-countstrictA function takes more parameters than the configured maximum
-
tx0/c:redundant-boolean-conditionalstrictA conditional expression chooses between
trueandfalse -
tx0/c:register-storagestrictA local variable is declared
register -
tx0/c:return-countstrictA function has more
returnstatements than the configured maximum -
tx0/c:setjmp-longjmpstrictControl is transferred with
setjmpandlongjmp -
tx0/c:shadowed-variablestrictA local variable has the name of a variable or parameter of an enclosing scope in the same function
-
tx0/c:shift-into-sign-bitstrictA constant
intis shifted left into or past its sign bit -
tx0/c:stray-semicolonstrictA second semicolon after a statement makes an empty statement
-
tx0/c:string-literal-pointerstrictA pointer to non-const
charis made to point to a string literal -
tx0/c:strlen-in-loop-conditionstrictThe condition of a loop calls
strlenon a string the loop does not change, once per step -
tx0/c:switch-without-defaultstrictA
switchhas nodefaultcase -
tx0/c:todo-commentstrictA comment marks work left to do with
TODO,FIXMEorXXX -
tx0/c:unbounded-sprintfstrictsprintfwrites formatted text into a buffer whose size it does not know -
tx0/c:unbounded-string-copystrictA string is copied or appended with a function that does not know the size of the destination
-
tx0/c:unchecked-number-parsestrictA number is parsed with
atoioratof, which cannot report invalid input -
tx0/c:unused-local-variablestrictA local variable is declared but never used
-
tx0/c:unused-static-functionstrictA
staticfunction is defined but never used in its file