From 48f6a10b59f00f85b4324ce5f420e374486e200b Mon Sep 17 00:00:00 2001 From: Jeroen Bos Date: Sat, 27 Apr 2024 17:14:11 +0200 Subject: [PATCH] wip --- .../Bits/IFloatingPointBitReaderExtensions.cs | 1 + .../Internals/BitReaderWithAlongTagger.cs | 25 +++++++++++++++++-- .../ULongLikeFloatingPointBitReader.cs | 2 ++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/JBSnorro/Collections/Bits/IFloatingPointBitReaderExtensions.cs b/JBSnorro/Collections/Bits/IFloatingPointBitReaderExtensions.cs index 3df32edb..60a6c5bd 100644 --- a/JBSnorro/Collections/Bits/IFloatingPointBitReaderExtensions.cs +++ b/JBSnorro/Collections/Bits/IFloatingPointBitReaderExtensions.cs @@ -22,6 +22,7 @@ public static double ReadDouble(this IBitReader reader, int bitCount, IFloatingP { if (reader == null) throw new ArgumentNullException(nameof(reader)); if (bitCount < IFloatingPointBitReader.MIN_BIT_COUNT) throw new ArgumentOutOfRangeException(nameof(bitCount)); + if ((ulong)bitCount > reader.RemainingLength) throw new ArgumentOutOfRangeException(nameof(bitCount)); if (!Enum.IsDefined(floatingPointEncoding)) throw new ArgumentOutOfRangeException(nameof(floatingPointEncoding)); switch (floatingPointEncoding) diff --git a/JBSnorro/Collections/Bits/Internals/BitReaderWithAlongTagger.cs b/JBSnorro/Collections/Bits/Internals/BitReaderWithAlongTagger.cs index b381a026..387ad858 100644 --- a/JBSnorro/Collections/Bits/Internals/BitReaderWithAlongTagger.cs +++ b/JBSnorro/Collections/Bits/Internals/BitReaderWithAlongTagger.cs @@ -1,4 +1,6 @@ -namespace JBSnorro.Collections.Bits.Internals; +using JBSnorro.Diagnostics; + +namespace JBSnorro.Collections.Bits.Internals; /// /// A bit reader that reads floating point numbers assuming they're particularly encoded. @@ -10,6 +12,19 @@ internal class BitReaderWithAlongTagger : BitReader public BitReaderWithAlongTagger(BitArray data, ulong position, ulong length, BitReader alongTagger) : base(data, position, length) { + Contract.Requires(alongTagger is not null); +#if DEBUG + var set = new HashSet(); + for (var a = alongTagger; a != null; a = (a as BitReaderWithAlongTagger)?.alongTagger) + { + if (set.Contains(a)) + { + throw new ArgumentException("Infinite loop"); + } + set.Add(a); + } +#endif + this.alongTagger = alongTagger; } @@ -21,12 +36,18 @@ protected internal override ulong current } set { + if (this.alongTagger is null) + { + // we're called from the base constructor + base.current = value; + return; + } unchecked { var delta = value - current; if (delta != 0) { - this.current = value; + base.current = value; alongTagger.current += delta; } } diff --git a/JBSnorro/Collections/Bits/Internals/ULongLikeFloatingPointBitReader.cs b/JBSnorro/Collections/Bits/Internals/ULongLikeFloatingPointBitReader.cs index d3e35f12..199b31f6 100644 --- a/JBSnorro/Collections/Bits/Internals/ULongLikeFloatingPointBitReader.cs +++ b/JBSnorro/Collections/Bits/Internals/ULongLikeFloatingPointBitReader.cs @@ -8,6 +8,8 @@ internal static double ReadDouble(IBitReader reader, int bitCount) { if (bitCount < IFloatingPointBitReader.MIN_BIT_COUNT || bitCount > 64) throw new ArgumentOutOfRangeException(nameof(bitCount)); + if ((ulong)bitCount > reader.RemainingLength) + throw new ArgumentOutOfRangeException(nameof(bitCount)); ulong value = reader.ReadUInt64(bitCount); if (value == 0)