Repository navigation
TSLANG_ISSUES 6: a string returned by C is copied - #575
Merged
Merged
Conversation
A `string` result of a C function (a top-level `declare function` with no definition in the program) or of a @cpp method was C's `char*` itself: a buffer C may reuse (`strerror(2)` read the next call's text) or the middle of another string (`strstr`). Under -mm=rc the reference count lives in the 8 bytes before a string, so retaining it wrote into memory that isn't the program's: storing `strchr(base, 'k')` rewrote two characters of `base`, and a release reaching zero would free C's pointer. The call's result now goes through ts.StringCopy (null stays null), owned like any fresh string. `Opaque` is the way to get the pointer itself. A function in a `declare namespace` or `declare class` (the default library's lib.d.ts) is TypeScript and isn't copied. TSLANG_ISSUES.md 6. Needs TypeScriptCompilerDefaultLib#33, which declares strstr, setlocale, regexp_* and the C time functions `Opaque` (or `int`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
added a commit
that referenced
this pull request
Oct 17, 2026
ASDAlexander77
added a commit
that referenced
this pull request
Oct 31, 2026
…first TSLANG_ISSUES 21. The string a C function returns is copied into a string of the program's own (#575), but not when the `.d.ts` declaring it was first reached through a module this file imports: `declaredFunctions` kept the module of that earlier pass, and isForeignCall wanted the one being generated. The raw `char*` was taken as it was - stored strerror() results showed the buffer's last text, and under -mm=rc the count was written into C's memory and the release freed it (native_ts_graphics on Android: "Scudo ERROR: misaligned pointer when deallocating"). A top-level body-less `declare function` is C whichever module's pass loaded its `.d.ts`: isForeignCall now asks that, and that this module's function has no body. Test c-string-import/ (gc, rc, none, own; compiled and JIT), whose `.d.ts` is outside tests/, where *.d.ts are build output and ignored. ctest 6560/6560; fuzz seeds 85000-85599 clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 31, 2026
ASDAlexander77
added a commit
that referenced
this pull request
Oct 31, 2026
…n imported .d.ts (#761) * @value T | null: ?: of a literal holding a new object (rc), ??, and print Three failures of a @value type that may be missing, all on main before #760: - rc: `d < 0 ? null : { at: d, owner: new Owner(d) }` returned as `Mark | null`. The branch released the new object before the result's retain after the join, which then retained freed memory. consumeConditionalResults' producerOf looks through a tuple cast to the @value type it is laid out as, so yieldsLiteralOfBranchTemporary sees the literal and the branch retains first. - `h ?? find(3)` was typed `Hit | (Hit | null)` and did not compile: an optional member of a union gave its element as one member. processUnionTypeItem now processes the element like any other member, so a union in it gives its members. - print / string conversion: an undefined `T | undefined` printed as null or as a zeroed T (the optional went through ValueOrDefault); it is "undefined" now. A null `T | null` stored without a tag (#760) printed T's fields; castNullableToString takes a @value base too and prints "null". Test value/value_missing_ops (gc, rc, none; own cannot yet tell which branch of a `?:` owns a literal it merges). ctest 6546/6546; fuzz seeds 83000-83599 clean; DefaultLib compiles under own and rc. Printing a @value value holding an interface still crashes, as printing any interface does - not changed here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * An interface printed shows its fields, as an object literal does Printing an interface value, or putting it into a string, crashed at run time, whatever object was behind it - a class, an object literal, or one a @value value holds. USER RULING: print its fields. castInterfaceToString: - its `toString` when the interface has one, as a class's is; - else its fields, read through the interface, those of the interfaces it extends first, in an object literal's format (`{ n: 3, name: 'b' }`, fieldsToString, which castTupleToString now shares); - null as "null"; - a field of an interface type already being printed is `[Object]`: the text is made inline, and `interface Chain { next: Chain }` nests without end. Test tests/00interface_print (every model). ctest 6552/6552; fuzz seeds 84000-84599 clean; DefaultLib compiles under all four models. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * A C string result is copied when an imported module loaded its .d.ts first TSLANG_ISSUES 21. The string a C function returns is copied into a string of the program's own (#575), but not when the `.d.ts` declaring it was first reached through a module this file imports: `declaredFunctions` kept the module of that earlier pass, and isForeignCall wanted the one being generated. The raw `char*` was taken as it was - stored strerror() results showed the buffer's last text, and under -mm=rc the count was written into C's memory and the release freed it (native_ts_graphics on Android: "Scudo ERROR: misaligned pointer when deallocating"). A top-level body-less `declare function` is C whichever module's pass loaded its `.d.ts`: isForeignCall now asks that, and that this module's function has no body. Test c-string-import/ (gc, rc, none, own; compiled and JIT), whose `.d.ts` is outside tests/, where *.d.ts are build output and ignored. ctest 6560/6560; fuzz seeds 85000-85599 clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
added a commit
that referenced
this pull request
Oct 31, 2026
… through an import (#762) #575's copy of a C function's `string` result is skipped when the `declare function` is in a .d.ts that an imported module also references (isForeignCall checks the module the declaration was recorded with). The raw char* then becomes a program string again: aliased buffers under gc, C++ memory counted and freed under rc (native_ts_graphics's SampleGame crashed on Android). Repro run on pre-alpha111, the IR, where to look and a suggested regression test. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
TSLANG_ISSUES.md 6: a
stringreturned by C was C'schar*itself.It was worse than stale text:
a = strerror(2); b = strerror(13)printed the second text twice;-mm=rcthe reference count lives in the 8 bytes before a string, so retaining C's pointer wrote into memory that isn't the program's. Storingstrchr(base, 'k')turnedabcdefghijklmnop1intoabddefghijklmnop1, thenabedefghijklmnop1; a release reaching zero would free C's pointer.Change: the result of a call into C, meaning a top-level
declare functionthat nothing in the program defines or a@cppmethod (ts.cpp_call), goes throughts.StringCopy. Null stays null, and the copy is owned like any fresh string (markFreshBlockOwned). Declaring the resultOpaquegives the raw pointer, for pointer arithmetic or for a string the caller must free. A function in adeclare namespaceordeclare class, as DefaultLib'slib.d.tsdeclares its methods, is TypeScript and isn't copied.Merge order: DefaultLib must merge first: ASDAlexander77/TypeScriptCompilerDefaultLib#33. It declares
strstr,setlocale,regexp_*and the C time functionsOpaque(intfor the two that returnerrno_t). DefaultLib built from older sources with this compiler would breakindexOfandRegExp.replace.Verified:
00c_string_copy.tsfails on alpha100 under gc and rc, and passes under gc, rc, own and none;--verify-ownershipis silent under rc and own;Known extra cost: DefaultLib's own TypeScript helpers declared top-level in
core.os.d.ts(convertNumber,time_*) are in a separately compiled object, solib.tscopies their results once more (toFixed, Date strings).Follow-ups, not in this PR:
stringparameters of@cppmethods that C++ calls (the method hasts.cpp_method): copying needs a release at function exit under rc;stringfields of C structs can't be told apart from TypeScript tuples.Docs:
docs/c-cpp-header-import.mdanddocs/cpp-classes.md;TSLANG_ISSUES.mdrow and section 6.🤖 Generated with Claude Code