Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some potential memleaks and use the newer caml_stat_strdup #20

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ jobs:
ocaml-compiler:
- 4.03.x
- 4.14.x
- 5.1.x
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: ocaml/setup-ocaml@v2
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
allow-prerelease-opam: true
- run: opam pin -n .
- run: opam depext -yt linenoise
- run: opam install -t . --deps-only
Expand Down
34 changes: 27 additions & 7 deletions src/linenoise_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ CAMLprim value ml_catch_break(value flag)
CAMLprim value ml_add_completion(value completions, value new_completion)
{
CAMLparam2(completions, new_completion);
linenoiseAddCompletion((linenoiseCompletions *)completions,
caml_strdup(String_val(new_completion)));
char* c_new_completion = caml_stat_strdup(String_val(new_completion));
linenoiseAddCompletion((linenoiseCompletions *)completions, c_new_completion);
caml_stat_free(c_new_completion);
CAMLreturn(Val_unit);
}

Expand All @@ -57,29 +58,39 @@ static char *hints_bridge(const char *buf, int *color, int *bold)
if (cb_result == Val_none) {
CAMLreturnT(char *,NULL);
} else {
char *msg = caml_strdup(String_val(Field(Field(cb_result, 0), 0)));
char* msg = caml_stat_strdup(String_val(Field(Field(cb_result, 0), 0)));
c-cube marked this conversation as resolved.
Show resolved Hide resolved
*color = Int_val(Field(Field(cb_result, 0), 1)) + 31;
*bold = Bool_val(Field(Field(cb_result, 0), 2));
CAMLreturnT(char *,msg);
}
}

static void free_hints_bridge(void* data) {
caml_stat_free(data);
}

__attribute__((constructor))
void set_free_hints(void) { linenoiseSetFreeHintsCallback(free); }

CAMLprim value ml_setup_bridges(value unit) {
CAMLparam1(unit);
linenoiseSetCompletionCallback(completion_bridge);
linenoiseSetHintsCallback(hints_bridge);
linenoiseSetFreeHintsCallback(free_hints_bridge);
CAMLreturn(Val_unit);
}

CAMLprim value ml_linenoise(value prompt)
{
CAMLparam1(prompt);
CAMLlocal1(lnoise_result);

linenoiseWasInterrupted = 0; // reset
const char *result = linenoise(caml_strdup(String_val(prompt)));
char* c_prompt = caml_stat_strdup(String_val(prompt));

const char *result = linenoise(c_prompt);

caml_stat_free(c_prompt);
if (!result) {
if (linenoiseWasInterrupted && raise_sys_break) {
caml_raise_constant(*caml_named_value("sys_break"));
Expand All @@ -95,7 +106,10 @@ CAMLprim value ml_linenoise(value prompt)
CAMLprim value ml_history_add(value line)
{
CAMLparam1(line);
CAMLreturn(Val_int(linenoiseHistoryAdd(caml_strdup(String_val(line)))));
char* c_line = caml_stat_strdup(String_val(line));
int res = linenoiseHistoryAdd(c_line);
caml_stat_free(c_line);
CAMLreturn(Val_int(res));
}

CAMLprim value ml_history_set_maxlen(value max)
Expand All @@ -107,13 +121,19 @@ CAMLprim value ml_history_set_maxlen(value max)
CAMLprim value ml_history_save(value filename)
{
CAMLparam1(filename);
CAMLreturn(Val_int(linenoiseHistorySave(caml_strdup(String_val(filename)))));
char* c_filename = caml_stat_strdup(String_val(filename));
int res = linenoiseHistorySave(c_filename);
caml_stat_free(c_filename);
CAMLreturn(Val_int(res));
}

CAMLprim value ml_history_load(value filename)
{
CAMLparam1(filename);
CAMLreturn(Val_int(linenoiseHistoryLoad(caml_strdup(String_val(filename)))));
char* c_filename= caml_stat_strdup(String_val(filename));
int res = linenoiseHistoryLoad(c_filename);
caml_stat_free(c_filename);
CAMLreturn(Val_int(res));
}

CAMLprim value ml_clearscreen(__attribute__((unused))value unit)
Expand Down
Loading