Repository navigation
Add caml_stat_char_array_{to,of}_os - #13497
Conversation
eec3a74 to
ad277f2
Compare
0d72493 to
4d2dd9d
Compare
| 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
A couple of points:
|
NickBarnes
left a comment
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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?
4d2dd9d to
e40e2a8
Compare
|
|
caml_stat_strndup_{to,of}_oscaml_stat_char_array_{to,of}_os
ede1303 to
ca2f53f
Compare
| { | ||
| CAMLassert(size > 0); | ||
| caml_stat_block result = caml_stat_alloc(size); | ||
| memcpy(result, s, size); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes - that’s the whole point of this function, processing buffers where the length is specified not computed.
ca2f53f to
ddf4056
Compare
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.
ddf4056 to
1a6647f
Compare
|
Gentle ping to @NickBarnes - this PR will shortly be blocking a couple more 😊 |
|
Additional annotations from @MisterDA pushed, thanks! |
25135f1 to
b274133
Compare
| [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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed - I pushed an updated version
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.
b274133 to
6612bfe
Compare
gasche
left a comment
There was a problem hiding this comment.
Approved on behalf of @MisterDA and @dustanddreams. I believe that the changes requested by @NickBarnes have all been implemented.
Two related updates in the UTF-16 conversion functions:
caml_stat_strdup_to_utf16are incorrect - it was usingcaml_stat_alloc_noexc. That's corrected, and having audited the places wherecaml_stat_strdup_to_utf16/caml_stat_strdup_to_oswere used, I've also addedcaml_stat_strdup_noexc_to_os(caml_stat_strdup_noexc_of_oswas added in Fixed resource leaks in runtime events system #13419) and used it when parsingld.conf, if somewhat pedantically as that code is executed early enough that it's written on the assumption that allocation can't fail.caml_stat_char_array_{to,of}_osfunctions, as outlined in Addcaml_stat_strndup_{to,of}_utf16to win32 functions #11449. I needcaml_stat_char_array_to_osfor a relocatable compiler PR to be able to encode strings containing embedded NUL characters in UTF-16. It obviously makes sense to addcaml_stat_char_array_of_osas 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_osis clearly useful, and so it makes sense to havecaml_stat_char_array_of_os, even though it's likely only ever to be used in its Windowscaml_stat_char_array_of_utf16variant.