|
| 1 | +/** Provides a library for errno-setting functions. */ |
| 2 | + |
| 3 | +import cpp |
| 4 | + |
| 5 | +/* |
| 6 | + * An errno-setting function |
| 7 | + */ |
| 8 | + |
| 9 | +abstract class ErrnoSettingFunction extends Function { } |
| 10 | + |
| 11 | +/* |
| 12 | + * An errno-setting function that return out-of-band errors indicators |
| 13 | + */ |
| 14 | + |
| 15 | +class OutOfBandErrnoSettingFunction extends ErrnoSettingFunction { |
| 16 | + OutOfBandErrnoSettingFunction() { |
| 17 | + this.hasGlobalName(["ftell", "fgetpos", "fsetpos", "mbrtowc", "wcrtomb", "wcsrtombs"]) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/* |
| 22 | + * An errno-setting function that return in-band errors indicators |
| 23 | + */ |
| 24 | + |
| 25 | +class InBandErrnoSettingFunction extends ErrnoSettingFunction { |
| 26 | + InBandErrnoSettingFunction() { |
| 27 | + this.hasGlobalName([ |
| 28 | + "fgetwc", "fputwc", "strtol", "wcstol", "strtoll", "wcstoll", "strtoul", "wcstoul", |
| 29 | + "strtoull", "wcstoull", "strtoumax", "wcstoumax", "strtod", "wcstod", "strtof", "wcstof", |
| 30 | + "strtold", "wcstold", "strtoimax", "wcstoimax" |
| 31 | + ]) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | + * A assignment expression setting `errno` to 0 |
| 37 | + */ |
| 38 | + |
| 39 | +class ErrnoZeroed extends AssignExpr { |
| 40 | + ErrnoZeroed() { |
| 41 | + this.getLValue() = any(MacroInvocation ma | ma.getMacroName() = "errno").getExpr() and |
| 42 | + this.getRValue().getValue() = "0" |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/* |
| 47 | + * A guard controlled by a errno comparison |
| 48 | + */ |
| 49 | + |
| 50 | +abstract class ErrnoGuard extends StmtParent { |
| 51 | + abstract ControlFlowNode getZeroedSuccessor(); |
| 52 | + |
| 53 | + abstract ControlFlowNode getNonZeroedSuccessor(); |
| 54 | +} |
| 55 | + |
| 56 | +class ErrnoIfGuard extends EqualityOperation, ErrnoGuard { |
| 57 | + ControlStructure i; |
| 58 | + |
| 59 | + ErrnoIfGuard() { |
| 60 | + this.getAnOperand() = any(MacroInvocation ma | ma.getMacroName() = "errno").getExpr() and |
| 61 | + this.getAnOperand().getValue() = "0" and |
| 62 | + i.getControllingExpr() = this |
| 63 | + } |
| 64 | + |
| 65 | + Stmt getThenSuccessor() { |
| 66 | + i.getControllingExpr() = this and |
| 67 | + (result = i.(IfStmt).getThen() or result = i.(Loop).getStmt()) |
| 68 | + } |
| 69 | + |
| 70 | + Stmt getElseSuccessor() { |
| 71 | + i.getControllingExpr() = this and |
| 72 | + ( |
| 73 | + i.(IfStmt).hasElse() and result = i.(IfStmt).getElse() |
| 74 | + or |
| 75 | + result = i.getFollowingStmt() |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + override ControlFlowNode getZeroedSuccessor() { |
| 80 | + if this instanceof EQExpr then result = this.getThenSuccessor() else result = getElseSuccessor() |
| 81 | + } |
| 82 | + |
| 83 | + override ControlFlowNode getNonZeroedSuccessor() { |
| 84 | + if this instanceof NEExpr then result = this.getThenSuccessor() else result = getElseSuccessor() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class ErrnoSwitchGuard extends SwitchCase, ErrnoGuard { |
| 89 | + ErrnoSwitchGuard() { |
| 90 | + this.getSwitchStmt().getExpr() = any(MacroInvocation ma | ma.getMacroName() = "errno").getExpr() |
| 91 | + } |
| 92 | + |
| 93 | + override ControlFlowNode getZeroedSuccessor() { |
| 94 | + result = this.getAStmt() and this.getExpr().getValue() = "0" |
| 95 | + } |
| 96 | + |
| 97 | + override ControlFlowNode getNonZeroedSuccessor() { |
| 98 | + result = this.getAStmt() and this.getExpr().getValue() != "0" |
| 99 | + } |
| 100 | +} |
0 commit comments