{"record":{"id":"85dfed09c1d63ed0","repo":"oven-sh/bun","slug":"memory-lifetime-is-ambiguous-when-returning-utf8st","errorCode":null,"errorMessage":"Memory lifetime is ambiguous when returning UTF8String","messagePattern":"Memory lifetime is ambiguous when returning UTF8String","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/codegen/bindgen.ts","lineNumber":391,"sourceCode":"      cpp.line(\"}\");\n      cpp.line(\"return JSC::JSValue::encode(JSC::jsUndefined());\");\n      break;\n    case \"basic-out-param\":\n      addCommaAfterArgument();\n      cpp.add(\"&out\");\n      cpp.line();\n      cpp.dedent();\n      cpp.line(\")) {\");\n      cpp.line(`    return {};`);\n      cpp.line(\"}\");\n      const simpleType = getSimpleIdlType(variant.ret);\n      if (simpleType) {\n        cpp.line(`return JSC::JSValue::encode(WebCore::toJS<${simpleType}>(*global, out));`);\n        break;\n      }\n      switch (variant.ret.kind) {\n        case \"UTF8String\":\n          throw new Error(\"Memory lifetime is ambiguous when returning UTF8String\");\n        case \"DOMString\":\n        case \"USVString\":\n        case \"ByteString\":\n          cpp.line(\n            `return JSC::JSValue::encode(WebCore::toJS<WebCore::IDL${variant.ret.kind}>(*global, out.transferToWTFString()));`,\n          );\n          break;\n      }\n      break;\n    default:\n      throw new Error(`TODO: emitCppCallToVariant for ${inspect(returnStrategy)}`);\n  }\n}\n\n/** If a simple IDL type mapping exists, it also currently means there is a direct C ABI mapping */\nfunction getSimpleIdlType(type: TypeImpl): string | undefined {\n  const map: { [K in TypeKind]?: string } = {\n    boolean: \"WebCore::IDLBoolean\",","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/src/codegen/bindgen.ts#L373-L409","documentation":"Guard in the bindgen out-parameter code path: the variant's return type is UTF8String, which the generator cannot emit because it is unclear who owns and frees the C-string after conversion to a JS value. It is a deliberate unsupported-combination sentinel, not a runtime failure — the .bind.ts declaration chose a return kind with no defined ownership transfer semantics.","triggerScenarios":"Thrown at src/codegen/bindgen.ts:391 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Change the variant's return type to a kind with explicit ownership (e.g. a JS-owned string type the generator supports)","Add a codegen arm for UTF8String out-params that copies the string into a JS value and defines the free strategy","Return a wrapper object whose finalizer owns the memory instead of a raw UTF8String"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","contentChangedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}