Skip to content

Commit 9283917

Browse files
authored
Improves rethrow exceptions from throw e; to just throw; to avoid loss of stack data info and removes unused lines of code for cleanliness identified by LGTM/Semmle. Fixes loss of confidence data in some cases from serialization where not specified. Fixes one hardcoded output filename for tagtest and prevents msg for console winding up in log. (#185)
1 parent eecbc67 commit 9283917

File tree

12 files changed

+138
-97
lines changed

12 files changed

+138
-97
lines changed

AppInspector/Commands/AnalyzeCommand.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public AnalyzeCommand(AnalyzeCommandOptions opt)
115115
Utils.Logger = null;
116116
WriteOnce.Log = null;
117117
}
118-
throw e;
118+
throw;
119119
}
120120

121121
_uniqueTagsControl = new HashSet<string>();
@@ -326,7 +326,7 @@ public override string GetResult()
326326
public override int Run()
327327
{
328328
WriteOnce.SafeLog("AnalyzeCommand::Run", LogLevel.Trace);
329-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Analyze"));
329+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "analyze"));
330330

331331
try
332332
{
@@ -369,7 +369,7 @@ public override int Run()
369369
WriteOnce.Error(ErrMsg.GetString(ErrMsg.ID.ANALYZE_NOPATTERNS));
370370
else
371371
{
372-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Analyze"));
372+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "analyze"));
373373
if (!_arg_autoBrowserOpen)
374374
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, "output.html"), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low);
375375
}
@@ -381,7 +381,7 @@ public override int Run()
381381
if (Utils.CLIExecutionContext)
382382
return (int)ExitCode.CriticalError;
383383
else
384-
throw e;
384+
throw;
385385
}
386386
finally
387387
{
@@ -609,7 +609,7 @@ private string ExtractExcerpt(string text, int startLineNumber, int length = 10)
609609
* space, so we'll find the smallest number of spaces at the beginning of
610610
* each line and use that.
611611
*/
612-
var n = (int)Math.Floor(Math.Log10(excerptEndLine) + 1);
612+
613613
var minSpaces = -1;
614614
for (var i = excerptStartLine; i <= excerptEndLine; i++)
615615
{
@@ -650,7 +650,7 @@ public void FlushAll()
650650
if (_arg_consoleVerbosityLevel.ToLower() != "none")
651651
{
652652
if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
653-
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Medium);
653+
WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Medium, false);
654654
else
655655
WriteOnce.NewLine();
656656
}
@@ -710,11 +710,11 @@ void UnZipAndProcess(string filename, ArchiveFileType archiveFileType)
710710
}
711711

712712
}
713-
catch (Exception e)
713+
catch (Exception)
714714
{
715715
string errmsg = ErrMsg.FormatString(ErrMsg.ID.ANALYZE_COMPRESSED_ERROR, filename);
716716
WriteOnce.Error(errmsg);
717-
throw e;
717+
throw;
718718
}
719719

720720
}
@@ -763,10 +763,10 @@ bool FileChecksPassed(string filePath, ref LanguageInfo languageInfo, long fileL
763763
return false;
764764
}
765765
}
766-
catch (Exception e)
766+
catch (Exception)
767767
{
768768
WriteOnce.Error(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_FILE_OR_DIR, filePath));
769-
throw e;
769+
throw;
770770
}
771771

772772
return true;

AppInspector/Commands/ExportTagsCommand.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public ExportTagsCommand(ExportTagsCommandOptions opt)
5353
Utils.Logger = null;
5454
WriteOnce.Log = null;
5555
}
56-
throw e;
56+
throw;
5757
}
5858
}
5959

@@ -160,32 +160,47 @@ public override int Run()
160160

161161
SortedDictionary<string, string> uniqueTags = new SortedDictionary<string, string>();
162162

163-
foreach (Rule r in _rules)
163+
try
164164
{
165-
//builds a list of unique tags
166-
foreach (string t in r.Tags)
165+
166+
foreach (Rule r in _rules)
167167
{
168-
if (uniqueTags.ContainsKey(t))
169-
continue;
170-
else
171-
uniqueTags.Add(t, t);
168+
//builds a list of unique tags
169+
foreach (string t in r.Tags)
170+
{
171+
if (uniqueTags.ContainsKey(t))
172+
continue;
173+
else
174+
uniqueTags.Add(t, t);
175+
}
172176
}
173-
}
174-
175-
//separate loop so results are sorted (Sorted type)
176-
foreach (string s in uniqueTags.Values)
177-
WriteOnce.Result(s, true);
178177

179-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Exporttags"));
180-
if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
181-
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low);
178+
//separate loop so results are sorted (Sorted type)
179+
foreach (string s in uniqueTags.Values)
180+
WriteOnce.Result(s, true);
182181

183-
WriteOnce.FlushAll();
182+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Exporttags"));
183+
if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
184+
WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false);
184185

185-
if (_arg_close_log_on_exit)
186+
WriteOnce.FlushAll();
187+
}
188+
catch (Exception e)
186189
{
187-
Utils.Logger = null;
188-
WriteOnce.Log = null;
190+
WriteOnce.Error(e.Message);
191+
//exit normaly for CLI callers and throw for DLL callers
192+
if (Utils.CLIExecutionContext)
193+
return (int)ExitCode.CriticalError;
194+
else
195+
throw;
196+
}
197+
finally
198+
{
199+
if (_arg_close_log_on_exit)
200+
{
201+
Utils.Logger = null;
202+
WriteOnce.Log = null;
203+
}
189204
}
190205

191206
return (int)ExitCode.Success;

AppInspector/Commands/PackRulesCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public PackRulesCommand(PackRulesCommandOptions opt)
6464
Utils.Logger = null;
6565
WriteOnce.Log = null;
6666
}
67-
throw e;
67+
throw;
6868
}
6969
}
7070

@@ -134,7 +134,7 @@ public override string GetResult()
134134
public override int Run()
135135
{
136136
WriteOnce.SafeLog("PackRules::Run", LogLevel.Trace);
137-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "PackRules"));
137+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "packrules"));
138138

139139
try
140140
{
@@ -154,7 +154,7 @@ public override int Run()
154154
fs.Close();
155155
}
156156

157-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "PackRules"));
157+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "packrules"));
158158
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputfile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Medium);
159159
WriteOnce.FlushAll();
160160
}
@@ -165,7 +165,7 @@ public override int Run()
165165
if (Utils.CLIExecutionContext)
166166
return (int)ExitCode.CriticalError;
167167
else
168-
throw e;
168+
throw;
169169
}
170170
finally
171171
{

AppInspector/Commands/TagDiffCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public TagDiffCommand(TagDiffCommandOptions opt)
7070
Utils.Logger = null;
7171
WriteOnce.Log = null;
7272
}
73-
throw e;
73+
throw;
7474
}
7575
}
7676

@@ -161,7 +161,7 @@ public override string GetResult()
161161
public override int Run()
162162
{
163163
WriteOnce.SafeLog("TagDiffCommand::Run", LogLevel.Trace);
164-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Tagdiff"));
164+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "tagdiff"));
165165

166166
ExitCode exitCode = ExitCode.CriticalError;
167167
//save to quiet analyze cmd and restore
@@ -264,9 +264,9 @@ public override int Run()
264264

265265
WriteOnce.General(ErrMsg.FormatString(ErrMsg.ID.TAGDIFF_RESULTS_DIFFER), false);
266266
WriteOnce.Result(resultsDiffer.ToString());
267-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagdiff"));
267+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "tagdiff"));
268268
if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
269-
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low);
269+
WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false);
270270

271271
WriteOnce.FlushAll();
272272
}
@@ -294,7 +294,7 @@ public override int Run()
294294
if (Utils.CLIExecutionContext)
295295
return (int)ExitCode.CriticalError;
296296
else
297-
throw e;
297+
throw;
298298
}
299299
finally
300300
{

AppInspector/Commands/TagTestCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public TagTestCommand(TagTestCommandOptions opt)
6868
Utils.Logger = null;
6969
WriteOnce.Log = null;
7070
}
71-
throw e;
71+
throw;
7272
}
7373
}
7474

@@ -221,7 +221,6 @@ public override int Run()
221221
analyzeCmdResult = (AnalyzeCommand.ExitCode)cmd1.Run();
222222

223223
//must be done here to avoid losing our handle from analyze command overwriting WriteOnce.Writer
224-
_arg_outputFile ??= "output.json";
225224
ConfigureFileOutput();
226225

227226
//restore
@@ -239,7 +238,7 @@ public override int Run()
239238
else
240239
WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.TAGTEST_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low);
241240

242-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest"));
241+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "tagtest"));
243242

244243
exitCode = _arg_tagTestType == TagTestType.RulesPresent ? ExitCode.SomeTagsFailed : ExitCode.TagsTestSuccess;
245244
}
@@ -287,7 +286,7 @@ public override int Run()
287286

288287
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Tagtest"));
289288
if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
290-
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low);
289+
WriteOnce.Info(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, WriteOnce.ConsoleVerbosity.Low, false);
291290

292291
WriteOnce.FlushAll();
293292
}
@@ -304,7 +303,7 @@ public override int Run()
304303
if (Utils.CLIExecutionContext)
305304
return (int)ExitCode.CriticalError;
306305
else
307-
throw e;
306+
throw;
308307
}
309308
finally
310309
{

AppInspector/Commands/VerifyRulesCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public VerifyRulesCommand(VerifyRulesCommandOptions opt)
5757
Utils.Logger = null;
5858
WriteOnce.Log = null;
5959
}
60-
throw e;
60+
throw;
6161
}
6262
}
6363

@@ -140,7 +140,7 @@ public override string GetResult()
140140
public override int Run()
141141
{
142142
WriteOnce.SafeLog("VerifyRulesCommand::Run", LogLevel.Trace);
143-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "Verify Rules"));
143+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "verifyrules"));
144144

145145
ExitCode exitCode = ExitCode.CriticalError;
146146

@@ -159,7 +159,7 @@ public override int Run()
159159
}
160160

161161
WriteOnce.Any(ErrMsg.GetString(ErrMsg.ID.VERIFY_RULES_RESULTS_SUCCESS), true, ConsoleColor.Green, WriteOnce.ConsoleVerbosity.Low);
162-
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "Verify Rules"));
162+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "verifyrules"));
163163
if (!String.IsNullOrEmpty(_arg_outputFile) && Utils.CLIExecutionContext)
164164
WriteOnce.Any(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_OUTPUT_FILE, _arg_outputFile), true, ConsoleColor.Gray, WriteOnce.ConsoleVerbosity.Low);
165165
WriteOnce.FlushAll();
@@ -169,9 +169,9 @@ public override int Run()
169169
WriteOnce.Error(e.Message);
170170
//exit normaly for CLI callers and throw for DLL callers
171171
if (Utils.CLIExecutionContext)
172-
return (int)ExitCode.CriticalError;//no way to distinguish from critical fail
172+
return (int)ExitCode.CriticalError;
173173
else
174-
throw e;
174+
throw;
175175
}
176176
finally
177177
{

AppInspector/RulesVerifier.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,33 @@ public RulesVerifier(string rulePath)
2424
}
2525

2626

27-
public bool Verify()
27+
public void Verify()
2828
{
29-
bool isCompiled = true;
30-
3129
if (Directory.Exists(_rulesPath))
32-
isCompiled = LoadDirectory(_rulesPath);
30+
LoadDirectory(_rulesPath);
3331
else if (File.Exists(_rulesPath))
34-
isCompiled = LoadFile(_rulesPath);
32+
LoadFile(_rulesPath);
3533
else
3634
{
3735
throw new Exception(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_RULE_PATH, _rulesPath));
3836
}
3937

40-
return isCompiled;
4138
}
4239

4340

4441

45-
private bool LoadDirectory(string path)
42+
private void LoadDirectory(string path)
4643
{
47-
bool result = true;
4844
foreach (string filename in Directory.EnumerateFileSystemEntries(path, "*.json", SearchOption.AllDirectories))
4945
{
50-
if (!LoadFile(filename))
51-
result = false;
46+
LoadFile(filename);
5247
}
53-
54-
return result;
5548
}
5649

57-
private bool LoadFile(string file)
50+
private void LoadFile(string file)
5851
{
5952
RuleSet rules = new RuleSet();
60-
bool noProblem = true;
53+
6154
try
6255
{
6356
rules.AddFile(file, null);
@@ -69,10 +62,7 @@ private bool LoadFile(string file)
6962
throw new Exception(ErrMsg.FormatString(ErrMsg.ID.VERIFY_RULE_FAILED, file));
7063
}
7164

72-
if (noProblem)
73-
_rules.AddRange(rules.AsEnumerable());
74-
75-
return noProblem;
65+
_rules.AddRange(rules.AsEnumerable());
7666
}
7767

7868

AppInspector/Utils.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ static public RuleSet GetDefaultRuleSet(Logger logger = null)
9393
{
9494
RuleSet ruleSet = new RuleSet(logger);
9595
Assembly assembly = Assembly.GetExecutingAssembly();
96-
string[] resourceName = assembly.GetManifestResourceNames();
9796
string filePath = "Microsoft.ApplicationInspector.Commands.defaultRulesPkd.json";
9897
Stream resource = assembly.GetManifestResourceStream(filePath);
9998
using (StreamReader file = new StreamReader(resource))
@@ -296,7 +295,6 @@ public static Logger SetupLogging(AllCommandOptions opts)
296295
LogManager.Configuration = config;
297296
Logger = LogManager.GetLogger("CST.ApplicationInspector");
298297
return Logger;
299-
300298
}
301299

302300
}

0 commit comments

Comments
 (0)