Skip to content

Commit

Permalink
Handle possible null stream in TokenGenerator static ctor and throw e…
Browse files Browse the repository at this point in the history
…xception when instance gets created.
  • Loading branch information
RedFlames committed Jul 20, 2024
1 parent 0af2345 commit c994b96
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CelesteNet.Shared/TokenGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public class TokenGenerator {

static TokenGenerator() {
// Read the list of polynomials
using Stream stream = typeof(TokenGenerator).Assembly.GetManifestResourceStream("polynomials.bin");
using Stream? stream = typeof(TokenGenerator).Assembly.GetManifestResourceStream("polynomials.bin");
if (stream == null) {
LFSRPolynomials = new uint[0];
return;
}
using BinaryReader reader = new BinaryReader(stream);

LFSRPolynomials = new uint[stream.Length / 4];
Expand All @@ -44,6 +48,9 @@ uint RandomUInt() {
return ((uint) rng.Next(1 << 30)) << 2 | ((uint) rng.Next(1 << 2));
}

if (LFSRPolynomials.Length == 0)
throw new FileLoadException("Could not load LFSR polynomials", "polynomials.bin");

lfsrStepRNG = rng;

// Initialize the LFSR to a random non-zero state
Expand Down

0 comments on commit c994b96

Please sign in to comment.