Skip to content

Use upstream implementation of caml_unix_error_message for runtime5 #2111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ocaml/otherlibs/unix/errmsg_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
#include <caml/sys.h>
#include "unixsupport.h"

#ifdef CAML_RUNTIME_5

CAMLprim value caml_unix_error_message(value err)
{
char buf[1024];
int errnum = caml_unix_code_of_unix_error(err);
return caml_copy_string(caml_strerror(errnum, buf, sizeof(buf)));
}

#else

CAMLprim value caml_unix_error_message(value err)
{
char buf[1024];
Expand All @@ -32,3 +43,5 @@ CAMLprim value caml_unix_error_message(value err)
*/
buf);
}

#endif
2 changes: 2 additions & 0 deletions ocaml/runtime4/caml/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
extern "C" {
#endif

CAMLextern char * caml_strerror(int errnum, char * buf, size_t buflen);

#define NO_ARG Val_int(0)

CAMLnoreturn_start
Expand Down
17 changes: 17 additions & 0 deletions ocaml/runtime4/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@
#include "caml/callback.h"
#include "caml/startup_aux.h"

CAMLexport char * caml_strerror(int errnum, char * buf, size_t buflen)
{
#ifdef _WIN32
/* Windows has a thread-safe strerror */
return strerror(errnum);
#else
int res = strerror_r(errnum, buf, buflen);
/* glibc<2.13 returns -1/sets errno, >2.13 returns +ve errno.
We assume that buffer size is large enough not to get ERANGE,
so we assume we got EINVAL. */
if (res != 0) {
snprintf(buf, buflen, "Unknown error %d", errnum);
}
return buf;
#endif
}

static char * error_message(void)
{
return strerror(errno);
Expand Down