GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — codeql/python/ql/src/Exceptions/IncorrectExceptOrder.ql at main · github/codeql Expand file tree/ Copy pathIncorrectExceptOrder.qlMore file actions 108 lines (93 loc) · 3.21 KB / Copy pathIncorrectExceptOrder.qlFile metadata and controls108 lines (93 loc) · 3.21 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 /** * @name Unreachable `except` block * @description Handling general exceptions before specific exceptions means that the specific * handlers are never executed. * @kind problem * @tags quality * reliability * error-handling * external/cwe/cwe-561 * @problem.severity error * @sub-severity low * @precision very-high * @id py/unreachable-except */ import python import semmle.python.dataflow.new.internal.DataFlowDispatch import semmle.python.ApiGraphs import semmle.python.frameworks.data.internal.ApiGraphModels predicate builtinException(string name) { typeModel("builtins.BaseException~Subclass", "builtins." + name, "") } predicate builtinExceptionSubclass(string base, string sub) { typeModel("builtins." + base + "~Subclass", "builtins." + sub, "") } newtype TExceptType = TClass(Class c) or TBuiltin(string name) { builtinException(name) } class ExceptType extends TExceptType { Class asClass() { this = TClass(result) } string asBuiltinName() { this = TBuiltin(result) } predicate isBuiltin() { this = TBuiltin(_) } string getName() { result = this.asClass().getName() or result = this.asBuiltinName() } string toString() { result = this.getName() } DataFlow::Node getAUse() { result = classTracker(this.asClass()) or API::builtin(this.asBuiltinName()).asSource().flowsTo(result) } ExceptType getADirectSuperclass() { result.asClass() = getADirectSuperclass(this.asClass()) or result.isBuiltin() and result.getAUse().asExpr() = this.asClass().getABase() or builtinExceptionSubclass(result.asBuiltinName(), this.asBuiltinName()) and this != result } /** * Holds if this element is at the specified location. * The location spans column `startColumn` of line `startLine` to * column `endColumn` of line `endLine` in file `filepath`. * For more information, see * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). */ predicate hasLocationInfo( string filePath, int startLine, int startColumn, int endLine, int endColumn ) { this.asClass() .getLocation() .hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn) or this.isBuiltin() and filePath = "" and startLine = 0 and startColumn = 0 and endLine = 0 and endColumn = 0 } } predicate incorrectExceptOrder(ExceptStmt ex1, ExceptType cls1, ExceptStmt ex2, ExceptType cls2) { exists(int i, int j, Try t | ex1 = t.getHandler(i) and ex2 = t.getHandler(j) and i < j and cls1 = exceptClass(ex1) and cls2 = exceptClass(ex2) and cls1 = cls2.getADirectSuperclass*() ) } ExceptType exceptClass(ExceptStmt ex) { ex.getType() = result.getAUse().asExpr() } from ExceptStmt ex1, ExceptType cls1, ExceptStmt ex2, ExceptType cls2, string msg where incorrectExceptOrder(ex1, cls1, ex2, cls2) and if cls1 = cls2 then msg = "This except block handling $@ is unreachable; as $@ also handles $@." else msg = "This except block handling $@ is unreachable; as $@ for the more general $@ always subsumes it." select ex2, msg, cls2, cls2.getName(), ex1, "this except block", cls1, cls1.getName() |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |