[PATCH] Fix Windows error handling for if_indextoname() and if_nametoindex()
"Simon Atanasyan via dev" <[email protected]> Fri, 29 May 2026 14:12:36 +0300
| Newsgroups | gmane.comp.apache.apr.devel |
|---|---|
| Message-ID | <CAK8m+TZ8Ej3PZJ-vEd-oSSjXdyXpUjMgM9AnUQJgvfnUJuEQtA@mail.gmail.com> |
--000000000000d4ac4d0652f2ecfa Content-Type: multipart/alternative; boundary="000000000000d4ac4c0652f2ecf8" --000000000000d4ac4c0652f2ecf8 Content-Type: text/plain; charset="UTF-8" Hi, On Windows, if_indextoname() and if_nametoindex() do not set errno on failure. The only indication of failure is their return value (NULL and 0, respectively). As a result, the value of errno after these calls is undefined and should not be used for error handling. The attached patch fixes this issue. When if_indextoname() returns NULL, apr_sockaddr_zone_get() now returns APR_EGENERAL instead of using errno. When if_nametoindex() returns 0, apr_sockaddr_zone_set() now always attempts to interpret the supplied zone identifier as a numeric value. Suggested commit message: [[[ Fix Windows error handling for if_indextoname() and if_nametoindex() On Windows, if_indextoname() and if_nametoindex() do not set errno on failure. Their return values are the only reliable indicators of success or failure, so errno must not be used after these calls. * network_io/unix/sockaddr.c (apr_sockaddr_zone_set): On Windows, do not check errno after if_nametoindex(). Always attempt to parse the zone identifier as a numeric value when if_nametoindex() fails. (apr_sockaddr_zone_get): On Windows, return APR_EGENERAL when if_indextoname() fails instead of returning the errno. Submitted by: Simon Atansyan <[email protected]> ]]] 1. https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-if_indextoname 2. https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-if_nametoindex -- Simon Atanasyan VisualSVN Software Limited --000000000000d4ac4c0652f2ecf8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div style=3D"font-family:arial,sans-serif" class=3D"gmail= _default">Hi,<br><br>On Windows, if_indextoname() and if_nametoindex() do n= ot set errno on failure. The only indication of failure is their return val= ue (NULL and 0, respectively). As a result, the value of errno after these = calls is undefined and should not be used for error handling.<br><br>The at= tached patch fixes this issue. When if_indextoname() returns NULL, apr_sock= addr_zone_get() now returns APR_EGENERAL instead of using errno. When if_na= metoindex() returns 0, apr_sockaddr_zone_set() now always attempts to inter= pret the supplied zone identifier as a numeric value.</div><div style=3D"fo= nt-family:arial,sans-serif" class=3D"gmail_default"><br></div><div><div sty= le=3D"font-family:arial,sans-serif" class=3D"gmail_default">Suggested commi= t message:</div><div style=3D"font-family:arial,sans-serif" class=3D"gmail_= default">[[[</div><div style=3D"font-family:arial,sans-serif" class=3D"gmai= l_default">Fix Windows error handling for if_indextoname() and if_nametoind= ex()<br><br>On Windows, if_indextoname() and if_nametoindex() do not set er= rno on failure. Their return values are the only reliable indicators of suc= cess or failure, so errno must not be used after these calls.<br><br>* netw= ork_io/unix/sockaddr.c<br>=C2=A0 (apr_sockaddr_zone_set): On Windows, do no= t check errno after if_nametoindex(). Always attempt to parse the zone iden= tifier as a numeric value when if_nametoindex() fails.<br>=C2=A0 (apr_socka= ddr_zone_get): On Windows, return APR_EGENERAL when if_indextoname() fails = instead of returning the errno.</div><div style=3D"font-family:arial,sans-s= erif" class=3D"gmail_default"><br>Submitted by: Simon Atansyan <<a href= =3D"mailto:[email protected]">[email protected]</a>></div><div style= =3D"font-family:arial,sans-serif" class=3D"gmail_default">]]]</div><br></di= v><div><div style=3D"font-family:arial,sans-serif" class=3D"gmail_default">= 1.=C2=A0<a href=3D"https://learn.microsoft.com/en-us/windows/win32/api/neti= oapi/nf-netioapi-if_indextoname">https://learn.microsoft.com/en-us/windows/= win32/api/netioapi/nf-netioapi-if_indextoname</a></div><div style=3D"font-f= amily:arial,sans-serif" class=3D"gmail_default">2.=C2=A0<a href=3D"https://= learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-if_nametoi= ndex">https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netio= api-if_nametoindex</a></div><br></div><span class=3D"gmail_signature_prefix= ">-- </span><br><div dir=3D"ltr" class=3D"gmail_signature" data-smartmail= =3D"gmail_signature">Simon Atanasyan<br>VisualSVN Software Limited</div></d= iv> --000000000000d4ac4c0652f2ecf8-- --000000000000d4ac4d0652f2ecfa Content-Type: text/plain; charset="US-ASCII"; name="indextoname-nametoindex-errno-win.patch.txt" Content-Disposition: attachment; filename="indextoname-nametoindex-errno-win.patch.txt" Content-Transfer-Encoding: base64 Content-ID: <f_mpqtnqa40> X-Attachment-Id: f_mpqtnqa40 SW5kZXg6IG5ldHdvcmtfaW8vdW5peC9zb2NrYWRkci5jDQo9PT09PT09PT09PT09PT09PT09PT09 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09DQotLS0gbmV0d29y a19pby91bml4L3NvY2thZGRyLmMJKHJldmlzaW9uIDE5MzQ3NDQpDQorKysgbmV0d29ya19pby91 bml4L3NvY2thZGRyLmMJKHdvcmtpbmcgY29weSkNCkBAIC0xMjM5LDEwICsxMjM5LDEyIEBADQog ICAgICAgICByZXR1cm4gQVBSX1NVQ0NFU1M7CiAgICAgfQogCisjaWYgIWRlZmluZWQoV0lOMzIp CiAgICAgaWYgKGVycm5vICE9IEVOT0RFVikgewogICAgICAgICByZXR1cm4gZXJybm87CiAgICAg fQotICAgIGVsc2UgeworI2VuZGlmCisgICAgewogICAgICAgICBjaGFyICplbmRwdHI7CiAgICAg ICAgIGFwcl9pbnQ2NF90IGkgPSBhcHJfc3RydG9pNjQoem9uZV9pZCwgJmVuZHB0ciwgMTApOwog CkBAIC0xMjcxLDcgKzEyNzMsMTEgQEANCiAgICAgaWYgKG5hbWUpIHsKICAgICAgICAgY2hhciAq YnVmID0gYXByX3BhbGxvYyhwLCBJRl9OQU1FU0laRSk7CiAgICAgICAgIGlmIChpZl9pbmRleHRv bmFtZShzYS0+c2Euc2luNi5zaW42X3Njb3BlX2lkLCBidWYpID09IE5VTEwpCisjaWYgZGVmaW5l ZChXSU4zMikKKyAgICAgICAgICAgIHJldHVybiBBUFJfRUdFTkVSQUw7CisjZWxzZQogICAgICAg ICAgICAgcmV0dXJuIGVycm5vOworI2VuZGlmCiAgICAgICAgICpuYW1lID0gYnVmOwogICAgIH0K IAo= --000000000000d4ac4d0652f2ecfa--