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>
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