Skip to content

DataTypeAttribute.GetDataTypeName() throws IndexOutOfRangeException #18236

Closed
@hughbe

Description

@hughbe

The following test demonstrates the bug, found in dotnet/corefx#11004

[Theory]
[InlineData((DataType)(-1))]
[InlineData(DataType.Upload + 1)]
public static void GetDataTypeName_InvalidDataType_ThrowsIndexOutOfRangeException(DataType dataType)
{
    DataTypeAttribute attribute = new DataTypeAttribute(dataType);
    Assert.Throws<IndexOutOfRangeException>(() => attribute.GetDataTypeName());
}

This is because the method EnsureValidDataType should be changed to:

private void EnsureValidDataType()
{
    if (DataType < DataType.Custom || DataType > DataType.Upload)
    {
        throw new InvalidOperationException("DataType is Invalid");
    }
    if (DataType == DataType.Custom && string.IsNullOrEmpty(CustomDataType))
    {
        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.DataTypeAttribute_EmptyDataTypeString));
    }
}

/cc @stephentoub (not sure who the code owner for this class is, or whether this code is legacy and shouldn't be touched)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions