[ocaml-ctypes] Ctypes and structs containing strings

Marcello Seri marcello.seri at citrix.com
Mon Mar 26 17:32:28 BST 2018


Hi,

I am sorry if I am cross-posting from https://discuss.ocaml.org/t/ctypes-and-structs-containing-strings/1752 but the post there got no attention and I was told that it was a better idea to ask on the mailing-list.

In `ocaml-opasswd` (https://github.com/xapi-project/ocaml-opasswd)<https://github.com/xapi-project/ocaml-opasswd> we are using ctypes to bind some functions from `passwd.h` and `shadow.h`. In rare cases, when using the library to update an entry in the shadow file, it seems that the two strings in the passwd struct are read from uninitialised memory instead of being copies of the ocaml strings that we passed to create the new shadow struct.

The code is here: https://github.com/xapi-project/ocaml-opasswd/blob/master/lib/shadow.ml#L20 and my fear is that declaring those as `string`:
```
let sp_name     = field shadow_t "sp_name" string
let sp_passwd = field shadow_t "sp_passwd" string
```
and then reading or writing them with
```
name     = getf !@sp sp_name;
passwd = getf !@sp sp_passwd;
```
and
```
setf sp_t sp_name sp.name;
setf sp_t sp_passwd sp.passwd;
```
could be wrong. Reading the documentation, it seems that strings view are doing the right copies and allocation there but I don't see other major points of failure. This code has been there for ages, and if it is wrong I am not sure how it should be fixed. To add some more context, the part of the code that sometimes seems to behave badly is https://github.com/xapi-project/ocaml-opasswd/blob/master/lib/common.ml#L19-L21

Is the declaration clearly wrong? How should I declare the strings and properly copy them between c and ocaml when using ctypes, should they be char ptr and handled manually?

Thanks in advance for the help.

Best,
Marcello

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20180326/34516eba/attachment.html>


More information about the Ctypes mailing list