Skip to content

Add caml_stat_char_array_{to,of}_os - #13497

Merged
gasche merged 3 commits into
ocaml:trunkfrom
dra27:caml_stat_strndup
Jan 22, 2025
Merged

gasche merged 3 commits into
ocaml:trunkfrom
dra27:caml_stat_strndup

Conversation

@dra27

@dra27 dra27 commented Sep 29, 2024 •

Copy link
Copy Markdown
Member

Two related updates in the UTF-16 conversion functions:

  1. Prompted by recent work on Fixed resource leaks in runtime events system #13419, I noticed that the semantics of caml_stat_strdup_to_utf16 are incorrect - it was using caml_stat_alloc_noexc. That's corrected, and having audited the places where caml_stat_strdup_to_utf16 / caml_stat_strdup_to_os were used, I've also added caml_stat_strdup_noexc_to_os (caml_stat_strdup_noexc_of_os was added in Fixed resource leaks in runtime events system #13419) and used it when parsing ld.conf, if somewhat pedantically as that code is executed early enough that it's written on the assumption that allocation can't fail.
  2. Added caml_stat_char_array_{to,of}_os functions, as outlined in Add caml_stat_strndup_{to,of}_utf16 to win32 functions #11449. I need caml_stat_char_array_to_os for a relocatable compiler PR to be able to encode strings containing embedded NUL characters in UTF-16. It obviously makes sense to add caml_stat_char_array_of_os as its dual, and external use-cases for that function are outlined in the issue (reading Registry data, etc.). In the issue, I noted that the _os versions don't necessarily make sense, but that's not quite right - caml_stat_char_array_to_os is clearly useful, and so it makes sense to have caml_stat_char_array_of_os, even though it's likely only ever to be used in its Windows caml_stat_char_array_of_utf16 variant.

@dra27 dra27 linked an issue Sep 29, 2024 that may be closed by this pull request
Comment thread runtime/caml/osdeps.h Resolved
@dra27
dra27 force-pushed the caml_stat_strndup branch 2 times, most recently from 0d72493 to 4d2dd9d Compare September 30, 2024 13:47
Comment thread runtime/caml/osdeps.h Resolved
Comment thread runtime/caml/memory.h Resolved
Comment thread runtime/caml/osdeps.h Resolved
Comment thread runtime/dynlink.c Resolved
Comment thread runtime/memory.c Outdated
CAMLexport caml_stat_string caml_stat_strndup(const char *s,
asize_t len, asize_t *out_len)
{
caml_stat_block result = caml_stat_alloc_noexc(len);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not correct. First, you need to clamp at max(len, strlen(s)), which in other words is strnlen(s, len). Then you need to add one byte for the NUL terminator, and explicitly write at after the memcpy call.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use strlen - the point here is that the "string" may both contain embedded NUL chars and may even not be NUL-terminated? I believe the code is what is needed (as the dual of the Windows side), but therefore the function name and description may need clarifying/changing (cf. 2nd para of #11449).

Comment thread runtime/win32.c Resolved
Comment thread runtime/win32.c Resolved
@NickBarnes

Copy link
Copy Markdown
Contributor

A couple of points:

  1. These names are too confusing: we really must avoid using str if we're not dealing with nul-terminated C strings. These functions are transcoding between arrays of char and arrays of char_os. So maybe caml_stat_char_array_of_os and caml_stat_char_array_to_os ? (lose the dup bit altogether)?
  2. The header comments are not completely clear on a couple of points:
  • how are the char arrays interpreted? My understanding is: "always UTF-8 or current Windows code page", but this should be written down in the comment for each function;
  • what are the units for len and *out_len, for the char_os arrays? Code points? char_os units? bytes? Again, this should be documented for each function. I guess the same question applies to the char arrays (are we counting chars or code points) but I assume chars.
    Also I think there may be a couple of points at which the relevant comment is for the wrong function.

@NickBarnes NickBarnes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment in the main conversation. This is broadly OK but the function names are intolerable and some of the header comments are either insufficient or slightly misleading.

Comment thread runtime/caml/osdeps.h Outdated
CAMLextern char* caml_stat_strdup_of_utf16(const wchar_t *s);

/* [caml_stat_strndup_to_utf16(s, n, &out_n)] returns a copy of the first n
bytes of s re-encoded in UTF-16. If out_n is not NULL, then the size of the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should explain how the bytes of s are interpreted (ASCII? UTF-8? Current Windows code page?). See also the comment for caml_stat_strdup_to_utf16.
Also: are the units of out_n bytes or (16-bit) code units?

@dra27
dra27 force-pushed the caml_stat_strndup branch from 4d2dd9d to e40e2a8 Compare October 21, 2024 16:50
@dra27

dra27 commented Oct 21, 2024

Copy link
Copy Markdown
Member Author

caml_stat_char_array_{to,of}_os sounds good to me, thanks! As part of de-stringing it, I've gone with "size" instead of "len" (intentionally keeping slen to correspond to the very unusual signed argument which gets passed to the Windows API functions).

@dra27 dra27 changed the title Add caml_stat_strndup_{to,of}_os Add caml_stat_char_array_{to,of}_os Oct 21, 2024
Comment thread runtime/memory.c Resolved
Comment thread runtime/win32.c Resolved
@dra27
dra27 force-pushed the caml_stat_strndup branch 2 times, most recently from ede1303 to ca2f53f Compare October 22, 2024 10:28
Comment thread runtime/memory.c
{
CAMLassert(size > 0);
caml_stat_block result = caml_stat_alloc(size);
memcpy(result, s, size);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes s is at least size bytes long. How do you plan to guarantee this? Do you intend for all callers of caml_stat_char_array_{of,to}_os to enforce this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - that’s the whole point of this function, processing buffers where the length is specified not computed.

@dra27
dra27 force-pushed the caml_stat_strndup branch from ca2f53f to ddf4056 Compare October 23, 2024 18:20
The Windows implementation caml_stat_strdup_to_utf16 returned NULL on
OOM, where caml_stat_strdup raises Out_of_memory. Windows implementation
fixed to raise Out_of_memory.

caml_stat_strdup_noexc_to_os to be used in the one place in the runtime
where NULL should in fact be returned.
@dra27
dra27 force-pushed the caml_stat_strndup branch from ddf4056 to 1a6647f Compare January 8, 2025 13:57
@dra27

dra27 commented Jan 9, 2025

Copy link
Copy Markdown
Member Author

Gentle ping to @NickBarnes - this PR will shortly be blocking a couple more 😊

@dra27

dra27 commented Jan 10, 2025

Copy link
Copy Markdown
Member Author

Additional annotations from @MisterDA pushed, thanks!

@dra27
dra27 force-pushed the caml_stat_strndup branch from 25135f1 to b274133 Compare January 21, 2025 18:14
Comment thread runtime/caml/osdeps.h
[Sys_error] if [s] is not valid UTF-8.
*/
CAMLmalloc(caml_stat_free, 1, 2) CAMLreturns_nonnull()
CAMLextern wchar_t *caml_stat_char_array_to_utf16(const char *s, size_t size,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comments do not explicitly mention that those functions are meant for char* data that may contain NUL characters. I think that it would be nice to point that out for people who may wonder when to use caml_stat_char_array_to_os instead of caml_stat_strdup_to_os.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - I pushed an updated version

dra27 and others added 2 commits January 21, 2025 23:09
Allows conversion of arbitrary char/wchar_t sequences by allowing the
number of characters copied to be specified. The output length of the
encoding can also be returned by these functions.
@dra27
dra27 force-pushed the caml_stat_strndup branch from b274133 to 6612bfe Compare January 21, 2025 23:09

@gasche gasche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved on behalf of @MisterDA and @dustanddreams. I believe that the changes requested by @NickBarnes have all been implemented.

@gasche
gasche merged commit be96495 into ocaml:trunk Jan 22, 2025
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.

Add caml_stat_strndup_{to,of}_utf16 to win32 functions

4 participants