Skip to content

Commit 43123f0

Browse files
Improve Confuser.Core quality (#63)
* Fix TeeList.Remove return value * Fix argument exceptions * Fix typos * Fix deterministic disposal * Fix RandomService seed
1 parent 5d9dcaf commit 43123f0

6 files changed

Lines changed: 40 additions & 33 deletions

File tree

Confuser.Core/ConfuserAssemblyResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void Clear() {
8181

8282
/// <inheritdoc />
8383
public bool Remove(string item) =>
84-
_lists.Aggregate(true, (current, list) => current | list.Remove(item));
84+
_lists.Aggregate(false, (current, list) => current | list.Remove(item));
8585

8686
/// <inheritdoc />
8787
public int Count => _lists[0].Count;

Confuser.Core/ConfuserEngine.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ static ConfuserEngine() {
6262
/// <paramref name="parameters" />.Project is <c>null</c>.
6363
/// </exception>
6464
public static Task Run(ConfuserParameters parameters, CancellationToken? token = null) {
65+
if (parameters == null)
66+
throw new ArgumentNullException(nameof(parameters));
6567
if (parameters.Project == null)
66-
throw new ArgumentNullException("parameters");
68+
throw new ArgumentNullException(nameof(parameters));
6769
if (token == null)
6870
token = new CancellationTokenSource().Token;
6971
return Task.Factory.StartNew(() => RunInternal(parameters, token.Value), token.Value);
@@ -136,7 +138,7 @@ static void RunInternal(ConfuserParameters parameters, CancellationToken token)
136138

137139
// 4. Load modules
138140
context.Logger.Info("Loading input modules...");
139-
marker.Initalize(prots, packers);
141+
marker.Initialize(prots, packers);
140142
MarkerResult markings = marker.MarkProject(context.Project, context);
141143
context.Modules = new ModuleSorter(markings.Modules).Sort().ToList().AsReadOnly();
142144
foreach (var module in context.Modules)
@@ -156,7 +158,7 @@ static void RunInternal(ConfuserParameters parameters, CancellationToken token)
156158
comp.Initialize(context);
157159
}
158160
catch (Exception ex) {
159-
context.Logger.ErrorException("Error occured during initialization of '" + comp.Name + "'.", ex);
161+
context.Logger.ErrorException("Error occurred during initialization of '" + comp.Name + "'.", ex);
160162
throw new ConfuserException(ex);
161163
}
162164
context.CheckCancellation();
@@ -478,31 +480,36 @@ static IEnumerable<string> GetFrameworkVersions() {
478480
using (RegistryKey ndpKey =
479481
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
480482
OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\")) {
483+
if (ndpKey == null) yield break;
481484
foreach (string versionKeyName in ndpKey.GetSubKeyNames()) {
482485
if (!versionKeyName.StartsWith("v"))
483486
continue;
484487

485-
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
486-
var name = (string)versionKey.GetValue("Version", "");
487-
string sp = versionKey.GetValue("SP", "").ToString();
488-
string install = versionKey.GetValue("Install", "").ToString();
489-
if (install == "" || sp != "" && install == "1")
490-
yield return versionKeyName + " " + name;
491-
492-
if (name != "")
493-
continue;
488+
using (RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName)) {
489+
if (versionKey == null) continue;
490+
var name = (string)versionKey.GetValue("Version", "");
491+
string sp = versionKey.GetValue("SP", "").ToString();
492+
string install = versionKey.GetValue("Install", "").ToString();
493+
if (install == "" || sp != "" && install == "1")
494+
yield return versionKeyName + " " + name;
494495

495-
foreach (string subKeyName in versionKey.GetSubKeyNames()) {
496-
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
497-
name = (string)subKey.GetValue("Version", "");
498496
if (name != "")
499-
sp = subKey.GetValue("SP", "").ToString();
500-
install = subKey.GetValue("Install", "").ToString();
501-
502-
if (install == "")
503-
yield return versionKeyName + " " + name;
504-
else if (install == "1")
505-
yield return " " + subKeyName + " " + name;
497+
continue;
498+
499+
foreach (string subKeyName in versionKey.GetSubKeyNames()) {
500+
using (RegistryKey subKey = versionKey.OpenSubKey(subKeyName)) {
501+
if (subKey == null) continue;
502+
name = (string)subKey.GetValue("Version", "");
503+
if (name != "")
504+
sp = subKey.GetValue("SP", "").ToString();
505+
install = subKey.GetValue("Install", "").ToString();
506+
507+
if (install == "")
508+
yield return versionKeyName + " " + name;
509+
else if (install == "1")
510+
yield return " " + subKeyName + " " + name;
511+
}
512+
}
506513
}
507514
}
508515
}

Confuser.Core/Marker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public class Marker {
5656
protected Dictionary<string, Protection> protections;
5757

5858
/// <summary>
59-
/// Initalizes the Marker with specified protections and packers.
59+
/// Initializes the Marker with specified protections and packers.
6060
/// </summary>
6161
/// <param name="protections">The protections.</param>
6262
/// <param name="packers">The packers.</param>
63-
public virtual void Initalize(IList<Protection> protections, IList<Packer> packers) {
63+
public virtual void Initialize(IList<Protection> protections, IList<Packer> packers) {
6464
this.protections = protections.ToDictionary(prot => prot.Id, prot => prot, StringComparer.OrdinalIgnoreCase);
6565
this.packers = packers.ToDictionary(packer => packer.Id, packer => packer, StringComparer.OrdinalIgnoreCase);
6666
}

Confuser.Core/ProtectionPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public enum PipelineStage {
2828
ProcessModule,
2929

3030
/// <summary>
31-
/// Confuser engine optimizes opcodes of the method bodys.
31+
/// Confuser engine optimizes opcodes of the method bodies.
3232
/// This stage occurs once per module.
3333
/// </summary>
3434
OptimizeMethods,

Confuser.Core/Services/RandomService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public RandomService(string seed) {
235235
public RandomGenerator GetRandomGenerator(string id) {
236236
if (string.IsNullOrEmpty(id))
237237
throw new ArgumentNullException("id");
238-
byte[] newSeed = seed;
238+
byte[] newSeed = (byte[])seed.Clone();
239239
byte[] idHash = Utils.SHA256(Encoding.UTF8.GetBytes(id));
240240
for (int i = 0; i < 32; i++)
241241
newSeed[i] ^= idHash[i];

Confuser.Core/Utils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ public static void AddListEntry<TKey, TValue>(this IDictionary<TKey, List<TValue
7676
/// <returns>The path of <paramref name="filespec" /> relative to <paramref name="folder" />.</returns>
7777
public static string GetRelativePath(string fileSpec, string baseDirectory) {
7878
if (fileSpec is null) throw new ArgumentNullException(nameof(fileSpec));
79-
if (baseDirectory is null) throw new ArgumentNullException(nameof(fileSpec));
79+
if (baseDirectory is null) throw new ArgumentNullException(nameof(baseDirectory));
8080

8181
return GetRelativePath(new FileInfo(fileSpec), new DirectoryInfo(baseDirectory));
8282
}
8383

8484
public static string GetRelativePath(FileInfo fileSpec, DirectoryInfo baseDirectory) {
8585
if (fileSpec is null) throw new ArgumentNullException(nameof(fileSpec));
86-
if (baseDirectory is null) throw new ArgumentNullException(nameof(fileSpec));
86+
if (baseDirectory is null) throw new ArgumentNullException(nameof(baseDirectory));
8787

8888
if (baseDirectory.FullName.EndsWith(Path.DirectorySeparatorChar.ToString())) {
8989
baseDirectory = new DirectoryInfo(baseDirectory.FullName.TrimEnd(Path.DirectorySeparatorChar));
@@ -117,8 +117,8 @@ public static string NullIfEmpty(this string val) {
117117
/// <param name="buffer">The input buffer.</param>
118118
/// <returns>The SHA1 hash of the input buffer.</returns>
119119
public static byte[] SHA1(byte[] buffer) {
120-
var sha = new SHA1Managed();
121-
return sha.ComputeHash(buffer);
120+
using (var sha = new SHA1Managed())
121+
return sha.ComputeHash(buffer);
122122
}
123123

124124
/// <summary>
@@ -143,8 +143,8 @@ public static byte[] Xor(byte[] buffer1, byte[] buffer2) {
143143
/// <param name="buffer">The input buffer.</param>
144144
/// <returns>The SHA256 hash of the input buffer.</returns>
145145
public static byte[] SHA256(byte[] buffer) {
146-
var sha = new SHA256Managed();
147-
return sha.ComputeHash(buffer);
146+
using (var sha = new SHA256Managed())
147+
return sha.ComputeHash(buffer);
148148
}
149149

150150
/// <summary>

0 commit comments

Comments
 (0)