|
| 1 | +/** |
| 2 | + * @id c/cert/errno-not-set-to-zero |
| 3 | + * @name ERR30-C: Errno is not set to zero prior to an errno-setting call |
| 4 | + * @description Set errno to zero prior to each call to an errno-setting function. Failing to do so |
| 5 | + * might end in spurious errno values. |
| 6 | + * @kind problem |
| 7 | + * @precision high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/cert/id/err30-c |
| 10 | + * correctness |
| 11 | + * external/cert/obligation/rule |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import codingstandards.c.cert |
| 16 | +import codingstandards.c.Errno |
| 17 | + |
| 18 | +/** |
| 19 | + * CFG nodes preceding a `ErrnoSettingFunctionCall` |
| 20 | + */ |
| 21 | +ControlFlowNode notZeroedPriorToErrnoSet(InBandErrnoSettingFunctionCall fc) { |
| 22 | + result = fc |
| 23 | + or |
| 24 | + exists(ControlFlowNode mid | |
| 25 | + result = mid.getAPredecessor() and |
| 26 | + mid = notZeroedPriorToErrnoSet(fc) and |
| 27 | + // stop recursion when `errno` is set to zero |
| 28 | + not result instanceof ErrnoZeroed and |
| 29 | + not result = any(ErrnoGuard g).getZeroedSuccessor() |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +from InBandErrnoSettingFunctionCall fc, ControlFlowNode cause |
| 34 | +where |
| 35 | + not isExcluded(cause, Contracts4Package::errnoNotSetToZeroQuery()) and |
| 36 | + cause = notZeroedPriorToErrnoSet(fc) and |
| 37 | + ( |
| 38 | + // `errno` is not reset anywhere in the function |
| 39 | + cause = fc.getEnclosingFunction().getBlock() |
| 40 | + or |
| 41 | + // `errno` is not reset after a call to an errno-setting function |
| 42 | + cause = any(InBandErrnoSettingFunctionCall ec | ec != fc) |
| 43 | + or |
| 44 | + // `errno` is not reset after a call to a function |
| 45 | + cause = any(FunctionCall fc2 | fc2 != fc) |
| 46 | + or |
| 47 | + // `errno` value is known to be != 0 |
| 48 | + cause = any(ErrnoGuard g).getNonZeroedSuccessor() |
| 49 | + ) |
| 50 | +select fc, "The value of `errno` may be different than `0` when this function is called." |
0 commit comments