From badaaf7eb6bf708fda15ba8d0a07d2e4497cb7fc Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 11 May 2018 13:32:59 -0400 Subject: [PATCH] Fix terminfo number reading with 32-bit integers (dotnet/corefx#29655) Commit migrated from https://github.com/dotnet/corefx/commit/62bbc0bfef4ff22bc5215349b9db5f5a53f3a580 --- src/libraries/System.Console/src/System/TermInfo.cs | 14 +++++++------- src/libraries/System.Console/tests/TermInfo.cs | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Console/src/System/TermInfo.cs b/src/libraries/System.Console/src/System/TermInfo.cs index ffccbf6..cb30386 100644 --- a/src/libraries/System.Console/src/System/TermInfo.cs +++ b/src/libraries/System.Console/src/System/TermInfo.cs @@ -101,8 +101,8 @@ namespace System private readonly int _nameSectionNumBytes; /// The number of bytes in the Booleans section of the database. private readonly int _boolSectionNumBytes; - /// The number of shorts in the numbers section of the database. - private readonly int _numberSectionNumShorts; + /// The number of integers in the numbers section of the database. + private readonly int _numberSectionNumInts; /// The number of offsets in the strings section of the database. private readonly int _stringSectionNumOffsets; /// The number of bytes in the strings table of the database. @@ -134,12 +134,12 @@ namespace System _nameSectionNumBytes = ReadInt16(data, 2); _boolSectionNumBytes = ReadInt16(data, 4); - _numberSectionNumShorts = ReadInt16(data, 6); + _numberSectionNumInts = ReadInt16(data, 6); _stringSectionNumOffsets = ReadInt16(data, 8); _stringTableNumBytes = ReadInt16(data, 10); if (_nameSectionNumBytes < 0 || _boolSectionNumBytes < 0 || - _numberSectionNumShorts < 0 || + _numberSectionNumInts < 0 || _stringSectionNumOffsets < 0 || _stringTableNumBytes < 0) { @@ -285,7 +285,7 @@ namespace System /// The offset into data where the string offsets section begins. We index into this section /// to find the location within the strings table where a string value exists. /// - private int StringOffsetsOffset { get { return NumbersOffset + (_numberSectionNumShorts * _sizeOfInt); } } + private int StringOffsetsOffset { get { return NumbersOffset + (_numberSectionNumInts * _sizeOfInt); } } /// The offset into data where the string table exists. private int StringsTableOffset { get { return StringOffsetsOffset + (_stringSectionNumOffsets * 2); } } @@ -337,14 +337,14 @@ namespace System int index = (int)numberIndex; Debug.Assert(index >= 0); - if (index >= _numberSectionNumShorts) + if (index >= _numberSectionNumInts) { // Some terminfo files may not contain enough entries to actually // have the requested one. return -1; } - return ReadInt16(_data, NumbersOffset + (index * 2)); + return ReadInt(_data, NumbersOffset + (index * _sizeOfInt), _readAs32Bit); } /// Parses the extended string information from the terminfo data. diff --git a/src/libraries/System.Console/tests/TermInfo.cs b/src/libraries/System.Console/tests/TermInfo.cs index fe5185a..9d444e0 100644 --- a/src/libraries/System.Console/tests/TermInfo.cs +++ b/src/libraries/System.Console/tests/TermInfo.cs @@ -92,6 +92,7 @@ public class TermInfo object info = CreateTermColorInfo(db); Assert.Equal(expectedForeground, EvaluateParameterizedStrings(GetForegroundFormat(info), colorValue)); Assert.Equal(expectedBackground, EvaluateParameterizedStrings(GetBackgroundFormat(info), colorValue)); + Assert.InRange(GetMaxColors(info), 1, int.MaxValue); } } -- 2.7.4