Skip to content

Commit 96c0ca8

Browse files
authored
Merge pull request #18865 from carldybdahl-microsoft/csharp/path-combine
Add CodeQL recommendation against Path.Combine
2 parents 7f56c67 + 2f7cdf1 commit 96c0ca8

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p><code>Path.Combine</code> may silently drop its earlier arguments if its later arguments are absolute paths. E.g. <code>Path.Combine("C:\\Users\\Me\\Documents", "C:\\Program Files\\") == "C:\\Program Files"</code>.</p>
7+
8+
</overview>
9+
<recommendation>
10+
<p>Use <code>Path.Join</code> instead.</p>
11+
</recommendation>
12+
<references>
13+
14+
<li>Microsoft Learn, .NET API browser, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-9.0">Path.Combine</a>.</li>
15+
<li>Microsoft Learn, .NET API browser, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.path.join?view=net-9.0">Path.Join</a>.</li>
16+
17+
</references>
18+
</qhelp>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Call to System.IO.Path.Combine
3+
* @description Finds calls to System.IO.Path's Combine method
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @precision very-high
7+
* @id cs/path-combine
8+
* @tags reliability
9+
*/
10+
11+
import csharp
12+
import semmle.code.csharp.frameworks.System
13+
14+
from MethodCall call
15+
where call.getTarget().hasFullyQualifiedName("System.IO", "Path", "Combine")
16+
select call, "Call to 'System.IO.Path.Combine'."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: newQuery
3+
---
4+
* Added a new query, `csharp/path-combine`, to recommend against the `Path.Combine` method due to it silently discarding its earlier parameters if later parameters are rooted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.IO;
2+
3+
class PathCombine
4+
{
5+
void bad()
6+
{
7+
Path.Combine(@"C:\Users", @"C:\Program Files");
8+
}
9+
10+
void good()
11+
{
12+
Path.Join(@"C:\Users", @"C:\Program Files");
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| PathCombine.cs:7:9:7:54 | call to method Combine | Call to 'System.IO.Path.Combine'. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bad Practices/PathCombine.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj

0 commit comments

Comments
 (0)