Skip to content

Commit

Permalink
flambda-backend: Use upstream implementation of caml_unix_error_messa…
Browse files Browse the repository at this point in the history
…ge for runtime5 (#2111)
  • Loading branch information
mshinwell authored Dec 4, 2023
1 parent 6fafc58 commit a998ddf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 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 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 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

0 comments on commit a998ddf

Please sign in to comment.