Skip to content

Commit 05b5226

Browse files
Potential fix for code scanning alert no. 1: Missing Dispose call on local IDisposable (#21)
Potential fix for [https://github.com/AzureCosmosDB/samples/security/code-scanning/1](https://github.com/AzureCosmosDB/samples/security/code-scanning/1) In general, the correct fix is to ensure that any object implementing `IDisposable` is disposed, preferably via a `using` statement/`using` declaration so that disposal happens automatically when the scope ends, even if exceptions occur. For a short, top-level sample like this, the most straightforward change is to wrap the `CosmosClient` creation in a `using` declaration so that it is disposed when the program reaches the end of the top-level scope. Concretely, in `dotnet/001-connect-passwordless/connect.cs`, change the line that creates `client` from a plain `var client = new CosmosClient(endpoint, credential);` to a `using var client = new CosmosClient(endpoint, credential);`. This keeps all existing behavior the same while ensuring `Dispose` is called when the top-level statements complete. No additional methods or imports are required; `using var` is a C# language feature and `CosmosClient` already implements `IDisposable`. _Suggested fixes powered by Copilot Autofix. Review carefully before merging._ Signed-off-by: Sidney Andrews <sidandrews@microsoft.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 934f6b5 commit 05b5226

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

dotnet/001-connect-passwordless/connect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
?? throw new InvalidOperationException("COSMOS_ENDPOINT is not configured. Run: dotnet user-secrets set --file connect.cs \"COSMOS_ENDPOINT\" \"<your-endpoint>\"");
1313

1414
var credential = new DefaultAzureCredential();
15-
var client = new CosmosClient(endpoint, credential);
15+
using var client = new CosmosClient(endpoint, credential);
1616

1717
AccountProperties account = await client.ReadAccountAsync();
1818
Console.WriteLine($"Account: {account.Id}");

0 commit comments

Comments
 (0)