merlin icon indicating copy to clipboard operation
merlin copied to clipboard

Wrong error reporting on Windows

Open MisterDA opened this issue 2 years ago • 0 comments

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: https://github.com/ocaml/merlin/blob/f1b877cd20d5c99b24bb8d051a63088488d26846/src/frontend/ocamlmerlin/ocamlmerlin.c#L81-L86 but is also called in the error path of Win32 functions: https://github.com/ocaml/merlin/blob/f1b877cd20d5c99b24bb8d051a63088488d26846/src/frontend/ocamlmerlin/ocamlmerlin.c#L118-L125 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.

MisterDA avatar Feb 21 '23 11:02 MisterDA