Skip to content

Commit a998ddf

Browse files
authored
flambda-backend: Use upstream implementation of caml_unix_error_message for runtime5 (#2111)
1 parent 6fafc58 commit a998ddf

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

otherlibs/unix/errmsg_unix.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
#include <caml/sys.h>
2222
#include "unixsupport.h"
2323

24+
#ifdef CAML_RUNTIME_5
25+
26+
CAMLprim value caml_unix_error_message(value err)
27+
{
28+
char buf[1024];
29+
int errnum = caml_unix_code_of_unix_error(err);
30+
return caml_copy_string(caml_strerror(errnum, buf, sizeof(buf)));
31+
}
32+
33+
#else
34+
2435
CAMLprim value caml_unix_error_message(value err)
2536
{
2637
char buf[1024];
@@ -32,3 +43,5 @@ CAMLprim value caml_unix_error_message(value err)
3243
*/
3344
buf);
3445
}
46+
47+
#endif

runtime4/caml/sys.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
extern "C" {
2525
#endif
2626

27+
CAMLextern char * caml_strerror(int errnum, char * buf, size_t buflen);
28+
2729
#define NO_ARG Val_int(0)
2830

2931
CAMLnoreturn_start

runtime4/sys.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@
6161
#include "caml/callback.h"
6262
#include "caml/startup_aux.h"
6363

64+
CAMLexport char * caml_strerror(int errnum, char * buf, size_t buflen)
65+
{
66+
#ifdef _WIN32
67+
/* Windows has a thread-safe strerror */
68+
return strerror(errnum);
69+
#else
70+
int res = strerror_r(errnum, buf, buflen);
71+
/* glibc<2.13 returns -1/sets errno, >2.13 returns +ve errno.
72+
We assume that buffer size is large enough not to get ERANGE,
73+
so we assume we got EINVAL. */
74+
if (res != 0) {
75+
snprintf(buf, buflen, "Unknown error %d", errnum);
76+
}
77+
return buf;
78+
#endif
79+
}
80+
6481
static char * error_message(void)
6582
{
6683
return strerror(errno);

0 commit comments

Comments
 (0)