yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
369 stars 95 forks source link

Distinguish bytes and string in generated stub code #622

Closed yallop closed 4 years ago

yallop commented 4 years ago

OCaml 4.10 defaults to the force-safe-string mode, so that the String_val macro returns const char *.

This PR updates ctypes to generate calls to Bytes_val rather than String_val for uses of ocaml_bytes, ensuring const-correctness and fixing problems such as the one reported here.

fdopen commented 4 years ago

Still insufficent in case of ocaml_bytes.

Bytes_val returns unsigned char *: https://github.com/ocaml/ocaml/blob/646d30404e6b5fa0d49aea3860cbf4efe3910601/runtime/caml/mlvalues.h#L269

The corresponding c type for Ctypes.ocaml_bytes is char *: https://github.com/ocamllabs/ocaml-ctypes/blob/4327af50c24dbfdf5f9ba9c22b8f432de290b50d/src/ctypes/ctypes_type_printing.ml#L77

And clang triggers a warning message by default for such casts: https://clang.llvm.org/docs/DiagnosticsReference.html#wpointer-sign

We should it either use ptr uchar for ocaml_bytes in the snippet above - or cast the unsigned specifier away inside the CTYPES_PTR_OF_OCAML_BYTES macro. Then there is at least the possibility that no warning is triggered.

yallop commented 4 years ago

We should it either use ptr uchar for ocaml_bytes in the snippet above

This seems like the best fix to me. #625 makes this change.