fix: pass Typst source across the FFI boundary with an explicit length - #34
Merged
evolvedlight merged 1 commit intoAug 31, 2026
Merged
Conversation
The source was handed to the native library as a NUL-terminated C string, so a document containing a NUL byte was cut off at the first one. Nothing reported an error: the truncated document compiled cleanly and the caller got back a result that silently omitted everything after the NUL. Pass the source as a (pointer, length) pair instead. A null pointer keeps its meaning of "no in-memory source", which stays distinct from a zero length, meaning an empty document. Related to evolvedlight#27.
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.
Follows on from #33, which moved the diagnostics onto
(pointer, length)but left the inbound source as a C string.create_compilerreadinput_sourcewithCStr::from_ptr, so a document containing a NUL byte was cut off at the first one. Nothing reported an error: the truncated document compiled cleanly and the caller got back a result that silently omitted everything after the NUL.Marshal.StringToCoTaskMemUTF8writes the full bytes, interior NUL included, so the loss happened entirely on the native side.The source now crosses as a
(pointer, length)pair. A null pointer still means "no in-memory source", which stays distinct from a zero length meaning an empty document.fixedover an empty array yields null, so the managed side routes an empty source through a one-byte placeholder to keep those two apart.Paths,
sys_inputsand the format strings stay C strings on purpose. A path cannot contain a NUL on either Windows or POSIX, and JSON carries a NUL as a six-character escape rather than the byte itself, so that transport never sees it. Encoding the difference between "a path" and "arbitrary user text" in the signature seemed worth keeping.Tests
source_after_a_nul_byte_is_still_compiledputs a syntax error after the NUL and asserts it is reported. With the truncation restored it fails with "invalid document compiled without an error", which is the bug.SourceAfterNullByteIsNotTruncatedasserts a heading after the NUL is present in the rendered PDF.