Skip to content

Commit b06f6f0

Browse files
authored
Enable infer# static analysis (#293)
* infer * ubuntu * rename * verify dispose
1 parent 9b99dae commit b06f6f0

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

.github/workflows/infer.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Infer
2+
3+
on:
4+
push:
5+
paths-ignore: [ '**.md' ]
6+
branches: [ main ]
7+
pull_request:
8+
paths-ignore: [ '**.md' ]
9+
branches: [ main ]
10+
11+
jobs:
12+
infer:
13+
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Setup .NET Core
19+
uses: actions/setup-dotnet@v2
20+
with:
21+
dotnet-version: 6.0.x
22+
- name: Install dependencies
23+
run: dotnet restore
24+
- name: Build
25+
run: dotnet build --configuration Release --no-restore
26+
27+
- name: Run Infer#
28+
uses: microsoft/infersharpaction@v1.4
29+
id: runinfersharp
30+
with:
31+
binary-path: BitFaster.Caching/bin
32+
33+
- name: Upload SARIF output to GitHub Security Center
34+
uses: github/codeql-action/upload-sarif@v2
35+
with:
36+
sarif_file: infer-out/report.sarif

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,21 @@ public void WhenNoInnerEventsNoOuterEvents()
6767

6868
cache.Events.HasValue.Should().BeFalse();
6969
}
70+
71+
// Infer identified AddOrUpdate and TryUpdate as resource leaks. This test verifies correct disposal.
72+
[Fact]
73+
public void WhenEntryIsUpdatedOldEntryIsDisposed()
74+
{
75+
var disposable1 = new Disposable();
76+
var disposable2 = new Disposable();
77+
78+
this.cache.AddOrUpdate(1, disposable1);
79+
80+
this.cache.TryUpdate(1, disposable2).Should().BeTrue();
81+
disposable1.IsDisposed.Should().BeTrue();
82+
83+
this.cache.TryUpdate(1, new Disposable()).Should().BeTrue();
84+
disposable2.IsDisposed.Should().BeTrue();
85+
}
7086
}
7187
}

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,21 @@ public void WhenNoInnerEventsNoOuterEvents()
6767

6868
cache.Events.HasValue.Should().BeFalse();
6969
}
70+
71+
// Infer identified AddOrUpdate and TryUpdate as resource leaks. This test verifies correct disposal.
72+
[Fact]
73+
public void WhenEntryIsUpdatedOldEntryIsDisposed()
74+
{
75+
var disposable1 = new Disposable();
76+
var disposable2 = new Disposable();
77+
78+
this.cache.AddOrUpdate(1, disposable1);
79+
80+
this.cache.TryUpdate(1, disposable2).Should().BeTrue();
81+
disposable1.IsDisposed.Should().BeTrue();
82+
83+
this.cache.TryUpdate(1, new Disposable()).Should().BeTrue();
84+
disposable2.IsDisposed.Should().BeTrue();
85+
}
7086
}
7187
}

BitFaster.Caching.UnitTests/ScopedTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ namespace BitFaster.Caching.UnitTests
99
{
1010
public class ScopedTests
1111
{
12+
[Fact]
13+
public void WhenScopeIsCreatedThenScopeDisposedValueIsDisposed()
14+
{
15+
var disposable = new Disposable();
16+
var scope = new Scoped<Disposable>(disposable);
17+
18+
scope.Dispose();
19+
disposable.IsDisposed.Should().BeTrue();
20+
}
21+
1222
[Fact]
1323
public void WhenScopeIsCreatedThenScopeDisposedLifetimeDisposesValue()
1424
{

0 commit comments

Comments
 (0)