Skip to content

Commit 57cee93

Browse files
committed
While fixing the ValuesPreserved test:
• Fix TMAGIC = "ustar " into TMAGIC = "ustar". It was meant to denote that this value should include a trailing NULL char, but actually the constant were authored with a space not NULL, so the NULL were never written, and the meaning space were written as is. Functions which checked for this value failed because they expected a 5-char word and got 6-char. • GetAsciiBytes might be requested to write more chars than there are in the string, make sure these are written as NULLs (would be correctly ignored when reading), unlike whatever trash might be there in the buffer). • Update HeaderEquality test, it were setting magic to the well-known "ustar" value and expecting to see a diff. But the entry is set to "ustar" by default. This test used to pass because the default "ustar" had a trailing space which made it another string. Set to smth which is really different now.
1 parent 2ef1706 commit 57cee93

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public void HeaderEquality()
283283
h2.LinkName = h1.LinkName;
284284
Assert.IsTrue(h1.Equals(h2));
285285

286-
h1.Magic = "ustar";
286+
h1.Magic = "other";
287287
Assert.IsFalse(h1.Equals(h2));
288288
h2.Magic = h1.Magic;
289289
Assert.IsTrue(h1.Equals(h2));

ICSharpCode.SharpZipLib/Tar/TarHeader.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ public class TarHeader : ICloneable
225225
public const byte LF_GNU_VOLHDR = (byte)'V';
226226

227227
/// <summary>
228-
/// The magic tag representing a POSIX tar archive. (includes trailing NULL)
228+
/// The magic tag representing a POSIX tar archive. (would be written with a trailing NULL)
229229
/// </summary>
230-
public const string TMAGIC = "ustar ";
230+
public const string TMAGIC = "ustar";
231231

232232
/// <summary>
233233
/// The magic tag representing an old GNU tar archive where version is included in magic and overwrites it
@@ -899,9 +899,13 @@ public static int GetAsciiBytes(string toAdd, int nameOffset, byte[] buffer, int
899899
throw new ArgumentNullException(nameof(buffer));
900900
}
901901

902-
for (int i = 0; i < length && nameOffset + i < toAdd.Length; ++i) {
902+
int i;
903+
for (i = 0; i < length && nameOffset + i < toAdd.Length; ++i) {
903904
buffer[bufferOffset + i] = (byte)toAdd[nameOffset + i];
904905
}
906+
// If length is beyond the toAdd string length (which is OK by the prev loop condition), eg if a field has fixed length and the string is shorter, make sure all of the extra chars are written as NULLs, so that the reader func would ignore them and get back the original string
907+
for (; i < length; ++i)
908+
buffer[bufferOffset + i] = 0;
905909
return bufferOffset + length;
906910
}
907911

0 commit comments

Comments
 (0)