@@ -179,5 +179,124 @@ public void TarReader_InvalidChecksum_ThrowsException(bool corrupted)
179179 Assert . Contains ( "Checksum" , exception . Message ) ;
180180 }
181181 }
182+
183+ public static IEnumerable < object [ ] > GnuBase256UidGidTestData ( )
184+ {
185+ // Leading 0x80 byte: the remaining bytes are a positive big-endian value.
186+ yield return new object [ ] { new byte [ ] { 0x80 , 0 , 0 , 0 , 0x7F , 0xFF , 0xFF , 0xFF } , int . MaxValue } ;
187+ yield return new object [ ] { new byte [ ] { 0x80 , 0 , 0 , 0 , 0xB6 , 0x5A , 0x65 , 0x38 } , unchecked ( ( int ) 0xB65A6538u ) } ;
188+ yield return new object [ ] { new byte [ ] { 0x80 , 0 , 0 , 0 , 0xFF , 0x5A , 0x65 , 0x38 } , unchecked ( ( int ) 0xFF5A6538u ) } ;
189+ yield return new object [ ] { new byte [ ] { 0x80 , 0 , 0 , 0 , 0xFF , 0xFF , 0xFF , 0xFF } , - 1 } ;
190+ // Leading 0xFF byte: the field is a negative big-endian value.
191+ yield return new object [ ] { new byte [ ] { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFE } , - 2 } ;
192+ }
193+
194+ [ Theory ]
195+ [ MemberData ( nameof ( GnuBase256UidGidTestData ) ) ]
196+ public void GnuBase256UidGid_LargerThanInt32MaxValue_DoesNotThrow ( byte [ ] fieldBytes , int expected )
197+ {
198+ byte [ ] tarData = CreateEntryWithRawUidGid ( fieldBytes , fieldBytes ) ;
199+
200+ using TarReader reader = new TarReader ( new MemoryStream ( tarData ) ) ;
201+ TarEntry entry = reader . GetNextEntry ( ) ;
202+ Assert . NotNull ( entry ) ;
203+ Assert . Equal ( expected , entry . Uid ) ;
204+ Assert . Equal ( expected , entry . Gid ) ;
205+ Assert . Null ( reader . GetNextEntry ( ) ) ;
206+ }
207+
208+ [ Theory ]
209+ [ MemberData ( nameof ( GnuBase256UidGidTestData ) ) ]
210+ public async Task GnuBase256UidGid_LargerThanInt32MaxValue_DoesNotThrow_Async ( byte [ ] fieldBytes , int expected )
211+ {
212+ byte [ ] tarData = CreateEntryWithRawUidGid ( fieldBytes , fieldBytes ) ;
213+
214+ await using TarReader reader = new TarReader ( new MemoryStream ( tarData ) ) ;
215+ TarEntry entry = await reader . GetNextEntryAsync ( ) ;
216+ Assert . NotNull ( entry ) ;
217+ Assert . Equal ( expected , entry . Uid ) ;
218+ Assert . Equal ( expected , entry . Gid ) ;
219+ Assert . Null ( await reader . GetNextEntryAsync ( ) ) ;
220+ }
221+
222+ [ Theory ]
223+ [ InlineData ( new byte [ ] { 0x80 , 0 , 0 , 1 , 0 , 0 , 0 , 0 } ) ] // uint.MaxValue + 1
224+ [ InlineData ( new byte [ ] { 0x80 , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF } ) ]
225+ [ InlineData ( new byte [ ] { 0xFF , 0xFF , 0xFF , 0xFF , 0x7F , 0xFF , 0xFF , 0xFF } ) ] // int.MinValue - 1
226+ public void GnuBase256UidGid_LargerThan32Bits_Throws ( byte [ ] fieldBytes )
227+ {
228+ byte [ ] tarData = CreateEntryWithRawUidGid ( fieldBytes , fieldBytes ) ;
229+
230+ using TarReader reader = new TarReader ( new MemoryStream ( tarData ) ) ;
231+ Assert . Throws < InvalidDataException > ( ( ) => reader . GetNextEntry ( ) ) ;
232+ }
233+
234+ [ Fact ]
235+ public void PaxUidGid_LargerThan32Bits_Throws ( )
236+ {
237+ // Write a valid 10 digit uid, then replace it in the extended attributes with one larger than uint.MaxValue.
238+ MemoryStream stream = new MemoryStream ( ) ;
239+ using ( TarWriter writer = new TarWriter ( stream , leaveOpen : true ) )
240+ {
241+ writer . WriteEntry ( new PaxTarEntry ( TarEntryType . Directory , "dir" , new Dictionary < string , string > ( ) { { "uid" , "3059377464" } } ) ) ;
242+ }
243+
244+ byte [ ] tarData = stream . ToArray ( ) ;
245+ byte [ ] original = System . Text . Encoding . ASCII . GetBytes ( "uid=3059377464" ) ;
246+ int index = tarData . AsSpan ( ) . IndexOf ( original ) ;
247+ Assert . True ( index >= 0 ) ;
248+ System . Text . Encoding . ASCII . GetBytes ( "uid=9999999999" ) . CopyTo ( tarData , index ) ;
249+
250+ using TarReader reader = new TarReader ( new MemoryStream ( tarData ) ) ;
251+ Assert . Throws < InvalidDataException > ( ( ) => reader . GetNextEntry ( ) ) ;
252+ }
253+
254+ [ Theory ]
255+ [ InlineData ( "2147483647" , int . MaxValue ) ]
256+ [ InlineData ( "3059377464" , unchecked ( ( int ) 3059377464u ) ) ]
257+ [ InlineData ( "4294967295" , - 1 ) ]
258+ public void PaxUidGid_LargerThanInt32MaxValue_DoesNotThrow ( string value , int expected )
259+ {
260+ MemoryStream stream = new MemoryStream ( ) ;
261+ using ( TarWriter writer = new TarWriter ( stream , leaveOpen : true ) )
262+ {
263+ writer . WriteEntry ( new PaxTarEntry ( TarEntryType . Directory , "dir" , new Dictionary < string , string > ( ) { { "uid" , value } , { "gid" , value } } ) ) ;
264+ }
265+
266+ stream . Position = 0 ;
267+ using TarReader reader = new TarReader ( stream ) ;
268+ PaxTarEntry entry = Assert . IsType < PaxTarEntry > ( reader . GetNextEntry ( ) ) ;
269+ Assert . Equal ( expected , entry . Uid ) ;
270+ Assert . Equal ( expected , entry . Gid ) ;
271+ Assert . Equal ( value , entry . ExtendedAttributes [ "uid" ] ) ;
272+ Assert . Equal ( value , entry . ExtendedAttributes [ "gid" ] ) ;
273+ }
274+
275+ // Writes a GNU entry, then overwrites its uid and gid fields with the given raw bytes and fixes up the checksum.
276+ private static byte [ ] CreateEntryWithRawUidGid ( byte [ ] uid , byte [ ] gid )
277+ {
278+ const int UidOffset = 108 , GidOffset = 116 , ChecksumOffset = 148 , FieldLength = 8 ;
279+
280+ MemoryStream ms = new MemoryStream ( ) ;
281+ using ( TarWriter writer = new TarWriter ( ms , TarEntryFormat . Gnu , leaveOpen : true ) )
282+ {
283+ writer . WriteEntry ( new GnuTarEntry ( TarEntryType . Directory , "dir" ) ) ;
284+ }
285+
286+ byte [ ] tarData = ms . ToArray ( ) ;
287+ uid . CopyTo ( tarData , UidOffset ) ;
288+ gid . CopyTo ( tarData , GidOffset ) ;
289+
290+ // The checksum is computed with the checksum field itself filled with spaces.
291+ tarData . AsSpan ( ChecksumOffset , FieldLength ) . Fill ( ( byte ) ' ' ) ;
292+ int checksum = 0 ;
293+ for ( int i = 0 ; i < 512 ; i ++ )
294+ {
295+ checksum += tarData [ i ] ;
296+ }
297+ System . Text . Encoding . ASCII . GetBytes ( Convert . ToString ( checksum , 8 ) . PadLeft ( 6 , '0' ) + "\0 " ) . CopyTo ( tarData , ChecksumOffset ) ;
298+
299+ return tarData ;
300+ }
182301 }
183302}
0 commit comments