Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in validation for BitVector32 #31410

Closed
oznetmaster opened this issue Nov 6, 2019 · 7 comments
Closed

Error in validation for BitVector32 #31410

oznetmaster opened this issue Nov 6, 2019 · 7 comments
Assignees
Labels
area-System.Collections bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@oznetmaster
Copy link

It appears that there is an error in the code for the validation of a new Section in BitVector32.

The code:

        private static Section CreateSectionHelper(short maxValue, short priorMask, short priorOffset)
        {
            if (maxValue < 1)
            {
                throw new ArgumentException(SR.Format(SR.Argument_InvalidValue_TooSmall, nameof(maxValue), 1), nameof(maxValue));
            }

            short offset = (short)(priorOffset + CountBitsSet(priorMask));
            if (offset >= 32)
            {
                throw new InvalidOperationException(SR.BitVectorFull);
            }
            return new Section(CreateMaskFromHighValue(maxValue), offset);
        }

creates a new section, given the previous section and the maxValue for the new section.

However, the lines:

            short offset = (short)(priorOffset + CountBitsSet(priorMask));
            if (offset >= 32)
            {
                throw new InvalidOperationException(SR.BitVectorFull);
            }

test for the previous section being invalid, not the new one being created.

It would seem like the code should be:

            short offset = (short)(priorOffset + CountBitsSet(priorMask));
            if (offset + CountBitsSet (CreateMaskFromHighValue(maxValue)) >= 32)
            {
                throw new InvalidOperationException(SR.BitVectorFull);
            }

Obviously, there is better ways to actually write the code. I am just demonstrating the issue.

The documentation states that the InvalidOperationException should be thrown if "maxValue is greater than the highest value that can be represented by the number of bits after previous."

@oznetmaster
Copy link
Author

It appears that my suggested code is wrong :( The test should be > 32, not >= 32.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@tannergooding
Copy link
Member

@eiriktsarpalis @layomia, would this fall under System.Collections? I'm not sure if this was mislabeled or not...

The issue does look like a bug:

var section1 = BitVector32.CreateSection(short.MaxValue);           // 0x0FFF, 0x00     0b0000_0000_0000_0000_0000_1111_1111_1111   Bits 00 to 11
var section2 = BitVector32.CreateSection(short.MaxValue, section1); // 0x0FFF, 0x0C     0b0000_0000_1111_1111_1111_0000_0000_0000   Bits 12 to 23
var section3 = BitVector32.CreateSection(short.MaxValue, section2); // 0x0FFF, 0x18     0b1111_1111_0000_0000_0000_0000_0000_0000   Bits 24 to 35!

@eiriktsarpalis
Copy link
Member

Yes, looks like it. Relabeling..

@eiriktsarpalis eiriktsarpalis changed the title Error in calidation for BitVector32 Error in validation for BitVector32 Mar 5, 2020
@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Mar 5, 2020
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Mar 5, 2020
@eiriktsarpalis eiriktsarpalis self-assigned this Sep 30, 2020
@eiriktsarpalis eiriktsarpalis added this to To do in System.Collections Area Ownership via automation Sep 30, 2020
@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 6.0.0 Jan 28, 2021
@eiriktsarpalis eiriktsarpalis added the help wanted [up-for-grabs] Good issue for external contributors label Jan 28, 2021
@huoyaoyuan
Copy link
Member

There is a test expecting highest section to be truncated:

[Theory]
[InlineData(4)]
[InlineData(5)]
[InlineData(short.MaxValue)]
[InlineData(byte.MaxValue)]
// Regardless of mask size, values will be truncated if they hang off the end
public static void Set_Section_OverflowTest(int value)
{
BitVector32 data = new BitVector32();
BitVector32.Section offset = BitVector32.CreateSection(short.MaxValue, BitVector32.CreateSection(short.MaxValue));
BitVector32.Section final = BitVector32.CreateSection(short.MaxValue, offset);
data[final] = value;
Assert.Equal((3 & value) << 30, data.Data);
Assert.NotEqual(value, data.Data);
Assert.Equal(3 & value, data[final]);
Assert.NotEqual(value, data[final]);
}

And the exception documentation implies that:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.bitvector32.createsection?view=net-5.0

previous includes the final bit in the BitVector32.
-or-
maxValue is greater than the highest value that can be represented by the number of bits after previous.

What should we do here? @eiriktsarpalis

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Jun 14, 2021

I suppose we could close the issue as by-design, but the behavior as-is still bothers me.

cc @Clockwork-Muse

@Clockwork-Muse
Copy link
Contributor

... I tried to fix this a long time ago, dotnet/corefx#2920 , but was told not to bother since it was a legacy type, so wrote the test as documentation of as-current behavior (although I left the existing validation alone, for some reason).

@eiriktsarpalis
Copy link
Member

Thanks for the context. In that case I'm going to close this issue as by-design.

@dotnet dotnet locked as resolved and limited conversation to collaborators Jul 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections bug help wanted [up-for-grabs] Good issue for external contributors
Projects
No open projects
Development

No branches or pull requests

6 participants