forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoDisposeCallOnLocalIDisposable.cs
More file actions
113 lines (95 loc) · 3.11 KB
/
NoDisposeCallOnLocalIDisposable.cs
File metadata and controls
113 lines (95 loc) · 3.11 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
109
110
111
112
113
using System;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Xml;
using System.Threading;
using System.Threading.Tasks;
class Test
{
IDisposable d;
IDisposable dProp { get; set; }
IDisposable this[int i] { get => null; set { } }
public IDisposable Method()
{
// GOOD: Disposed automatically.
using (var c1 = new Timer(TimerProc))
{
}
// GOOD: Dispose called in finally
Timer c1a = null;
try
{
c1a = new Timer(TimerProc);
}
finally
{
if (c1a != null)
{
c1a.Dispose();
}
}
// GOOD: Close called in finally
FileStream c1b = null;
try
{
c1b = new FileStream("", FileMode.CreateNew, FileAccess.Write);
}
finally
{
if (c1b != null)
{
c1b.Close();
}
}
// BAD: No Dispose call
var c1d = new Timer(TimerProc);
var fs = new FileStream("", FileMode.CreateNew, FileAccess.Write);
new FileStream("", FileMode.CreateNew, FileAccess.Write).Fluent();
// GOOD: Disposed via wrapper
fs = new FileStream("", FileMode.CreateNew, FileAccess.Write);
var z = new GZipStream(fs, CompressionMode.Compress);
z.Close();
// GOOD: Escapes
d = new Timer(TimerProc);
if (d == null)
return new Timer(TimerProc);
fs = new FileStream("", FileMode.CreateNew, FileAccess.Write);
d = new GZipStream(fs, CompressionMode.Compress);
dProp = new Timer(TimerProc);
this[0] = new Timer(TimerProc);
d = new FileStream("", FileMode.CreateNew, FileAccess.Write).Fluent();
// GOOD: Passed to another IDisposable
using (var reader = new StreamReader(new FileStream("", FileMode.Open)))
;
// GOOD: XmlDocument.Load disposes incoming XmlReader (False positive as this is disposed in library code)
var xmlReader = XmlReader.Create(new StringReader("xml"), null);
var xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
// GOOD: Passed to a library (False positive as this is disposed in library code).
DisposalTests.Class1.Dispose(new StreamWriter("output.txt"));
// GOOD: Disposed automatically.
using var c2 = new Timer(TimerProc);
// GOOD: ownership taken via ??
StringReader source = null;
using (XmlReader.Create(source ?? new StringReader("xml"), null))
;
// GOOD: Flagging these generates too much noise and there is a general
// acceptance that Tasks are not disposed.
// https://devblogs.microsoft.com/pfxteam/do-i-need-to-dispose-of-tasks/
Task t = new Task(() => { });
t.Start();
t.Wait();
return null;
}
// GOOD: Escapes
IDisposable Create() => new Timer(TimerProc);
void TimerProc(object obj)
{
}
public void Dispose() { }
}
static class Extensions
{
public static FileStream Fluent(this FileStream fs) => fs;
}