xavierleroy / camlidl

Stub code generator for OCaml/C interface
Other
34 stars 8 forks source link

Using const qualifier on arrays (non-string) generates broken code #9

Closed greg-kennedy closed 1 year ago

greg-kennedy commented 5 years ago

Using this sample test IDL:

void test_const_array ( [in] int array_size, [in, size_is(array_size)] const char * array );

generates code that does not compile:

value camlidl_test_test_const_array(
        value _v_array)
{
  int array_size; /*in*/
  char const *array; /*in*/
  mlsize_t _c1;
  mlsize_t _c2;
  value _v3;
  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  _c1 = Wosize_val(_v_array);
  array = camlidl_malloc(_c1 * sizeof(char const ), _ctx);
  for (_c2 = 0; _c2 < _c1; _c2++) {
    _v3 = Field(_v_array, _c2);
    array[_c2] = Int_val(_v3);
  }
  array_size = _c1;
  test_const_array(array_size, array);
  camlidl_free(_ctx);
  return Val_unit;
}

The problem is that the const is carried to an internal array here: char const *array; /*in*/ ... but then in the loop that copies input ML to C, const prevents this assignment: array[_c2] = Int_val(_v3);

Now, the usefulness of const on an interface .h is debatable... regardless, it's really not clear why CamlIDL breaks here, from a user perspective. Simply removing const from the internal array within the function leads to code that does compile properly. Or, change the documentation to note this : )

xavierleroy commented 1 year ago

At long last, this issue should be fixed in release 1.10 (commit d5d11eb).