Skip to content

Wrong error reporting on Windows #1570

@MisterDA

Description

@MisterDA

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:

static void failwith_perror(const char *msg)
{
perror(msg);
dumpinfo();
exit(EXIT_FAILURE);
}

but is also called in the error path of Win32 functions:
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.

https://github.com/ocaml/ocaml/blob/5608830692984aff7eebba7c1ed42d0f6d2c15b8/otherlibs/unix/errmsg_win32.c#L26-L45

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions