Skip to content

Commit a1f038d

Browse files
Merge pull request #238 from genaris/fix/js-string-free
fix(js): free native strings
2 parents 58880b3 + 9cd430c commit a1f038d

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

wrappers/javascript/anoncreds-nodejs/src/NodeJSAnoncreds.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ import {
3232
} from './ffi'
3333
import { getNativeAnoncreds } from './library'
3434

35-
function handleReturnPointer<Return>(returnValue: Buffer): Return {
35+
function handleReturnPointer<Return>(returnValue: Buffer, cleanupCallback?: (buffer: Buffer) => void): Return {
3636
if (returnValue.address() === 0) {
3737
throw AnoncredsError.customError({ message: 'Unexpected null pointer' })
3838
}
3939

40-
return returnValue.deref() as Return
40+
const ret = returnValue.deref() as Return
41+
if (cleanupCallback) cleanupCallback(returnValue)
42+
43+
return ret
4144
}
4245

4346
export class NodeJSAnoncreds implements Anoncreds {
@@ -60,7 +63,7 @@ export class NodeJSAnoncreds implements Anoncreds {
6063
this.nativeAnoncreds.anoncreds_generate_nonce(ret)
6164
this.handleError()
6265

63-
return handleReturnPointer<string>(ret)
66+
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
6467
}
6568

6669
public createSchema(options: {
@@ -86,7 +89,7 @@ export class NodeJSAnoncreds implements Anoncreds {
8689
this.nativeAnoncreds.anoncreds_revocation_registry_definition_get_attribute(objectHandle, name, ret)
8790
this.handleError()
8891

89-
return handleReturnPointer<string>(ret)
92+
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
9093
}
9194

9295
public credentialGetAttribute(options: { objectHandle: ObjectHandle; name: string }) {
@@ -96,7 +99,7 @@ export class NodeJSAnoncreds implements Anoncreds {
9699
this.nativeAnoncreds.anoncreds_credential_get_attribute(objectHandle, name, ret)
97100
this.handleError()
98101

99-
return handleReturnPointer<string>(ret)
102+
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
100103
}
101104

102105
public createCredentialDefinition(options: {
@@ -215,7 +218,7 @@ export class NodeJSAnoncreds implements Anoncreds {
215218
this.nativeAnoncreds.anoncreds_encode_credential_attributes(attributeRawValues, ret)
216219
this.handleError()
217220

218-
const result = handleReturnPointer<string>(ret)
221+
const result = handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
219222

220223
return result.split(',')
221224
}
@@ -296,7 +299,7 @@ export class NodeJSAnoncreds implements Anoncreds {
296299
this.nativeAnoncreds.anoncreds_create_link_secret(ret)
297300
this.handleError()
298301

299-
return handleReturnPointer<string>(ret)
302+
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
300303
}
301304

302305
public createPresentation(options: {
@@ -639,7 +642,7 @@ export class NodeJSAnoncreds implements Anoncreds {
639642
this.nativeAnoncreds.anoncreds_get_current_error(ret)
640643
this.handleError()
641644

642-
return handleReturnPointer<string>(ret)
645+
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
643646
}
644647

645648
private objectFromJson(method: (byteBuffer: Buffer, ret: Buffer) => unknown, options: { json: string }) {
@@ -721,9 +724,13 @@ export class NodeJSAnoncreds implements Anoncreds {
721724
this.handleError()
722725

723726
const returnValue = handleReturnPointer<{ data: Buffer; len: number }>(ret)
724-
const output = new Uint8Array(byteBufferToBuffer(returnValue))
727+
const jsonAsArray = new Uint8Array(byteBufferToBuffer(returnValue))
728+
729+
const output = new TextDecoder().decode(jsonAsArray)
730+
731+
this.nativeAnoncreds.anoncreds_buffer_free(returnValue.data)
725732

726-
return new TextDecoder().decode(output)
733+
return output
727734
}
728735

729736
public getTypeName(options: { objectHandle: ObjectHandle }) {
@@ -734,7 +741,7 @@ export class NodeJSAnoncreds implements Anoncreds {
734741
this.nativeAnoncreds.anoncreds_object_get_type_name(objectHandle, ret)
735742
this.handleError()
736743

737-
return handleReturnPointer<string>(ret)
744+
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
738745
}
739746

740747
public objectFree(options: { objectHandle: ObjectHandle }) {

wrappers/javascript/anoncreds-nodejs/src/library/bindings.ts

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export const nativeBindings = {
120120
anoncreds_generate_nonce: [FFI_ERRORCODE, [FFI_STRING_PTR]],
121121
anoncreds_get_current_error: [FFI_ERRORCODE, [FFI_STRING_PTR]],
122122
anoncreds_object_free: [FFI_VOID, [FFI_OBJECT_HANDLE]],
123+
anoncreds_string_free: [FFI_VOID, [FFI_STRING_PTR]],
123124
anoncreds_object_get_json: [FFI_ERRORCODE, [FFI_OBJECT_HANDLE, ByteBufferStructPtr]],
124125
anoncreds_object_get_type_name: [FFI_ERRORCODE, [FFI_OBJECT_HANDLE, FFI_STRING_PTR]],
125126
anoncreds_presentation_request_from_json: [FFI_ERRORCODE, [ByteBufferStruct, FFI_STRING_PTR]],

wrappers/javascript/anoncreds-react-native/cpp/turboModuleUtility.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
4949
? jsi::Value::null()
5050
: jsi::String::createFromAscii(rt, *value);
5151
object.setProperty(rt, "value", valueWithoutNullptr);
52+
53+
if (!isNullptr) anoncreds_string_free((char *)*value);
5254
}
5355

5456
object.setProperty(rt, "errorCode", int(code));
@@ -98,6 +100,8 @@ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
98100
? jsi::Value::null()
99101
: jsi::String::createFromUtf8(rt, value->data, value->len);
100102
object.setProperty(rt, "value", valueWithoutNullptr);
103+
104+
if (value != nullptr) anoncreds_buffer_free(*value);
101105
}
102106

103107
object.setProperty(rt, "errorCode", int(code));

0 commit comments

Comments
 (0)