Repository navigation
Conversation
PS sizes a set as ceil(members / 8) bytes. Delphi rounds 3 bytes up to 4, and on Win64 5..7 bytes up to 8. On the InvokeCall path the two sizes were assumed equal, which breaks sets of 17..24 members everywhere and of 33..56 members on Win64: - result, 3 bytes: written as a Longint, one byte past the variable; - result, 5..7 bytes: the RTTI lookup filtered on size, found nothing, skipped the call and still returned True, so the script got an unwritten result without any error; - var parameter: the callee writes its own size through the pointer, past the end of the script variable; - by-value parameter: the callee reads past the end. The Delphi type is now looked up by name with the size as a preference, not a filter, and each direction copies only the bytes both sides have: 3-byte sets go through a zero-extended Cardinal, wider ones through a zero-padded buffer of the Delphi size, and var sets copy the script's share back after the call. Types whose sizes agree resolve and travel exactly as before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
PS sizes a set as
ceil(members / 8)bytes. Delphi rounds 3 bytes up to 4, and on Win64 also 5..7 bytes up to 8:On the
InvokeCall.incpath (the default since Delphi 10.1) the two sizes are assumed to be equal, so a set of 17..24 members (everywhere) or 33..56 members (Win64) breaks when it crosses the script/Delphi boundary:Longint(res.Dta^) := ..., one byte past the script variable;TypeSize = aByteSize, finds nothing, skips theInvokeandInnerfuseCallstill returnsTrue. The native function is never called and the script gets an unwritten result, with no error at all;Change (
Source/InvokeCall.inc)FindNativeSetTypelooks the Delphi type up by name with the size as a preference, not a filter: a type with the same name and size wins, otherwise the same name at Delphi's size. It returnsPTypeInfoand the size, not aTRttiType, so nothing outlives theTRttiContext.Cardinal; as a result, theCardinalis invoked and only 3 bytes are kept; as a var parameter, a 4-byte buffer.TOpenArrayData.DataSet); as a result,min(PS, Delphi)bytes are copied; as a var parameter, a buffer of the Delphi size whose script share is copied back after the call.Repro / testing
Delphi 13, current master, Win32 and Win64. Two native set types (20 and 44 members, declared in a unit interface so RTTI can see them), registered functions returning them, taking them by value and as
var. In the script each set sits in a record followed by guard bytes, so writes past the end become visible:The by-value over-read is not visible from the script (the extra bytes only hold bits of non-existent members); it is fixed the same way for safety.
We have been running the same approach in production since August (with a different type lookup); this PR rewrites it against the current upstream code.