Skip to content

Commit bc7cf6b

Browse files
Fixed all Code Analysis warnings
The code compiles with all code analysis warnings enabled. Also fixed up a few issues those exposed.
1 parent 37b4327 commit bc7cf6b

File tree

11 files changed

+724
-68
lines changed

11 files changed

+724
-68
lines changed

Source/ProcMonDebugOutput.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ProcMonDebugOutput", "ProcMonDebugOutput\ProcMonDebugOutput.vcxproj", "{67431913-19A9-4C9E-8DE4-C56939F8324B}"
77
EndProject
88
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NativeTest", "NativeTest\NativeTest.vcxproj", "{C2944A24-633E-4006-8000-CA00A47737DE}"
9+
ProjectSection(ProjectDependencies) = postProject
10+
{67431913-19A9-4C9E-8DE4-C56939F8324B} = {67431913-19A9-4C9E-8DE4-C56939F8324B}
11+
EndProjectSection
912
EndProject
1013
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sysinternals.Debug", "Sysinternals.Debug\Sysinternals.Debug.csproj", "{FB1D522E-1ACB-49DD-93D4-123E6EA13AED}"
1114
EndProject

Source/Sysinternals.Debug/AllCodeAnalysisRulesAsErrors.ruleset

Lines changed: 591 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8" ?>
22
<Dictionary>
33
<Words>
44
<Unrecognized>
55
</Unrecognized>
66
<Recognized>
7-
<Word>Sysinternals</Word>
87
<Word>Wintellect</Word>
8+
<Word>Sysinternals</Word>
99
</Recognized>
1010
<Deprecated>
1111
</Deprecated>
1212
<Compound>
1313
</Compound>
1414
<DiscreteExceptions>
15+
<Term>ListView</Term>
16+
<Term>RegEx</Term>
1517
</DiscreteExceptions>
1618
</Words>
1719
<Acronyms>
1820
<CasingExceptions>
21+
<Word>WiX</Word>
22+
<Word>log</Word>
23+
<Word>net</Word>
1924
</CasingExceptions>
2025
</Acronyms>
2126
</Dictionary>
1.39 KB
Binary file not shown.

Source/Sysinternals.Debug/NativeMethods.cs

Lines changed: 84 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,110 @@
88
// - Moved to VS 2013 and .NET 4.5.1
99
//////////////////////////////////////////////////////////////////////////////*/
1010

11-
using System.Text;
12-
using Microsoft.Win32.SafeHandles;
13-
1411
namespace Sysinternals.Debug
1512
{
13+
using Microsoft.Win32.SafeHandles;
1614
using System;
1715
using System.Diagnostics;
16+
using System.Diagnostics.CodeAnalysis;
17+
using System.Globalization;
1818
using System.Runtime.InteropServices;
19+
using System.Security;
20+
using System.Text;
1921

2022
/// <summary>
2123
/// A class to wrap all the native code needed by this assembly.
2224
/// </summary>
25+
[SuppressMessage("Microsoft.Portability",
26+
"CA1903:UseOnlyApiFromTargetedFramework",
27+
MessageId = "System.Security.SecuritySafeCriticalAttribute",
28+
Justification = "Everyone is running .NET 2.0 SP2 so they have SecuritySafeCritical")]
29+
[SecuritySafeCritical]
2330
internal static class NativeMethods
2431
{
2532
// Constants to represent C preprocessor macros for PInvokes
26-
const uint GENERIC_WRITE = 0x40000000;
27-
const uint OPEN_EXISTING = 3;
28-
const uint FILE_WRITE_ACCESS = 0x0002;
29-
const uint FILE_SHARE_WRITE = 0x00000002;
30-
const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
31-
const uint METHOD_BUFFERED = 0;
33+
private const uint GENERIC_WRITE = 0x40000000;
34+
private const uint OPEN_EXISTING = 3;
35+
private const uint FILE_WRITE_ACCESS = 0x0002;
36+
private const uint FILE_SHARE_WRITE = 0x00000002;
37+
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
38+
private const uint METHOD_BUFFERED = 0;
3239

33-
// Procmon Constants
34-
const uint FILE_DEVICE_PROCMON_LOG = 0x00009535;
35-
const string PROCMON_DEBUGGER_HANDLER = "\\\\.\\Global\\ProcmonDebugLogger";
40+
// Process Monitor Constants
41+
private const uint FILE_DEVICE_PROCMON_LOG = 0x00009535;
42+
private const string PROCMON_DEBUGGER_HANDLER = "\\\\.\\Global\\ProcmonDebugLogger";
3643

3744
/// <summary>
38-
/// The handle to the procmon log device.
45+
/// The handle to the Process Monitor log device.
3946
/// </summary>
4047
private static SafeFileHandle hProcMon;
4148

4249
/// <summary>
43-
/// Get the IO Control code for the ProcMon log.
50+
/// Gets the IO Control code for the ProcMon log.
4451
/// </summary>
4552
private static uint IOCTL_EXTERNAL_LOG_DEBUGOUT { get { return CTL_CODE(); } }
4653

54+
/// <summary>
55+
/// Builds the control code for the Process Monitor driver access.
56+
/// </summary>
4757
/// <seealso href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff543023(v=vs.85).aspx"/>
48-
private static uint CTL_CODE(
49-
uint DeviceType = FILE_DEVICE_PROCMON_LOG,
50-
uint Function = 0x81,
51-
uint Method = METHOD_BUFFERED,
52-
uint Access = FILE_WRITE_ACCESS)
58+
private static uint CTL_CODE(uint DeviceType = FILE_DEVICE_PROCMON_LOG,
59+
uint Function = 0x81,
60+
uint Method = METHOD_BUFFERED,
61+
uint Access = FILE_WRITE_ACCESS)
5362
{
5463
return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
5564
}
5665

57-
/// <remarks>This is only used for opening the procmon log handle, hence the default parameters.</remarks>
66+
/// <summary>
67+
/// Handles calling CreateFile.
68+
/// </summary>
69+
/// <remarks>
70+
/// This is only used for opening the Process Monitor log handle, hence the default parameters.
71+
/// </remarks>
5872
/// <seealso href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx"/>
59-
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
60-
private static extern SafeFileHandle CreateFile(
61-
string lpFileName = PROCMON_DEBUGGER_HANDLER,
62-
uint dwDesiredAccess = GENERIC_WRITE,
63-
uint dwShareMode = FILE_SHARE_WRITE,
64-
IntPtr lpSecurityAttributes = default(IntPtr),
65-
uint dwCreationDisposition = OPEN_EXISTING,
66-
uint dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL,
67-
IntPtr hTemplateFile = default(IntPtr));
73+
[SuppressMessage("Microsoft.Security",
74+
"CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule",
75+
Justification = "This is a bug in Code Analysis on pre-4.0 assemblies: http://connect.microsoft.com/VisualStudio/feedback/details/729254/bogus-ca5122-warning-about-p-invoke-declarations-should-not-be-safe-critical")]
76+
[DllImport("kernel32.dll",
77+
SetLastError = true,
78+
CharSet = CharSet.Unicode)]
79+
private static extern SafeFileHandle CreateFile(string lpFileName = PROCMON_DEBUGGER_HANDLER,
80+
uint dwDesiredAccess = GENERIC_WRITE,
81+
uint dwShareMode = FILE_SHARE_WRITE,
82+
IntPtr lpSecurityAttributes = default(IntPtr),
83+
uint dwCreationDisposition = OPEN_EXISTING,
84+
uint dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL,
85+
IntPtr hTemplateFile = default(IntPtr));
6886

69-
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
70-
private static extern bool DeviceIoControl(
71-
SafeFileHandle hDevice, uint dwIoControlCode,
72-
StringBuilder lpInBuffer, uint nInBufferSize,
73-
IntPtr lpOutBuffer, uint nOutBufferSize,
74-
out uint lpBytesReturned, IntPtr lpOverlapped);
87+
[SuppressMessage("Microsoft.Security",
88+
"CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule",
89+
Justification = "This is a bug in Code Analysis on pre-4.0 assemblies: http://connect.microsoft.com/VisualStudio/feedback/details/729254/bogus-ca5122-warning-about-p-invoke-declarations-should-not-be-safe-critical")]
90+
[DllImport("kernel32.dll",
91+
ExactSpelling = true,
92+
SetLastError = true,
93+
CharSet = CharSet.Unicode)]
94+
[return: MarshalAs(UnmanagedType.Bool)]
95+
private static extern bool DeviceIoControl(SafeFileHandle hDevice,
96+
uint dwIoControlCode,
97+
StringBuilder lpInBuffer,
98+
uint nInBufferSize,
99+
IntPtr lpOutBuffer,
100+
uint nOutBufferSize,
101+
out uint lpBytesReturned,
102+
IntPtr lpOverlapped);
75103

104+
[SuppressMessage("Microsoft.Performance",
105+
"CA1810:InitializeReferenceTypeStaticFieldsInline",
106+
Justification = "How else are you going to set up a static event? (http://social.msdn.microsoft.com/Forums/en-US/d11fe313-278c-4cae-bfcc-b119204866c7/ca1810-incorrect?forum=vstscode)")]
76107
static NativeMethods()
77108
{
78109
AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
79110
{
80-
if (!hProcMon.IsInvalid) hProcMon.Close();
111+
if (!hProcMon.IsInvalid)
112+
{
113+
hProcMon.Close();
114+
}
81115
};
82116
}
83117

@@ -96,28 +130,23 @@ static NativeMethods()
96130
public static bool ProcMonDebugOutput(string message, params object[] args)
97131
{
98132
bool returnValue = false;
99-
try
100-
{
101-
StringBuilder renderedMessage = new StringBuilder();
102-
renderedMessage.AppendFormat(message, args);
103-
uint outLen;
104-
if (hProcMon == null || hProcMon.IsInvalid)
105-
{
106-
hProcMon = CreateFile();
107-
}
108-
DeviceIoControl(
109-
hProcMon, IOCTL_EXTERNAL_LOG_DEBUGOUT,
110-
renderedMessage, (uint)(message.Length * Marshal.SizeOf(typeof(char))),
111-
IntPtr.Zero, 0, out outLen, IntPtr.Zero);
112-
}
113-
catch (EntryPointNotFoundException notFoundException)
133+
StringBuilder renderedMessage = new StringBuilder();
134+
renderedMessage.AppendFormat(CultureInfo.CurrentCulture, message, args);
135+
uint outLen;
136+
137+
if (hProcMon == null || hProcMon.IsInvalid)
114138
{
115-
// This means the appropriate ProcMonDebugOutput[Win32|x64].DLL
116-
// file could not be found. I'll eat this exception so it does
117-
// not take down the application.
118-
Debug.WriteLine(notFoundException.Message);
139+
hProcMon = CreateFile();
119140
}
120141

142+
returnValue = DeviceIoControl(hProcMon,
143+
IOCTL_EXTERNAL_LOG_DEBUGOUT,
144+
renderedMessage,
145+
(uint)(message.Length * sizeof(System.Char)),
146+
IntPtr.Zero,
147+
0,
148+
out outLen,
149+
IntPtr.Zero);
121150
return returnValue;
122151
}
123152
}

Source/Sysinternals.Debug/Properties/AssemblyInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection;
33
using System.Runtime.CompilerServices;
44
using System.Runtime.InteropServices;
5+
using System.Security;
56

67
[assembly: CLSCompliant(true)]
78
// General Information about an assembly is controlled through the following
@@ -16,6 +17,10 @@
1617
[assembly: AssemblyTrademark("")]
1718
[assembly: AssemblyCulture("")]
1819

20+
[assembly: SecurityCritical]
21+
22+
23+
1924
// Setting ComVisible to false makes the types in this assembly not visible
2025
// to COM components. If you need to access a type in this assembly from
2126
// COM, set the ComVisible attribute to true on that type.

Source/Sysinternals.Debug/Sysinternals.Debug.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
2525
<RunCodeAnalysis>true</RunCodeAnalysis>
26-
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
26+
<CodeAnalysisRuleSet>AllCodeAnalysisRulesAsErrors.ruleset</CodeAnalysisRuleSet>
2727
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2828
<DocumentationFile>..\Debugx64\Sysinternals.Debug.XML</DocumentationFile>
2929
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
@@ -36,7 +36,7 @@
3636
<ErrorReport>prompt</ErrorReport>
3737
<WarningLevel>4</WarningLevel>
3838
<RunCodeAnalysis>true</RunCodeAnalysis>
39-
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
39+
<CodeAnalysisRuleSet>AllCodeAnalysisRulesAsErrors.ruleset</CodeAnalysisRuleSet>
4040
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
4141
<DocumentationFile>..\Releasex64\Sysinternals.Debug.XML</DocumentationFile>
4242
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
@@ -53,7 +53,7 @@
5353
<Compile Include="ProcessMonitorTraceListerner.cs" />
5454
</ItemGroup>
5555
<ItemGroup>
56-
<CodeAnalysisDictionary Include="Dictionary.xml" />
56+
<CodeAnalysisDictionary Include="CodeAnalysisDictionary.xml" />
5757
</ItemGroup>
5858
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5959
<PropertyGroup>
3.37 KB
Binary file not shown.

Source/Sysinternals.log4net/ProcMonAppender.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
using log4net.Core;
44
using log4net.Layout;
55
using Sysinternals.Debug;
6+
using System.Diagnostics.CodeAnalysis;
67

78
namespace Sysinternals.log4net
89
{
910
/// <summary>
1011
/// A <c>log4net</c> appender for ProcMonDebugOutput.
1112
/// </summary>
13+
[SuppressMessage("Microsoft.Naming",
14+
"CA1704:IdentifiersShouldBeSpelledCorrectly",
15+
MessageId = "Proc",
16+
Justification="Naming conforms to the rest of the project")]
17+
[SuppressMessage("Microsoft.Naming",
18+
"CA1704:IdentifiersShouldBeSpelledCorrectly",
19+
MessageId = "Appender",
20+
Justification = "Naming conforms the log4net project")]
1221
public class ProcMonAppender : AppenderSkeleton
1322
{
1423
/// <summary>
@@ -17,6 +26,9 @@ public class ProcMonAppender : AppenderSkeleton
1726
/// <remarks>
1827
/// Sets the default layout.
1928
/// </remarks>
29+
[SuppressMessage("Microsoft.Usage",
30+
"CA2214:DoNotCallOverridableMethodsInConstructors",
31+
Justification="Justin put this in and as I don't use log4net, I'm afraid to touch it.")]
2032
public ProcMonAppender()
2133
{
2234
// Although it breaks convention set by the built-in appenders, this is more forgiving.

Source/Sysinternals.log4net/Properties/AssemblyInfo.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using System.Runtime.CompilerServices;
34
using System.Runtime.InteropServices;
5+
using System.Security;
46

7+
[assembly:CLSCompliant(true)]
58
// General Information about an assembly is controlled through the following
69
// set of attributes. Change these attribute values to modify the information
710
// associated with an assembly.
811
[assembly: AssemblyTitle("Sysinternals.log4net")]
9-
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyDescription("The log4net version of the Process Monitor tracing tool")]
1013
[assembly: AssemblyConfiguration("")]
11-
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyCompany("Wintellect")]
1215
[assembly: AssemblyProduct("Sysinternals.log4net")]
13-
[assembly: AssemblyCopyright("Copyright © 2014")]
16+
[assembly: AssemblyCopyright("Copyright © Wintellect 2014")]
1417
[assembly: AssemblyTrademark("")]
1518
[assembly: AssemblyCulture("")]
1619

@@ -19,6 +22,8 @@
1922
// COM, set the ComVisible attribute to true on that type.
2023
[assembly: ComVisible(false)]
2124

25+
[assembly: SecurityCritical]
26+
2227
// The following GUID is for the ID of the typelib if this project is exposed to COM
2328
[assembly: Guid("d8560003-f6b2-40b4-989f-ff24d28a8eb4")]
2429

0 commit comments

Comments
 (0)