fix(core): handle invalid bytecode in getUnlinkedBytecode for library-linked contracts#1246
Conversation
…info contains library-linked contracts When a build-info file contains contracts with external library link references, getUnlinkedBytecode() would throw 'Bytecode is not a valid hex string' for unrelated contracts with no library dependencies. Root cause: unlinkBytecode() applies wrong link references to the target bytecode, corrupting it. getVersion() then throws on the invalid hex. Fix: wrap getVersion() call in try/catch and skip candidates that produce invalid bytecode, matching the intended fallthrough behavior. Fixes OpenZeppelin#1227
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThis PR fixes a crash in ChangesBug fix for getUnlinkedBytecode crash
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/core/src/validate/query.test.ts (1)
50-85: ⚡ Quick winAdd one more regression for the
unlinkBytecode-throws path.This test is solid for invalid-hex-after-unlinking. Adding a second case with out-of-bounds/malformed link references would lock in the non-fatal behavior for direct
unlinkBytecodefailures too.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/validate/query.test.ts` around lines 50 - 85, Add a second regression test that directly exercises unlinkBytecode to ensure out-of-bounds or malformed link references do not cause a fatal error: create a mock validation entry (e.g., LibraryLinkedContract) with a linkReferences array whose start/length exceed the provided bytecode bounds or contain malformed placeholders, then call unlinkBytecode(validation as ValidationRunData, targetBytecode) and assert it either throws a controlled, documented error (using t.throws with the expected message) or safely returns the original bytecode depending on the expected behavior; reference the existing test helpers and symbols getUnlinkedBytecode, unlinkBytecode, ValidationRunData, and the sample targetBytecode/SimpleContract setup so the new test mirrors the existing structure but targets the unlinkBytecode failure path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/validate/query.ts`:
- Around line 132-142: The try/catch only wraps getVersion so unlinkBytecode
errors can still crash getUnlinkedBytecode; move the call to
unlinkBytecode(bytecode, linkReferences) inside the same try block that calls
getVersion (or wrap both in a single try) and on any exception continue to the
next candidate, ensuring the logic that checks
validation[name].version?.withMetadata remains the same.
---
Nitpick comments:
In `@packages/core/src/validate/query.test.ts`:
- Around line 50-85: Add a second regression test that directly exercises
unlinkBytecode to ensure out-of-bounds or malformed link references do not cause
a fatal error: create a mock validation entry (e.g., LibraryLinkedContract) with
a linkReferences array whose start/length exceed the provided bytecode bounds or
contain malformed placeholders, then call unlinkBytecode(validation as
ValidationRunData, targetBytecode) and assert it either throws a controlled,
documented error (using t.throws with the expected message) or safely returns
the original bytecode depending on the expected behavior; reference the existing
test helpers and symbols getUnlinkedBytecode, unlinkBytecode, ValidationRunData,
and the sample targetBytecode/SimpleContract setup so the new test mirrors the
existing structure but targets the unlinkBytecode failure path.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3645d6fc-6068-44ac-9a34-95119c1bca39
📒 Files selected for processing (2)
packages/core/src/validate/query.test.tspackages/core/src/validate/query.ts
Motivation
Fixes #1227
getUnlinkedBytecode()throws"Bytecode is not a valid hex string"whena Hardhat build-info file contains contracts with external library link
references (e.g.,
LockupLib,ERC3643BatchLib), even when the contractbeing deployed has no library dependencies.
Root Cause
The function iterates over all contracts with
linkReferencesand appliestheir link references to the target bytecode via
unlinkBytecode(). Whenwrong link references are applied, the resulting bytecode is corrupted.
getVersion()then throws on the invalid hex string before the versioncheck can determine it's not a match.
Fix
Wrap the
getVersion()call in a try/catch. WhenunlinkBytecode()withwrong link references produces invalid bytecode, skip that candidate and
continue to the next one.
Testing
Added regression test. All 588 tests pass.
Summary by CodeRabbit
Bug Fixes
Tests