Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Fix non-ordinal string comparison. #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Zip Tests/ExtendedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,32 @@ public void TestZip_IsZipFile_Stream()
}
}

[TestMethod]
public void ReadZip_DirectoryBitNotSetForFileWithEmojiName()
{
string zipFileToCreate = Path.Combine(TopLevelDir, "ReadZip_DirectoryBitNotSetForFileWithEmojiName.zip");
string entryName = "Files/\u26BE";

using (ZipFile zip1 = new ZipFile(Encoding.UTF8))
{
zip1.AddEntry(entryName, new byte[0]);

ZipEntry entry = zip1[entryName];
Assert.AreNotEqual(null, entry);
Assert.IsFalse(entry.IsDirectory,
"The IsDirectory property was not set as expected.");

zip1.Save(zipFileToCreate);
}

using (ZipFile zip2 = ZipFile.Read(zipFileToCreate))
{
ZipEntry entry = zip2[entryName];
Assert.AreNotEqual(null, entry);
Assert.IsFalse(entry.IsDirectory,
"The IsDirectory property was not set as expected.");
}
}

[TestMethod]
public void ReadZip_DirectoryBitSetForEmptyDirectories()
Expand Down
6 changes: 3 additions & 3 deletions src/Zip.Shared/Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static DateTime RoundToEvenSecond(DateTime source)
internal static string NormalizePath(string path)
{
// remove leading single dot slash
if (path.StartsWith(".\\")) path = path.Substring(2);
if (path.StartsWith(".\\", StringComparison.Ordinal)) path = path.Substring(2);

// remove intervening dot-slash
path = path.Replace("\\.\\", "\\");
Expand All @@ -109,7 +109,7 @@ internal static string NormalizePath(string path)

private static string SimplifyFwdSlashPath(string path)
{
if (path.StartsWith("./")) path = path.Substring(2);
if (path.StartsWith("./", StringComparison.Ordinal)) path = path.Substring(2);
path = path.Replace("/./", "/");

// Replace foo/anything/../bar with foo/bar
Expand Down Expand Up @@ -138,7 +138,7 @@ public static string NormalizePathForUseInZipFile(string pathName)
pathName = pathName.Replace('\\', '/');

// trim all leading slashes
while (pathName.StartsWith("/")) pathName = pathName.Substring(1);
while (pathName.StartsWith("/", StringComparison.Ordinal)) pathName = pathName.Substring(1);

return SimplifyFwdSlashPath(pathName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Zip.Shared/ZipDirEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ internal static ZipEntry ReadDirEntry(ZipFile zf,
if (zde.AttributesIndicateDirectory)
zde.MarkAsDirectory(); // may append a slash to filename if nec.
// workitem 6898
else if (zde._FileNameInArchive.EndsWith("/")) zde.MarkAsDirectory();
else if (zde._FileNameInArchive.EndsWith("/", StringComparison.Ordinal)) zde.MarkAsDirectory();

zde._CompressedFileDataSize = zde._CompressedSize;
if ((zde._BitField & 0x01) == 0x01)
Expand Down
6 changes: 3 additions & 3 deletions src/Zip.Shared/ZipEntry.Extract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ bool IsDoneWithOutputToBaseDir(string baseDir, out string outFileName)
if (f.IndexOf(':') == 1)
f = f.Substring(2);

if (f.StartsWith("/"))
if (f.StartsWith("/", StringComparison.Ordinal))
f = f.Substring(1);

f = SharedUtilities.SanitizePath(f);
Expand All @@ -1388,7 +1388,7 @@ bool IsDoneWithOutputToBaseDir(string baseDir, out string outFileName)
outFileName = outFileName.Replace('/', Path.DirectorySeparatorChar);

// check if it is a directory
if (IsDirectory || FileName.EndsWith("/"))
if (IsDirectory || FileName.EndsWith("/", StringComparison.Ordinal))
{
if (!Directory.Exists(outFileName))
{
Expand All @@ -1412,7 +1412,7 @@ bool IsDoneWithOutputToBaseDir(string baseDir, out string outFileName)
/// </summary>
bool IsDoneWithOutputToStream()
{
return IsDirectory || FileName.EndsWith("/");
return IsDirectory || FileName.EndsWith("/", StringComparison.Ordinal);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/Zip.Shared/ZipEntry.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ private static bool ReadHeader(ZipEntry ze, System.Text.Encoding defaultEncoding
ze._FileNameInArchive = ze.AlternateEncoding.GetString(block);

// workitem 6898
if (ze._FileNameInArchive.EndsWith("/")) ze.MarkAsDirectory();
if (ze._FileNameInArchive.EndsWith("/", StringComparison.Ordinal)) ze.MarkAsDirectory();

bytesRead += ze.ProcessExtraField(ze.ArchiveStream, extraFieldLength);

ze._LengthOfTrailer = 0;

// workitem 6607 - don't read for directories
// actually get the compressed size and CRC if necessary
if (!ze._FileNameInArchive.EndsWith("/") && (ze._BitField & 0x0008) == 0x0008)
if (!ze._IsDirectory && (ze._BitField & 0x0008) == 0x0008)
{
// This descriptor exists only if bit 3 of the general
// purpose bit flag is set (see below). It is byte aligned
Expand Down
2 changes: 1 addition & 1 deletion src/Zip.Shared/ZipEntry.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,7 @@ private void CopyThroughWithRecompute(Stream outstream)
WriteHeader(outstream, 0);
StoreRelativeOffset();

if (!this.FileName.EndsWith("/"))
if (!this.FileName.EndsWith("/", StringComparison.Ordinal))
{
// Not a directory; there is file data.
// Seek to the beginning of the entry data in the input stream.
Expand Down
2 changes: 1 addition & 1 deletion src/Zip.Shared/ZipEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2504,7 +2504,7 @@ internal void MarkAsDirectory()
{
_IsDirectory = true;
// workitem 6279
if (!_FileNameInArchive.EndsWith("/"))
if (!_FileNameInArchive.EndsWith("/", StringComparison.Ordinal))
_FileNameInArchive += "/";
}

Expand Down
4 changes: 2 additions & 2 deletions src/Zip.Shared/ZipFile.Extract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ private void _InternalExtractAll(string path, bool overrideExtractExistingProper
foreach (ZipEntry e in _entries.Values)
{
// check if it is a directory
if ((e.IsDirectory) || (e.FileName.EndsWith("/")))
if ((e.IsDirectory) || (e.FileName.EndsWith("/", StringComparison.Ordinal)))
{
string outputFile = (e.FileName.StartsWith("/"))
string outputFile = (e.FileName.StartsWith("/", StringComparison.Ordinal))
? Path.Combine(path, e.FileName.Substring(1))
: Path.Combine(path, e.FileName);

Expand Down
6 changes: 3 additions & 3 deletions src/Zip.Shared/ZipFile.Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public void UpdateSelectedFiles(String selectionCriteria,

private string EnsureendInSlash(string s)
{
if (s.EndsWith("\\")) return s;
if (s.EndsWith("\\", StringComparison.Ordinal)) return s;
return s + "\\";
}

Expand All @@ -659,7 +659,7 @@ private void _AddOrUpdateSelectedFiles(String selectionCriteria,
}

// workitem 9176
while (directoryOnDisk.EndsWith("\\")) directoryOnDisk = directoryOnDisk.Substring(0, directoryOnDisk.Length - 1);
while (directoryOnDisk.EndsWith("\\", StringComparison.Ordinal)) directoryOnDisk = directoryOnDisk.Substring(0, directoryOnDisk.Length - 1);
if (Verbose) StatusMessageTextWriter.WriteLine("adding selection '{0}' from dir '{1}'...",
selectionCriteria, directoryOnDisk);
Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria,
Expand Down Expand Up @@ -1444,7 +1444,7 @@ private bool Evaluate(Ionic.Zip.ZipEntry entry)
// workitem 9174
if (slashSwapped != null)
{
while (slashSwapped.EndsWith("\\"))
while (slashSwapped.EndsWith("\\", StringComparison.Ordinal))
slashSwapped = slashSwapped.Substring(0, slashSwapped.Length - 1);
}
foreach (Ionic.Zip.ZipEntry e in zip)
Expand Down
4 changes: 2 additions & 2 deletions src/Zip.Shared/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@ public ZipEntry this[String fileName]
if (e.FileName.Replace("\\", "/") == fileName) return e;

// check for a difference only in trailing slash
if (e.FileName.EndsWith("/"))
if (e.FileName.EndsWith("/", StringComparison.Ordinal))
{
var fileNameNoSlash = e.FileName.Trim("/".ToCharArray());
if (fileNameNoSlash == fileName) return e;
Expand All @@ -3179,7 +3179,7 @@ public ZipEntry this[String fileName]
if (String.Compare(e.FileName.Replace("\\", "/"), fileName, StringComparison.CurrentCultureIgnoreCase) == 0) return e;

// check for a difference only in trailing slash
if (e.FileName.EndsWith("/"))
if (e.FileName.EndsWith("/", StringComparison.Ordinal))
{
var fileNameNoSlash = e.FileName.Trim("/".ToCharArray());

Expand Down
2 changes: 1 addition & 1 deletion src/Zip.Shared/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ public ZipEntry PutNextEntry(String entryName)
_currentEntry.AlternateEncoding = this.AlternateEncoding;
_currentEntry.AlternateEncodingUsage = this.AlternateEncodingUsage;

if (entryName.EndsWith("/")) _currentEntry.MarkAsDirectory();
if (entryName.EndsWith("/", StringComparison.Ordinal)) _currentEntry.MarkAsDirectory();

_currentEntry.EmitTimesInWindowsFormatWhenSaving = ((_timestamp & ZipEntryTimestamp.Windows) != 0);
_currentEntry.EmitTimesInUnixFormatWhenSaving = ((_timestamp & ZipEntryTimestamp.Unix) != 0);
Expand Down
2 changes: 1 addition & 1 deletion src/Zlib.Shared/GZipStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public String FileName
{
_FileName = _FileName.Replace("/", "\\");
}
if (_FileName.EndsWith("\\"))
if (_FileName.EndsWith("\\", StringComparison.Ordinal))
throw new Exception("Illegal filename");
if (_FileName.IndexOf("\\") != -1)
{
Expand Down