Skip to content

String_val returns const char * - #790

Closed
MisterDA wants to merge 1 commit into
ocsigen:masterfrom
MisterDA:string_val_const_char
Closed

MisterDA wants to merge 1 commit into
ocsigen:masterfrom
MisterDA:string_val_const_char

Conversation

@MisterDA

Copy link
Copy Markdown
Contributor

String_val(v) returns a pointer to the first byte of the string
v, with type char * or, when OCaml is configured with
-force-safe-string, with type const char *.

https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html#ss:c-block-access

Safe strings have been the default since OCaml 4.06.

https://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html

The function CreateProcess has two parameters that are non-const:

LPSTR  lpCommandLine
LPVOID lpEnvironment

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

Passing a const string triggers warnings. One can cast away the const
but that does not seem safe, or duplicate the string. Here, I’m
duplicating the string.

> `String_val(v)` returns a pointer to the first byte of the string
> `v`, with type `char *` or, when OCaml is configured with
> `-force-safe-string`, with type `const char *`.

https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html#ss:c-block-access

Safe strings have been the default since OCaml 4.06.

https://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html

The function CreateProcess has two parameters that are non-const:

    LPSTR  lpCommandLine
    LPVOID lpEnvironment

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

Passing a const string triggers warnings. One can cast away the const
but that does not seem safe, or duplicate the string. Here, I’m
duplicating the string.
@aantron

aantron commented Aug 2, 2020

Copy link
Copy Markdown
Collaborator

MSDN has:

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

Given the typical representation of strings in OCaml, do we ever end up in an environment where CreateProcess is CreateProcessW? I guess this is a question for anyone watching the repo. @dra27?

MSDN is silent on whether either CreateProcess function can write into lpEnvironment.

@dra27

dra27 commented Aug 2, 2020

Copy link
Copy Markdown
Contributor

Note that since 4.06, OCaml itself always uses Unicode versions of functions. It's not related here, but unless you use Windows '95/'98/ME, the ANSI versions of functions are stubs which convert their arguments to Unicode and call the "wide" version. As it happens, that's the explanation for why this only affects CreateProcessW (see blog post on this here).

Why not upgrade the LWT Unix stubs to use Unicode on Windows? That way you'd be copying anyway because you'll need to use caml_stat_strdup_to_os on both strings (cf. https://github.com/ocaml/ocaml/blob/trunk/otherlibs/win32unix/createprocess.c#L100)

@aantron

aantron commented Aug 3, 2020

Copy link
Copy Markdown
Collaborator

Thanks @dra27.

I agree. We should aim to change Lwt to use the -W functions on Windows, as you suggest, in the long term (if this isn't superseded by libuv conversion or some other such work).

For right now, I think the best thing to do is cast, and add a comment that this is only valid because we are calling CreateProcessA, perhaps with links to this PR, the blog post, and/or the MSDN entry. Or we can change this specific code to call CreateProcessW, as in the code @dra27 linked.

@aantron aantron closed this in c656870 Aug 22, 2020
@MisterDA
MisterDA deleted the string_val_const_char branch October 4, 2021 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants