Skip to content

Avoid IRawFile.ToArray() to reduce memory allocations - #391

Merged
secana merged 1 commit into
secana:mainfrom
tibel:optimize-memory-allocations
May 2, 2026
Merged

secana merged 1 commit into
secana:mainfrom
tibel:optimize-memory-allocations

Conversation

@tibel

@tibel tibel commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Replaces IRawFile.ToArray() calls that copy the entire PE file into a byte[] with span-based alternatives, reducing memory pressure when parsing large binaries.

Changes

AuthenticodeInfo

  • Replaced HashAlgorithm.TransformBlock on a full-file byte[] with IncrementalHash.AppendData(ReadOnlySpan<byte>) fed by IRawFile.AsSpan()
  • Added new public overload ComputeAuthenticodeHashFromPeFile(HashAlgorithmName) for modern callers
  • Marked old ComputeAuthenticodeHashFromPeFile(HashAlgorithm) as [Obsolete] (still works, delegates internally)
  • Updated VerifyHash() to use new overload directly

ImageLoadConfigDirectory

  • Replaced ToArray() + Marshal.Copy() with AsSpan() + Span.CopyTo() via unsafe pointer

StreamFile

  • Replaced ToArray() → ToList() → RemoveRange() → ToArray() with in-place byte shifting + SetLength() truncation

@secana
secana merged commit 05eda8b into secana:main May 2, 2026
5 checks passed
@tibel
tibel deleted the optimize-memory-allocations branch May 4, 2026 13:46
secana added a commit to secana/PeNet-Analyzer that referenced this pull request May 7, 2026
Updated [PeNet](https://github.com/secana/PeNet) from 6.0.0 to 6.1.1.

<details>
<summary>Release notes</summary>

_Sourced from [PeNet's
releases](https://github.com/secana/PeNet/releases)._

## 6.1.1

## What's Changed
* Avoid IRawFile.ToArray() to reduce memory allocations by @​tibel in
secana/PeNet#391


**Full Changelog**:
secana/PeNet@v6.1.0...v6.1.1

## 6.1.0

## What's Changed
* Bump Microsoft.SourceLink.GitHub from 8.0.0 to 10.0.102 by
@​dependabot[bot] in secana/PeNet#373
* Bump Microsoft.SourceLink.GitHub from 10.0.102 to 10.0.103 by
@​dependabot[bot] in secana/PeNet#374
* Bump System.Security.Cryptography.Pkcs from 10.0.2 to 10.0.3 by
@​dependabot[bot] in secana/PeNet#375
* Bump Microsoft.NET.Test.Sdk from 18.0.1 to 18.3.0 by @​dependabot[bot]
in secana/PeNet#377
* Bump Microsoft.SourceLink.GitHub from 10.0.103 to 10.0.200 by
@​dependabot[bot] in secana/PeNet#378
* Bump System.Security.Cryptography.Pkcs from 10.0.3 to 10.0.4 by
@​dependabot[bot] in secana/PeNet#379
* Bump System.Security.Cryptography.Pkcs from 10.0.4 to 10.0.5 by
@​dependabot[bot] in secana/PeNet#381
* Bump Microsoft.SourceLink.GitHub from 10.0.200 to 10.0.201 by
@​dependabot[bot] in secana/PeNet#380
* Bump dependabot/fetch-metadata from 2.5.0 to 3.0.0 by
@​dependabot[bot] in secana/PeNet#382
* Bump Microsoft.NET.Test.Sdk from 18.3.0 to 18.4.0 by @​dependabot[bot]
in secana/PeNet#383
* Bump Microsoft.SourceLink.GitHub from 10.0.201 to 10.0.202 by
@​dependabot[bot] in secana/PeNet#384
* Bump System.Security.Cryptography.Pkcs from 10.0.5 to 10.0.6 by
@​dependabot[bot] in secana/PeNet#385
* Bump Microsoft.SourceLink.GitHub from 10.0.202 to 10.0.203 by
@​dependabot[bot] in secana/PeNet#386
* Bump dependabot/fetch-metadata from 3.0.0 to 3.1.0 by
@​dependabot[bot] in secana/PeNet#388
* Bump System.Security.Cryptography.Pkcs from 10.0.6 to 10.0.7 by
@​dependabot[bot] in secana/PeNet#387
* Bump Microsoft.NET.Test.Sdk from 18.4.0 to 18.5.1 by @​dependabot[bot]
in secana/PeNet#389
* Add SigningTimestamp property to AuthenticodeInfo by @​tibel in
secana/PeNet#390

## New Contributors
* @​tibel made their first contribution in
secana/PeNet#390

**Full Changelog**:
secana/PeNet@v6.0.0...v6.1.0

Commits viewable in [compare
view](secana/PeNet@v6.0.0...v6.1.1).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=PeNet&package-manager=nuget&previous-version=6.0.0&new-version=6.1.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants