Skip to content

Commit

Permalink
Merge pull request #238 from genaris/fix/js-string-free
Browse files Browse the repository at this point in the history
fix(js): free native strings
  • Loading branch information
andrewwhitehead authored Sep 5, 2023
2 parents 58880b3 + 9cd430c commit a1f038d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
29 changes: 18 additions & 11 deletions wrappers/javascript/anoncreds-nodejs/src/NodeJSAnoncreds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ import {
} from './ffi'
import { getNativeAnoncreds } from './library'

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

return returnValue.deref() as Return
const ret = returnValue.deref() as Return
if (cleanupCallback) cleanupCallback(returnValue)

return ret
}

export class NodeJSAnoncreds implements Anoncreds {
Expand All @@ -60,7 +63,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_generate_nonce(ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

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

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

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

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public createCredentialDefinition(options: {
Expand Down Expand Up @@ -215,7 +218,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_encode_credential_attributes(attributeRawValues, ret)
this.handleError()

const result = handleReturnPointer<string>(ret)
const result = handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)

return result.split(',')
}
Expand Down Expand Up @@ -296,7 +299,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_create_link_secret(ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public createPresentation(options: {
Expand Down Expand Up @@ -639,7 +642,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_get_current_error(ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

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

const returnValue = handleReturnPointer<{ data: Buffer; len: number }>(ret)
const output = new Uint8Array(byteBufferToBuffer(returnValue))
const jsonAsArray = new Uint8Array(byteBufferToBuffer(returnValue))

const output = new TextDecoder().decode(jsonAsArray)

this.nativeAnoncreds.anoncreds_buffer_free(returnValue.data)

return new TextDecoder().decode(output)
return output
}

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

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public objectFree(options: { objectHandle: ObjectHandle }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const nativeBindings = {
anoncreds_generate_nonce: [FFI_ERRORCODE, [FFI_STRING_PTR]],
anoncreds_get_current_error: [FFI_ERRORCODE, [FFI_STRING_PTR]],
anoncreds_object_free: [FFI_VOID, [FFI_OBJECT_HANDLE]],
anoncreds_string_free: [FFI_VOID, [FFI_STRING_PTR]],
anoncreds_object_get_json: [FFI_ERRORCODE, [FFI_OBJECT_HANDLE, ByteBufferStructPtr]],
anoncreds_object_get_type_name: [FFI_ERRORCODE, [FFI_OBJECT_HANDLE, FFI_STRING_PTR]],
anoncreds_presentation_request_from_json: [FFI_ERRORCODE, [ByteBufferStruct, FFI_STRING_PTR]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
? jsi::Value::null()
: jsi::String::createFromAscii(rt, *value);
object.setProperty(rt, "value", valueWithoutNullptr);

if (!isNullptr) anoncreds_string_free((char *)*value);
}

object.setProperty(rt, "errorCode", int(code));
Expand Down Expand Up @@ -98,6 +100,8 @@ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
? jsi::Value::null()
: jsi::String::createFromUtf8(rt, value->data, value->len);
object.setProperty(rt, "value", valueWithoutNullptr);

if (value != nullptr) anoncreds_buffer_free(*value);
}

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

0 comments on commit a1f038d

Please sign in to comment.