MIME-Version: 1.0 References: <20220218124149.149-1-lstipakov@...277...> In-Reply-To: From: Lev Stipakov Date: Tue, 22 Feb 2022 11:30:28 +0200 Message-ID: Content-Type: multipart/alternative; boundary="0000000000009d35be05d89800d6" Subject: Re: [Openvpn-devel] [PATCH master+release/2.5] error.c: use correct API to get error description on Windows List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Selva Nair Cc: openvpn-devel , Lev Stipakov --0000000000009d35be05d89800d6 Content-Type: text/plain; charset="UTF-8" Hi, > While this would be a cleaner fix, it also requires extensive changes and > it is not always easy to decide where to use M_ERRNO and where to use > M_WINERR. E.g., without looking into the internals of platform.c one > doesn't know whether platform_open() uses _wopen() or CreateFile(). > True. > A possible option is to continue the use of M_ERRNO on WIndows as is > (except for socket errors), continue to use GetLastError() in x_msg(), but > if/when the latter if it returns zero, try errno and strerrror(). Not ideal > but less changes and easier and transparent to the user of msg(). That > said, I haven't checked whether GetLastError() returns zero or a valid and > correct error code for C runtime errors -- without which this approach wont > work. > I did some tests, ::GetLastError appears to return zero or correct error code: sqrt(-1); int err = errno; // -33 EDOM DWORD lasterr = GetLastError(); // 0 _open("c:\\Temp\\notfound", _O_RDONLY); err = errno; // 2 ENOENT lasterr = GetLastError(); // 2 ERROR_FILE_NOT_FOUND _open("c:\\Temp\\directory", _O_RDONLY); err = errno; // 13 EACCES lasterr = GetLastError(); // 5 ERROR_ACCESS_DENIED malloc(10000000000); err = errno; // 12 ENOMEM lasterr = GetLastError(); // 8 ERROR_NOT_ENOUGH_MEMORY So let's go with this approach. > BTW, there are a couple of uses of strerror() in check_status() (error.c) > missed by the original patch. > Thanks, I will address it in this "proper" fix. -- -Lev --0000000000009d35be05d89800d6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,
=C2=A0
=
While this would be a cleaner fix, it also requires= extensive changes and it is not always easy to decide where to use M_ERRNO= and where to use M_WINERR. E.g., without looking into the internals of pla= tform.c=C2=A0one doesn't know whether platform_open() uses _wopen() or = CreateFile().

True.
=
=C2=A0
A possible option is to continue t= he use of M_ERRNO on WIndows as is (except for socket errors), continue to = use GetLastError() in x_msg(), but if/when the latter if it returns zero, t= ry errno and strerrror(). Not ideal but less changes and easier and transpa= rent to the user of msg(). That said, I haven't=C2=A0checked whether Ge= tLastError() returns zero or a valid and correct error code for C runtime e= rrors -- without which this approach wont work.

I did some tests, ::GetLastError appears to return= =C2=A0zero or correct error code:

=C2=A0 =C2=A0 sq= rt(-1);
=C2=A0 =C2=A0 int err =3D errno; // -33 EDOM
=C2=A0 =C2=A0 DW= ORD lasterr =3D GetLastError(); // 0

=C2=A0 =C2=A0 _open("c:\\T= emp\\notfound", _O_RDONLY);
=C2=A0 =C2=A0 err =3D errno; // 2 ENOEN= T
=C2=A0 =C2=A0 lasterr =3D GetLastError(); // 2 ERROR_FILE_NOT_FOUND
=C2=A0 =C2=A0 _open("c:\\Temp\\directory", _O_RDONLY);
= =C2=A0 =C2=A0 err =3D errno; // 13 EACCES
=C2=A0 =C2=A0 lasterr =3D GetL= astError(); // 5 ERROR_ACCESS_DENIED

=C2=A0 =C2=A0 malloc(1000000000= 0);
=C2=A0 =C2=A0 err =3D errno; // 12 ENOMEM
=C2=A0 =C2=A0 lasterr = =3D GetLastError(); // 8 ERROR_NOT_ENOUGH_MEMORY

S= o let's go with this approach.
=C2=A0
BTW, there are a couple of uses of strerror() in check_status() (err= or.c) missed by the original patch.

=
Thanks, I will address it in this "proper" fix.
<= /div>

--
-Lev<= /div>
--0000000000009d35be05d89800d6--