-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: free strings after being read to prevent potential memory leaks
- Loading branch information
Showing
6 changed files
with
34 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import { dylib } from "../mod.ts"; | ||
|
||
export type Sanitizer = () => void | PromiseLike<void>; | ||
|
||
const textEncoder = new TextEncoder(); | ||
export function CString(string: string): Deno.PointerValue<string> { | ||
return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`)); | ||
} | ||
|
||
export function readCString(pointer: Deno.PointerObject): string; | ||
export function readCString(pointer: Deno.PointerValue): string | null; | ||
export function readCString(pointer: Deno.PointerValue): string | null { | ||
// This method reads string from the given pointer and frees the string | ||
// SAFETY: Do not use readCString twice on the same pointer as it will cause double free | ||
// In that case just use `Deno.UnsafePointerView(ptr).getCString()` directly | ||
export async function readCString(pointer: Deno.PointerObject): Promise<string>; | ||
export async function readCString(pointer: Deno.PointerValue): Promise<string | null>; | ||
export async function readCString(pointer: Deno.PointerValue): Promise<string | null> { | ||
if (!pointer) return null; | ||
return new Deno.UnsafePointerView(pointer).getCString(); | ||
const string = new Deno.UnsafePointerView(pointer).getCString(); | ||
await dylib.symbols.MONERO_free(pointer); | ||
return string; | ||
} |
This file contains 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