Skip to content

avoid signed overflow in cache memory limit#4561

Merged
lovell merged 1 commit into
lovell:mainfrom
metsw24-max:cache-memory-overflow
Jun 26, 2026
Merged

avoid signed overflow in cache memory limit#4561
lovell merged 1 commit into
lovell:mainfrom
metsw24-max:cache-memory-overflow

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

sharp.cache({ memory }) passes the megabyte value through to the native cache helper, which reads it as a 32-bit int and multiplies by 1048576 to convert to bytes before handing the result to vips_cache_set_max_mem. The multiply runs in 32-bit signed arithmetic and only widens to the size_t the function expects afterwards, so any value of 2048 or above overflows first. A 2048MB request wraps to a negative number that sign-extends to roughly 16 exabytes, which leaves the operation cache effectively unbounded, while exactly 4096MB wraps to zero and silently disables it. I came across this while confirming that larger cache limits were honoured and saw the reported memory.max had no relation to the value passed in. Casting the operand to size_t before the multiply keeps the conversion in 64-bit so the byte count stays correct across the documented range. I have limited the change to that one expression and added a regression test at 4096MB, which reported a maximum of zero beforehand.

@lovell

lovell commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Thanks for the PR. Whilst we're fixing this, I think we might also want to use Uint32Value instead of Int32Value for the cache values as they should all be positive integers. I guess we could also verify these values in the JavaScript layer.

@metsw24-max
metsw24-max force-pushed the cache-memory-overflow branch from 76a3289 to 69a016f Compare June 26, 2026 09:12
@metsw24-max

Copy link
Copy Markdown
Contributor Author

Makes sense. Switched all three cache values to Uint32Value. I kept the size_t widening on memory because Uint32Value on its own still does the MB-to-bytes multiply in 32-bit and wraps from 4096MB up.

Also added a guard in lib/utility.mjs that rejects any cache value that isn't a non-negative integer, with a test for it. All squashed into the one commit.

@lovell

lovell commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Thanks for the updates, it looks like we might have to skip the max memory test on 32-bit systems. The buildPlatformArch() function should provide a suitable value for this.

Read the cache values as unsigned integers and keep the megabyte to byte conversion in size_t, so the memory limit no longer wraps at 2048MB and above. Reject non-integer and negative values in the JavaScript layer.
@metsw24-max
metsw24-max force-pushed the cache-memory-overflow branch from 69a016f to 6b3e1c7 Compare June 26, 2026 15:21
@metsw24-max

Copy link
Copy Markdown
Contributor Author

Good shout. The 4096MB byte count overflows a 32-bit size_t too, so I now skip that test when buildPlatformArch() reports a 32-bit arch (arm, ia32, wasm32). Runs as before on 64-bit and skips cleanly when I force npm_config_arch=ia32.

@lovell
lovell merged commit bd0c2c7 into lovell:main Jun 26, 2026
31 of 32 checks passed
@lovell

lovell commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Thank you. (The wasm32 test failure is unrelated, I'm fixing this now.)

dadezzz pushed a commit to dadezzz/university_notes that referenced this pull request Jul 5, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [sharp](https://sharp.pixelplumbing.com) ([source](https://github.com/lovell/sharp), [changelog](https://github.com/lovell/sharp/blob/main/docs/src/content/docs/changelog.md)) | [`0.35.2` → `0.35.3`](https://renovatebot.com/diffs/npm/sharp/0.35.2/0.35.3) | ![age](https://developer.mend.io/api/mc/badges/age/npm/sharp/0.35.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/sharp/0.35.2/0.35.3?slim=true) |

---

### Release Notes

<details>
<summary>lovell/sharp (sharp)</summary>

### [`v0.35.3`](https://github.com/lovell/sharp/releases/tag/v0.35.3)

[Compare Source](lovell/sharp@v0.35.2...v0.35.3)

- Tighten verification of `text` dimensions, TIFF tile dimensions and `extend` values.

- Improve code bundler support by resolving path to libvips binary.

- Increase default concurrency when use of `MALLOC_ARENA_MAX` is detected.

- Emit warning about binaries provided by Electron for use on Linux.

- Add `hasAlpha` property to output `info`.
  [#&#8203;4500](lovell/sharp#4500)

- TypeScript: Return more precise `Buffer<ArrayBuffer>` from `toBuffer`.
  [#&#8203;4520](lovell/sharp#4520)
  [@&#8203;Andarist](https://github.com/Andarist)

- Bound `clahe` width and height to avoid signed overflow.
  [#&#8203;4551](lovell/sharp#4551)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Bound `trim` margin to avoid signed overflow.
  [#&#8203;4552](lovell/sharp#4552)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Reject infinite values when validating numbers.
  [#&#8203;4553](lovell/sharp#4553)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Bound extract region to libvips coordinate limit.
  [#&#8203;4555](lovell/sharp#4555)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Verify background colour values are numbers.
  [#&#8203;4556](lovell/sharp#4556)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Bound create and raw input dimensions to coordinate limit.
  [#&#8203;4558](lovell/sharp#4558)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Tighten recomb and affine matrix verification.
  [#&#8203;4560](lovell/sharp#4560)
  [@&#8203;chatman-media](https://github.com/chatman-media)

- Verify cache memory limit to avoid overflow.
  [#&#8203;4561](lovell/sharp#4561)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yNDYuMSIsInVwZGF0ZWRJblZlciI6IjQzLjI0Ni4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
paultibbetts added a commit to paultibbetts/dev that referenced this pull request Jul 7, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@astrojs/starlight](https://starlight.astro.build) ([source](https://github.com/withastro/starlight/tree/HEAD/packages/starlight)) | [`^0.40.0` → `^0.41.0`](https://renovatebot.com/diffs/npm/@astrojs%2fstarlight/0.40.0/0.41.3) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@astrojs%2fstarlight/0.41.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@astrojs%2fstarlight/0.40.0/0.41.3?slim=true) |
| [sharp](https://sharp.pixelplumbing.com) ([source](https://github.com/lovell/sharp), [changelog](https://github.com/lovell/sharp/blob/main/docs/src/content/docs/changelog.md)) | [`0.35.2` → `0.35.3`](https://renovatebot.com/diffs/npm/sharp/0.35.2/0.35.3) | ![age](https://developer.mend.io/api/mc/badges/age/npm/sharp/0.35.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/sharp/0.35.2/0.35.3?slim=true) |

---

### Release Notes

<details>
<summary>withastro/starlight (@&#8203;astrojs/starlight)</summary>

### [`v0.41.3`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0413)

[Compare Source](https://github.com/withastro/starlight/compare/@astrojs/[email protected]...@astrojs/[email protected])

##### Patch Changes

- [#&#8203;3911](withastro/starlight#3911) [`1686ecc`](withastro/starlight@1686ecc) Thanks [@&#8203;timothyjordan](https://github.com/timothyjordan)! - Keeps keyboard focus inside the mobile menu while it is open, preventing focus moving to hidden interactive elements in page content.

### [`v0.41.2`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0412)

[Compare Source](https://github.com/withastro/starlight/compare/@astrojs/[email protected]...@astrojs/[email protected])

##### Patch Changes

- [#&#8203;4008](withastro/starlight#4008) [`58a3520`](withastro/starlight@58a3520) Thanks [@&#8203;FrancoKaddour](https://github.com/FrancoKaddour)! - Fixes the table of contents overflowing the right edge of the viewport when a custom `--sl-content-width` value exceeds available space

- [#&#8203;4015](withastro/starlight#4015) [`bdbfffc`](withastro/starlight@bdbfffc) Thanks [@&#8203;delucis](https://github.com/delucis)! - Fixes an issue where aside icons were rendered incorrectly in projects where Astro’s MDX integration had optimization disabled

### [`v0.41.1`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0411)

[Compare Source](https://github.com/withastro/starlight/compare/@astrojs/[email protected]...@astrojs/[email protected])

##### Patch Changes

- [#&#8203;3967](withastro/starlight#3967) [`72e63dc`](withastro/starlight@72e63dc) Thanks [@&#8203;HiDeoo](https://github.com/HiDeoo)! - Adds 2 new icons: `link` and `link-alt`.

- [#&#8203;3988](withastro/starlight#3988) [`ac55cfa`](withastro/starlight@ac55cfa) Thanks [@&#8203;delucis](https://github.com/delucis)! - Fixes a dependency resolution issue introduced in Starlight v0.41

- [#&#8203;3967](withastro/starlight#3967) [`72e63dc`](withastro/starlight@72e63dc) Thanks [@&#8203;HiDeoo](https://github.com/HiDeoo)! - Optimizes the icons of Markdown asides.

### [`v0.41.0`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0410)

[Compare Source](https://github.com/withastro/starlight/compare/@astrojs/[email protected]...@astrojs/[email protected])

##### Minor Changes

- [#&#8203;3951](withastro/starlight#3951) [`1202dd4`](withastro/starlight@1202dd4) Thanks [@&#8203;HiDeoo](https://github.com/HiDeoo)! - Adds support for Astro v7, drops support for Astro v6.

##### Upgrade Astro and dependencies

⚠️ **BREAKING CHANGE:** Astro v6 is no longer supported. Make sure you [update Astro](https://docs.astro.build/en/guides/upgrade-to/v7/) and any other official integrations at the same time as updating Starlight:

```sh
npx @&#8203;astrojs/upgrade
```

*Community Starlight plugins and Astro integrations may also need to be manually updated to work with Astro v7. If you encounter any issues, please reach out to the plugin or integration author to see if it is a known issue or if an updated version is being worked on.*

⚠️ **BREAKING CHANGE:** This release drops official support for Chromium-based browsers prior to version 111 (released 07 March 2023) and Safari-based browsers prior to version 16.4 (released 27 March 2023). You can find a list of currently supported browsers and their versions using this [browserslist query](https://browsersl.ist/#q=%3E+0.5%25%2C+not+dead%2C+Chrome+%3E%3D+111%2C+Edge+%3E%3D+111%2C+Firefox+%3E%3D+121%2C+Safari+%3E%3D+16.4%2C+iOS+%3E%3D+16.4%2C+not+op_mini+all).

##### Patch Changes

- [#&#8203;3953](withastro/starlight#3953) [`a935d33`](withastro/starlight@a935d33) Thanks [@&#8203;HiDeoo](https://github.com/HiDeoo)! - Fixes Starlight Markdown processing being potentially applied to files that should not be processed.

</details>

<details>
<summary>lovell/sharp (sharp)</summary>

### [`v0.35.3`](https://github.com/lovell/sharp/releases/tag/v0.35.3)

[Compare Source](lovell/sharp@v0.35.2...v0.35.3)

- Tighten verification of `text` dimensions, TIFF tile dimensions and `extend` values.

- Improve code bundler support by resolving path to libvips binary.

- Increase default concurrency when use of `MALLOC_ARENA_MAX` is detected.

- Emit warning about binaries provided by Electron for use on Linux.

- Add `hasAlpha` property to output `info`.
  [#&#8203;4500](lovell/sharp#4500)

- TypeScript: Return more precise `Buffer<ArrayBuffer>` from `toBuffer`.
  [#&#8203;4520](lovell/sharp#4520)
  [@&#8203;Andarist](https://github.com/Andarist)

- Bound `clahe` width and height to avoid signed overflow.
  [#&#8203;4551](lovell/sharp#4551)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Bound `trim` margin to avoid signed overflow.
  [#&#8203;4552](lovell/sharp#4552)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Reject infinite values when validating numbers.
  [#&#8203;4553](lovell/sharp#4553)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Bound extract region to libvips coordinate limit.
  [#&#8203;4555](lovell/sharp#4555)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Verify background colour values are numbers.
  [#&#8203;4556](lovell/sharp#4556)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Bound create and raw input dimensions to coordinate limit.
  [#&#8203;4558](lovell/sharp#4558)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Tighten recomb and affine matrix verification.
  [#&#8203;4560](lovell/sharp#4560)
  [@&#8203;chatman-media](https://github.com/chatman-media)

- Verify cache memory limit to avoid overflow.
  [#&#8203;4561](lovell/sharp#4561)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://github.com/renovatebot/renovate/discussions) if that's undesired.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44Mi4wIiwidXBkYXRlZEluVmVyIjoiNDMuODIuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIl19-->

---------

Co-authored-by: Paul Tibbetts <[email protected]>
Reviewed-on: https://gitea.cloud.paultibbetts.uk/paul/dev/pulls/139
Co-authored-by: Renovate Bot <[email protected]>
Co-committed-by: Renovate Bot <[email protected]>
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