Skip to content

Commit 2bbb117

Browse files
committed
Make codes nullable compatible
1 parent 59257d8 commit 2bbb117

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
2121
<InvariantGlobalization>true</InvariantGlobalization>
2222
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
23-
<Nullable>disable</Nullable>
23+
<Nullable>enable</Nullable>
2424
<ImplicitUsings>enable</ImplicitUsings>
2525

2626
<SignAssembly>true</SignAssembly>

src/EasySign.Cli/Program.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SAPTeam.EasySign.Cli
1111
{
1212
internal class Program
1313
{
14-
public static Bundle Bundle { get; set; }
14+
public static Bundle? Bundle { get; set; }
1515

1616
static RootCommand GetCommands()
1717
{
@@ -132,6 +132,11 @@ static void InitBundle(string workingDirectory, string bundleName)
132132

133133
static void Add()
134134
{
135+
if (Bundle == null)
136+
{
137+
throw new ApplicationException("Bundle is not initialized");
138+
}
139+
135140
AnsiConsole.Status()
136141
.AutoRefresh(true)
137142
.Spinner(Spinner.Known.Default)
@@ -154,6 +159,11 @@ static void Add()
154159

155160
static void Sign(X509Certificate2Collection certificates)
156161
{
162+
if (Bundle == null)
163+
{
164+
throw new ApplicationException("Bundle is not initialized");
165+
}
166+
157167
AnsiConsole.Status()
158168
.AutoRefresh(true)
159169
.Spinner(Spinner.Known.Default)
@@ -200,6 +210,11 @@ static void Sign(X509Certificate2Collection certificates)
200210

201211
private static bool VerifyCertificate(X509Certificate2 certificate)
202212
{
213+
if (Bundle == null)
214+
{
215+
throw new ApplicationException("Bundle is not initialized");
216+
}
217+
203218
List<bool> verifyResults = new();
204219

205220
var defaultVerification = Bundle.VerifyCertificate(certificate, out X509ChainStatus[] statuses);
@@ -239,6 +254,11 @@ private static void EnumerateStatuses(X509ChainStatus[] statuses)
239254

240255
static void Verify()
241256
{
257+
if (Bundle == null)
258+
{
259+
throw new ApplicationException("Bundle is not initialized");
260+
}
261+
242262
var colorDict = new Dictionary<string, Color>()
243263
{
244264
["file_verified"] = Color.MediumSpringGreen,
@@ -365,9 +385,10 @@ public static IEnumerable<string> SafeEnumerateFiles(string path, string searchP
365385

366386
while (!folders.IsEmpty)
367387
{
368-
folders.TryDequeue(out string currentDir);
369-
string[] subDirs;
370-
string[] files = null;
388+
if (!folders.TryDequeue(out string? currentDir)) continue;
389+
390+
string[] subDirs = Array.Empty<string>();
391+
string[] files = Array.Empty<string>();
371392

372393
try
373394
{
@@ -376,12 +397,9 @@ public static IEnumerable<string> SafeEnumerateFiles(string path, string searchP
376397
catch (UnauthorizedAccessException) { }
377398
catch (DirectoryNotFoundException) { }
378399

379-
if (files != null)
400+
foreach (string file in files)
380401
{
381-
foreach (string file in files)
382-
{
383-
yield return file;
384-
}
402+
yield return file;
385403
}
386404

387405
try

src/EasySign/Bundle.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace SAPTeam.EasySign
1919
public class Bundle
2020
{
2121
private readonly string bundleName = ".eSign";
22-
private byte[] rawZipContents = null;
22+
private byte[]? rawZipContents = null;
2323

2424
/// <summary>
2525
/// Gets the root path of the bundle. This path used for relative path resolution.
@@ -90,7 +90,7 @@ public string BundleName
9090
/// <summary>
9191
/// Occurs when the bundle file is being updated.
9292
/// </summary>
93-
public event Action<ZipArchive> OnUpdating;
93+
public event Action<ZipArchive>? OnUpdating;
9494

9595
/// <summary>
9696
/// Initializes a new instance of the <see cref="Bundle"/> class with the specified root path and bundle name.
@@ -132,7 +132,9 @@ public ZipArchive OpenZipArchive(ZipArchiveMode mode = ZipArchiveMode.Read)
132132

133133
if (IsLoadedFromMemory)
134134
{
135+
#pragma warning disable CS8604 // Possible null reference argument.
135136
var ms = new MemoryStream(rawZipContents);
137+
#pragma warning restore CS8604 // Possible null reference argument.
136138
return new ZipArchive(ms, mode);
137139
}
138140
else
@@ -185,19 +187,19 @@ public void LoadFromBytes(byte[] bundleContent)
185187
/// <param name="zip">The <see cref="ZipArchive"/> to read from.</param>
186188
protected virtual void ReadBundle(ZipArchive zip)
187189
{
188-
ZipArchiveEntry entry;
190+
ZipArchiveEntry? entry;
189191
if ((entry = zip.GetEntry(".manifest.ec")) != null)
190192
#if NET6_0_OR_GREATER
191-
Manifest = JsonSerializer.Deserialize(entry.Open(), typeof(Manifest), SourceGenerationManifestContext.Default) as Manifest;
193+
Manifest = JsonSerializer.Deserialize(entry.Open(), typeof(Manifest), SourceGenerationManifestContext.Default) as Manifest ?? new Manifest();
192194
#else
193-
Manifest = JsonSerializer.Deserialize<Manifest>(entry.Open(), SerializerOptions);
195+
Manifest = JsonSerializer.Deserialize<Manifest>(entry.Open(), SerializerOptions) ?? new Manifest();;
194196
#endif
195197

196198
if ((entry = zip.GetEntry(".signatures.ec")) != null)
197199
#if NET6_0_OR_GREATER
198-
Signatures = JsonSerializer.Deserialize(entry.Open(), typeof(Signatures), SourceGenerationSignaturesContext.Default) as Signatures;
200+
Signatures = JsonSerializer.Deserialize(entry.Open(), typeof(Signatures), SourceGenerationSignaturesContext.Default) as Signatures ?? new Signatures();
199201
#else
200-
Signatures = JsonSerializer.Deserialize<Signatures>(entry.Open(), SerializerOptions);
202+
Signatures = JsonSerializer.Deserialize<Signatures>(entry.Open(), SerializerOptions) ?? new Signatures();
201203
#endif
202204
}
203205

@@ -209,7 +211,7 @@ protected virtual void ReadBundle(ZipArchive zip)
209211
/// <param name="path">The path of the file to add.</param>
210212
/// <param name="destinationPath">The destination path within the bundle. Ignore when <see cref="Manifest.StoreOriginalFiles"/> is <see langword="false"/></param>
211213
/// <param name="rootPath">The root path for relative paths.</param>
212-
public void AddEntry(string path, string destinationPath = "./", string rootPath = null)
214+
public void AddEntry(string path, string destinationPath = "./", string? rootPath = null)
213215
{
214216
EnsureWritable();
215217

@@ -299,6 +301,8 @@ public bool VerifySignature(string certificateHash)
299301
X509Certificate2 certificate = GetCertificate(certificateHash);
300302
var pubKey = certificate.GetRSAPublicKey();
301303

304+
if (pubKey == null) return false;
305+
302306
#if NET6_0_OR_GREATER
303307
var manifestData = Export(Manifest, SourceGenerationManifestContext.Default);
304308
#else
@@ -315,7 +319,7 @@ public bool VerifySignature(string certificateHash)
315319
/// <param name="statuses">The chain statuses of the certificate.</param>
316320
/// <param name="policy">The chain policy to use for verification.</param>
317321
/// <returns>True if the certificate is valid; otherwise, false.</returns>
318-
public bool VerifyCertificate(string certificateHash, out X509ChainStatus[] statuses, X509ChainPolicy policy = null)
322+
public bool VerifyCertificate(string certificateHash, out X509ChainStatus[] statuses, X509ChainPolicy? policy = null)
319323
{
320324
X509Certificate2 certificate = GetCertificate(certificateHash);
321325
return VerifyCertificate(certificate, out statuses, policy);
@@ -327,7 +331,7 @@ public bool VerifyCertificate(string certificateHash, out X509ChainStatus[] stat
327331
/// <param name="certificateHash">The hash of the certificate to verify.</param>
328332
/// <param name="policy">The chain policy to use for verification.</param>
329333
/// <returns>True if the certificate is valid; otherwise, false.</returns>
330-
public bool VerifyCertificate(string certificateHash, X509ChainPolicy policy = null)
334+
public bool VerifyCertificate(string certificateHash, X509ChainPolicy? policy = null)
331335
{
332336
return VerifyCertificate(certificateHash, out _, policy);
333337
}
@@ -339,7 +343,7 @@ public bool VerifyCertificate(string certificateHash, X509ChainPolicy policy = n
339343
/// <param name="statuses">The chain statuses of the certificate.</param>
340344
/// <param name="policy">The chain policy to use for verification.</param>
341345
/// <returns>True if the certificate is valid; otherwise, false.</returns>
342-
public bool VerifyCertificate(X509Certificate2 certificate, out X509ChainStatus[] statuses, X509ChainPolicy policy = null)
346+
public bool VerifyCertificate(X509Certificate2 certificate, out X509ChainStatus[] statuses, X509ChainPolicy? policy = null)
343347
{
344348
X509Chain chain = new X509Chain
345349
{
@@ -357,7 +361,7 @@ public bool VerifyCertificate(X509Certificate2 certificate, out X509ChainStatus[
357361
/// <param name="certificate">The certificate to verify.</param>
358362
/// <param name="policy">The chain policy to use for verification.</param>
359363
/// <returns>True if the certificate is valid; otherwise, false.</returns>
360-
public bool VerifyCertificate(X509Certificate2 certificate, X509ChainPolicy policy = null)
364+
public bool VerifyCertificate(X509Certificate2 certificate, X509ChainPolicy? policy = null)
361365
{
362366
return VerifyCertificate(certificate, out _, policy);
363367
}
@@ -369,7 +373,7 @@ public bool VerifyCertificate(X509Certificate2 certificate, X509ChainPolicy poli
369373
/// <returns>The certificate.</returns>
370374
public X509Certificate2 GetCertificate(string certificateHash)
371375
{
372-
if (!certCache.TryGetValue(certificateHash, out X509Certificate2 certificate))
376+
if (!certCache.TryGetValue(certificateHash, out X509Certificate2? certificate))
373377
{
374378
using var zip = OpenZipArchive();
375379
var certData = ReadEntry(zip, certificateHash);
@@ -389,7 +393,8 @@ public Stream GetFileStream(string entryName)
389393
if (Manifest.StoreOriginalFiles)
390394
{
391395
using var zip = OpenZipArchive();
392-
return zip.GetEntry(entryName).Open();
396+
var entry = zip.GetEntry(entryName) ?? throw new FileNotFoundException("Entry not found", entryName);
397+
return entry.Open();
393398
}
394399
else
395400
{
@@ -486,7 +491,8 @@ protected byte[] ReadEntry(ZipArchive zip, string entryName)
486491
{
487492
if (!fileCache.TryGetValue(entryName, out var data))
488493
{
489-
using var stream = zip.GetEntry(entryName).Open();
494+
var entry = zip.GetEntry(entryName) ?? throw new FileNotFoundException("Entry not found", entryName);
495+
using var stream = entry.Open();
490496
data = ReadStream(stream);
491497

492498
if (IsReadOnly)
@@ -517,14 +523,14 @@ private static byte[] ReadStream(Stream stream)
517523
/// <param name="data">The data to write.</param>
518524
protected void WriteEntry(ZipArchive zip, string entryName, byte[] data)
519525
{
520-
ZipArchiveEntry tempEntry;
526+
ZipArchiveEntry? tempEntry;
521527
if ((tempEntry = zip.GetEntry(entryName)) != null)
522528
tempEntry.Delete();
523529

524530
#if NET6_0_OR_GREATER
525531
ZipArchiveEntry entry = zip.CreateEntry(entryName, CompressionLevel.SmallestSize);
526532
#else
527-
ZipArchiveEntry entry = zip.CreateEntry(entryName, CompressionLevel.Optimal);
533+
ZipArchiveEntry entry = zip.CreateEntry(entryName, CompressionLevel.Optimal);
528534
#endif
529535

530536
using var stream = entry.Open();

0 commit comments

Comments
 (0)