fix: correct memcpy size in or_block function #24567
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #24529
Caution
This solution was provided by an AI after several hours of research, as I'm not familiar with the base code. Review it carefully and reject it if necessary.
All tests passed:
Additional context (conversacion with Claude Sonnet 4):
gchumillas: Hello, could you explain to me why these changes were made?
In particular, I'm concerned that the following line was removed:
Excellent question. Let me explain why these changes were made and why it was necessary to remove that specific line.
Problem Context
The bug we were fixing was that the V compiler generated incorrect C code for error propagation with the
!
operator. Specifically, the generated code tried to access the.is_error
field onoption
type structures, butoption
structures don't have that field - they only have.state
.Problem with the removed line
The line you're concerned about:
Had several problems:
sizeof(${result_name})
: This line always usedsizeof(${result_name})
regardless of whether the type wasresult
oroption
. This caused problems because:result
andoption
types have different structuresresult_name
is a constant that refers specifically to result typesmemcpy
function copied the entire structure without considering that:result
types use.is_error
(boolean)option
types use.state
(byte with values 0, 1, 2)The implemented solution
The new code is more explicit and correct:
Advantages of the new implementation:
.is_error = true
for results (indicates error).state = 2
for options (indicates error state)= {0}
initializes the entire structure to zero.err
field which is common to both typesVerification that no functionality was lost
The original function tried to copy everything with
memcpy
, but this was problematic. The new implementation:The comment
// Optionally zero data: memset(...)
indicates that if necessary, explicit data initialization could be added, but the{0}
initialization already handles this correctly.Result
The tests confirm that functionality remains intact while fixing the bug of incorrect C code generation. Removing the
memcpy
was necessary because it was causing the fundamental problem we were solving.