|
| 1 | +/** |
| 2 | + * @name Unclosed Repository or Storage |
| 3 | + * @description Detects Repository or Storage instances that are created but never closed, |
| 4 | + * which can lead to file handle leaks on Windows. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @id go-git/unclosed-resources |
| 8 | + * @tags reliability |
| 9 | + * resource-management |
| 10 | + */ |
| 11 | + |
| 12 | +import go |
| 13 | + |
| 14 | +/** |
| 15 | + * A type that represents either `*Repository` or a Storage type that needs closing. |
| 16 | + */ |
| 17 | +class ResourceType extends Type { |
| 18 | + ResourceType() { |
| 19 | + // Repository type |
| 20 | + this.(PointerType).getBaseType().hasQualifiedName("github.com/go-git/go-git/v6", "Repository") |
| 21 | + or |
| 22 | + // filesystem.Storage |
| 23 | + this.(PointerType).getBaseType().hasQualifiedName("github.com/go-git/go-git/v6/storage/filesystem", "Storage") |
| 24 | + or |
| 25 | + // transactional.Storage |
| 26 | + this.(PointerType).getBaseType().hasQualifiedName("github.com/go-git/go-git/v6/storage/transactional", "Storage") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * A function call that creates a resource (Repository or Storage). |
| 32 | + */ |
| 33 | +class ResourceCreation extends CallExpr { |
| 34 | + ResourceCreation() { |
| 35 | + exists(Function f | f = this.getTarget() | |
| 36 | + // Repository creation functions |
| 37 | + f.hasQualifiedName("github.com/go-git/go-git/v6", ["PlainOpen", "PlainOpenWithOptions", "PlainClone", "PlainCloneContext", "PlainInit", "Init", "Clone", "CloneContext", "Open"]) |
| 38 | + or |
| 39 | + // Storage creation functions |
| 40 | + f.hasQualifiedName("github.com/go-git/go-git/v6/storage/filesystem", ["NewStorage", "NewStorageWithOptions"]) |
| 41 | + or |
| 42 | + f.hasQualifiedName("github.com/go-git/go-git/v6/storage/transactional", "NewStorage") |
| 43 | + or |
| 44 | + // Submodule.Repository() method |
| 45 | + f.hasQualifiedName("github.com/go-git/go-git/v6", "Submodule", "Repository") |
| 46 | + or |
| 47 | + // Worktree.Repository() method |
| 48 | + f.hasQualifiedName("github.com/go-git/go-git/v6", "Worktree", "Repository") |
| 49 | + or |
| 50 | + // Repository.Worktree() method returns a Worktree with repository field |
| 51 | + f.hasQualifiedName("github.com/go-git/go-git/v6", "Repository", "Worktree") |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * A call to Close() method on a resource. |
| 58 | + */ |
| 59 | +class CloseCall extends MethodCall { |
| 60 | + CloseCall() { |
| 61 | + this.getTarget().getName() = "Close" and |
| 62 | + this.getReceiver().getType() instanceof ResourceType |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Checks if a variable has a Close() call (direct or in defer) in the same function. |
| 68 | + */ |
| 69 | +predicate hasCloseCall(SsaVariable v) { |
| 70 | + exists(CloseCall close | |
| 71 | + close.getReceiver() = v.getAUse() |
| 72 | + ) |
| 73 | + or |
| 74 | + // Check for defer Close() patterns |
| 75 | + exists(DeferStmt defer, CloseCall close | |
| 76 | + defer.getCall() = close and |
| 77 | + close.getReceiver() = v.getAUse() |
| 78 | + ) |
| 79 | + or |
| 80 | + // Check for defer func() { _ = x.Close() }() patterns |
| 81 | + exists(DeferStmt defer, FuncLit fn, AssignStmt assign, CloseCall close | |
| 82 | + defer.getCall().(CallExpr).getCalleeExpr() = fn and |
| 83 | + fn.getBody().getAStmt() = assign and |
| 84 | + assign.getRhs(0) = close and |
| 85 | + close.getReceiver() = v.getAUse() |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +from ResourceCreation create, SsaVariable v |
| 90 | +where |
| 91 | + // The resource is assigned to a variable |
| 92 | + v.getDefinition().(SsaExplicitDefinition).getInstruction().getNode() = create and |
| 93 | + // The variable is not closed |
| 94 | + not hasCloseCall(v) and |
| 95 | + // The variable is not assigned to a field (which might be closed elsewhere) |
| 96 | + not exists(Field f | v.getAUse() = f.getAWrite().getRhs()) and |
| 97 | + // The variable is not returned (caller's responsibility) |
| 98 | + not exists(ReturnStmt ret | v.getAUse() = ret.getExpr()) |
| 99 | +select create, "This resource is created but never closed, which may cause file handle leaks." |
0 commit comments