Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

The deLoose function searches for UTF-8 replacement character sequences (0xEF 0xBF 0xBD) but had an off-by-one error allowing out-of-bounds reads.

Changes

  • utf8.js: Fixed bounds check when searching for 3-byte replacement sequence
    • Changed last = res.length - 2last = res.length - 3 (last valid position to read 3 consecutive bytes)
    • Changed loop condition start < laststart <= last
    • Added pos > last guard after indexOf to prevent OOB access when 0xEF found near array end
// Before: when pos = res.length - 2, accessing res[pos + 2] reads res[res.length]
const last = res.length - 2
while (start < last) {
  const pos = res.indexOf(0xef, start)
  if (pos === -1) break
  if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) { // OOB possible

// After: safe bounds for 3-byte sequence access
const last = res.length - 3
while (start <= last) {
  const pos = res.indexOf(0xef, start)
  if (pos === -1 || pos > last) break
  if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) { // always in bounds
Original prompt

This section details on the original issue you should resolve

<issue_title>Review the codebase and identify mistakes</issue_title>
<issue_description>Ignore tests/wpt and other vendored tests

Note: wif using synchronous path for async implementation is not an issue, it's done for perf, as underlying method is always sync (again, for perf).

Pay close attention to corner cases

Ignore utf-32 for now, it's not yet shipped and is incomplete.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copilot AI changed the title [WIP] Review codebase and identify mistakes Fix off-by-one bounds check in utf8.js deLoose function Jan 28, 2026
Copilot AI requested a review from ChALkeR January 28, 2026 21:04
@ChALkeR
Copy link
Collaborator

ChALkeR commented Jan 28, 2026

This has no real effect but makes sense for code clarity

@ChALkeR ChALkeR changed the title Fix off-by-one bounds check in utf8.js deLoose function refactor: cleaner bounds check in utf8.js deLoose Feb 1, 2026
@ChALkeR ChALkeR marked this pull request as ready for review February 1, 2026 18:59
@ChALkeR ChALkeR merged commit 92ef515 into main Feb 1, 2026
30 checks passed
@ChALkeR ChALkeR deleted the copilot/review-codebase-for-mistakes-one-more-time branch February 1, 2026 22:58
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.

Review the codebase and identify mistakes

2 participants