[PR #400] [MERGED] Avoid throwing NotSupportedException in ReaderFactory hot path #1005

Open
opened 2026-01-29 22:18:34 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/400
Author: @MattKotsenas
Created: 7/10/2018
Status: Merged
Merged: 7/10/2018
Merged by: @adamhathcock

Base: masterHead: feature/avoid-exception-in-readerfactory


📝 Commits (1)

  • bebccaa Avoid throwing NotSupportedException in ReaderFactory hot path

📊 Changes

2 files changed (+2 additions, -1 deletions)

View changed files

📝 src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs (+1 -0)
📝 src/SharpCompress/Common/Zip/ZipHeaderFactory.cs (+1 -1)

📄 Description

ReaderFactory.Open() calls ZipArchive.IsZipFile() to determine if the stream is a zip archive, which calls into ZipHeaderFactory.ReadHeader(), which throws a NotSupportedException when the bytes do not match a valid header.

To be clear, this exception is caught and IsZipFile() returns false, but when called in a hot-path, these exceptions can become expensive.

To address this issue, ReadHeader now returns null in the default cause instead of throwing. All callsites were already checking for and handling null, so no behavior should change.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/400 **Author:** [@MattKotsenas](https://github.com/MattKotsenas) **Created:** 7/10/2018 **Status:** ✅ Merged **Merged:** 7/10/2018 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `feature/avoid-exception-in-readerfactory` --- ### 📝 Commits (1) - [`bebccaa`](https://github.com/adamhathcock/sharpcompress/commit/bebccaae28f07bc23a9c9704dd21f3e5d4f78868) Avoid throwing `NotSupportedException` in `ReaderFactory` hot path ### 📊 Changes **2 files changed** (+2 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs` (+1 -0) 📝 `src/SharpCompress/Common/Zip/ZipHeaderFactory.cs` (+1 -1) </details> ### 📄 Description `ReaderFactory.Open()` calls `ZipArchive.IsZipFile()` to determine if the stream is a zip archive, which calls into `ZipHeaderFactory.ReadHeader()`, which throws a `NotSupportedException` when the bytes do not match a valid header. To be clear, this exception is caught and `IsZipFile()` returns `false`, but when called in a hot-path, these exceptions can become expensive. To address this issue, `ReadHeader` now returns `null` in the default cause instead of throwing. All callsites were already checking for and handling `null`, so no behavior should change. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:18:34 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1005