Skip to content

Commit

Permalink
fix some potential memleaks and use the newer caml_stat_strdup
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Feb 16, 2024
1 parent 5af72e2 commit fc06d16
Showing 1 changed file with 27 additions and 7 deletions.
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)));
*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

0 comments on commit fc06d16

Please sign in to comment.