-
Notifications
You must be signed in to change notification settings - Fork 245
Description
AFAICT Merlin doesn't report errors correctly in its C code on Windows.
Errors of functions from the Win32 set (such as WriteFile, CreateProcess) can be retrieved with GetLastError and formatted using FormatMessage. Functions from the CRT (the "usual" libc, like _chdir) set the error code in errno. The error string can be retrieved with strerror, perror and the like.
The function failwith_perror, as currently defined, will work only with CRT functions:
merlin/src/frontend/ocamlmerlin/ocamlmerlin.c
Lines 81 to 86 in f1b877c
| static void failwith_perror(const char *msg) | |
| { | |
| perror(msg); | |
| dumpinfo(); | |
| exit(EXIT_FAILURE); | |
| } |
but is also called in the error path of Win32 functions:
merlin/src/frontend/ocamlmerlin/ocamlmerlin.c
Lines 118 to 125 in f1b877c
| static void ipc_send(HANDLE hPipe, unsigned char *buffer, size_t len, HANDLE fds[3]) | |
| { | |
| DWORD dwNumberOfBytesWritten; | |
| if (!WriteFile(hPipe, fds, 3 * sizeof(HANDLE), &dwNumberOfBytesWritten, NULL) || dwNumberOfBytesWritten != 3 * sizeof(HANDLE)) | |
| failwith_perror("sendmsg"); | |
| if (!WriteFile(hPipe, buffer, len, &dwNumberOfBytesWritten, NULL) || dwNumberOfBytesWritten != len) | |
| failwith_perror("send"); | |
| } |
I suppose it will show the last reported error of a CRT function in that case.
My understanding is that the Unix library works around this problem by mapping Win32 error codes to errno codes, with a fallback.
Taking inspiration from this, Merlin's failwith_perror should probably take the error number as a parameter and use strerror to retrieve the error string, or use functions from the caml_ library to report errors.