Skip to content

Commit fd33b9e

Browse files
authored
Fix nullability annotations on ProcessModule.FileName/ModuleName (#63272)
1 parent 079245b commit fd33b9e

File tree

8 files changed

+35
-38
lines changed

8 files changed

+35
-38
lines changed

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal static unsafe int[] ListAllPids()
7474
/// Gets executable name for process given it's PID
7575
/// </summary>
7676
/// <param name="pid">The PID of the process</param>
77-
public static unsafe string? GetProcPath(int pid)
77+
public static unsafe string GetProcPath(int pid)
7878
{
7979
Span<int> sysctlName = stackalloc int[] { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid };
8080
byte* pBuffer = null;
@@ -83,7 +83,7 @@ internal static unsafe int[] ListAllPids()
8383
try
8484
{
8585
Interop.Sys.Sysctl(sysctlName, ref pBuffer, ref bytesLength);
86-
return System.Text.Encoding.UTF8.GetString(pBuffer, (int)bytesLength - 1);
86+
return System.Text.Encoding.UTF8.GetString(pBuffer, bytesLength - 1);
8787
}
8888
finally
8989
{

src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.ParseMapModules.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ private static ProcessModuleCollection ParseMapsModulesCore(IEnumerable<string>
6161
// Not a continuation, commit any current modules and create a new one.
6262
CommitCurrentModule();
6363

64-
module = new ProcessModule
64+
module = new ProcessModule(parsedLine.Path, Path.GetFileName(parsedLine.Path))
6565
{
66-
FileName = parsedLine.Path,
67-
ModuleName = Path.GetFileName(parsedLine.Path),
6866
ModuleMemorySize = parsedLine.Size,
6967
EntryPointAddress = IntPtr.Zero // unknown
7068
};

src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ public partial class ProcessModule : System.ComponentModel.Component
178178
internal ProcessModule() { }
179179
public System.IntPtr BaseAddress { get { throw null; } }
180180
public System.IntPtr EntryPointAddress { get { throw null; } }
181-
public string? FileName { get { throw null; } }
181+
public string FileName { get { throw null; } }
182182
public System.Diagnostics.FileVersionInfo FileVersionInfo { get { throw null; } }
183183
public int ModuleMemorySize { get { throw null; } }
184-
public string? ModuleName { get { throw null; } }
184+
public string ModuleName { get { throw null; } }
185185
public override string ToString() { throw null; }
186186
}
187187
public partial class ProcessModuleCollection : System.Collections.ReadOnlyCollectionBase

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.BSD.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@ internal static ProcessModuleCollection GetModules(int processId)
4242
// and why MainModule exists.
4343
try
4444
{
45-
string? exePath = GetProcPath(processId);
45+
string exePath = GetProcPath(processId);
4646
if (!string.IsNullOrEmpty(exePath))
4747
{
4848
return new ProcessModuleCollection(1)
4949
{
50-
new ProcessModule()
51-
{
52-
FileName = exePath,
53-
ModuleName = Path.GetFileName(exePath)
54-
}
50+
new ProcessModule(exePath, Path.GetFileName(exePath))
5551
};
5652
}
5753
}

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.FreeBSD.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static int[] GetProcessIds()
1515
return Interop.Process.ListAllPids();
1616
}
1717

18-
internal static string? GetProcPath(int processId)
18+
internal static string GetProcPath(int processId)
1919
{
2020
return Interop.Process.GetProcPath(processId);
2121
}

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,20 @@ internal static ProcessModuleCollection GetModules(int processId)
4141
ProcessModuleCollection modules = Interop.procfs.ParseMapsModules(processId) ?? new(capacity: 0);
4242

4343
// Move the main executable module to be the first in the list if it's not already
44-
string? exePath = Process.GetExePath(processId);
45-
for (int i = 0; i < modules.Count; i++)
44+
if (Process.GetExePath(processId) is string exePath)
4645
{
47-
ProcessModule module = modules[i];
48-
if (module.FileName == exePath)
46+
for (int i = 0; i < modules.Count; i++)
4947
{
50-
if (i > 0)
48+
ProcessModule module = modules[i];
49+
if (module.FileName == exePath)
5150
{
52-
modules.RemoveAt(i);
53-
modules.Insert(0, module);
51+
if (i > 0)
52+
{
53+
modules.RemoveAt(i);
54+
modules.Insert(0, module);
55+
}
56+
break;
5457
}
55-
break;
5658
}
5759
}
5860

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,6 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
161161
continue;
162162
}
163163

164-
var module = new ProcessModule()
165-
{
166-
ModuleMemorySize = ntModuleInfo.SizeOfImage,
167-
EntryPointAddress = ntModuleInfo.EntryPoint,
168-
BaseAddress = ntModuleInfo.BaseOfDll
169-
};
170-
171164
int length = 0;
172165
while ((length = Interop.Kernel32.GetModuleBaseName(processHandle, moduleHandle, chars, chars.Length)) == chars.Length)
173166
{
@@ -178,12 +171,11 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
178171

179172
if (length == 0)
180173
{
181-
module.Dispose();
182174
HandleLastWin32Error();
183175
continue;
184176
}
185177

186-
module.ModuleName = new string(chars, 0, length);
178+
string moduleName = new string(chars, 0, length);
187179

188180
while ((length = Interop.Kernel32.GetModuleFileNameEx(processHandle, moduleHandle, chars, chars.Length)) == chars.Length)
189181
{
@@ -194,7 +186,6 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
194186

195187
if (length == 0)
196188
{
197-
module.Dispose();
198189
HandleLastWin32Error();
199190
continue;
200191
}
@@ -205,9 +196,13 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
205196
{
206197
charsSpan = charsSpan.Slice(NtPathPrefix.Length);
207198
}
208-
module.FileName = charsSpan.ToString();
209199

210-
modules.Add(module);
200+
modules.Add(new ProcessModule(charsSpan.ToString(), moduleName)
201+
{
202+
ModuleMemorySize = ntModuleInfo.SizeOfImage,
203+
EntryPointAddress = ntModuleInfo.EntryPoint,
204+
BaseAddress = ntModuleInfo.BaseOfDll,
205+
});
211206
}
212207
}
213208
finally

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessModule.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ namespace System.Diagnostics
1313
[Designer("System.Diagnostics.Design.ProcessModuleDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
1414
public class ProcessModule : Component
1515
{
16+
private readonly string _fileName;
17+
private readonly string _moduleName;
1618
private FileVersionInfo? _fileVersionInfo;
1719

18-
internal ProcessModule() { }
20+
internal ProcessModule(string fileName, string moduleName)
21+
{
22+
_fileName = fileName;
23+
_moduleName = moduleName;
24+
}
1925

2026
/// <devdoc>
2127
/// Returns the name of the Module.
2228
/// </devdoc>
23-
public string? ModuleName { get; internal set; }
29+
public string ModuleName => _moduleName;
2430

2531
/// <devdoc>
2632
/// Returns the full file path for the location of the module.
2733
/// </devdoc>
28-
public string? FileName { get; internal set; }
34+
public string FileName => _fileName;
2935

3036
/// <devdoc>
3137
/// Returns the memory address that the module was loaded at.
@@ -49,7 +55,7 @@ internal ProcessModule() { }
4955
/// <devdoc>
5056
/// Returns version information about the module.
5157
/// </devdoc>
52-
public FileVersionInfo FileVersionInfo => _fileVersionInfo ?? (_fileVersionInfo = FileVersionInfo.GetVersionInfo(FileName!));
58+
public FileVersionInfo FileVersionInfo => _fileVersionInfo ??= FileVersionInfo.GetVersionInfo(_fileName);
5359

5460
public override string ToString() => $"{base.ToString()} ({ModuleName})";
5561
}

0 commit comments

Comments
 (0)