<!DOCTYPE html>
<!-- BaNnErBlUrFlE-BoDy-start -->
<!-- Preheader Text : BEGIN -->
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;opacity:0;overflow:hidden;">
I've encountered a problem with errno handling which I think is a bug. For some standard C functions, there is no way to detect whether an error has occurred, other than reading `errno` after the call is complete. However, these functions do</div>
<!-- Preheader Text : END -->

<!-- Email Banner : BEGIN -->
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;opacity:0;overflow:hidden;">ZjQcmQRYFpfptBannerStart</div>

<!--[if ((ie)|(mso))]>
  <table border="0" cellspacing="0" cellpadding="0" width="100%" style="padding: 0px 0px 10px 0px; direction: ltr" lang="en"><tr><td>
    <table border="0" cellspacing="0" cellpadding="0" style="padding: 0px 8px 6px 8px; width: 100%; border-radius:4px; border-top:4px solid #8193a0;background-color:#60beeb;"><tr><td valign="top">
      <table align="left" border="0" cellspacing="0" cellpadding="0" style="padding: 0px 8px 4px 8px; font-size: 12px; line-height: 16px">
        <tr><td style="color:#000000; font-family: 'Arial', sans-serif; font-weight:bold; font-size:14px; line-height: 20px; direction: ltr">
          This Message Is From an External Sender
        </td></tr>
        <tr><td style="color:#000000; font-weight:normal; font-family: 'Arial', sans-serif; font-size:12px; direction: ltr">
          This message came from outside your organization.
        </td></tr>

      </table>
      <![if ie]><br clear="all"><![endif]>
      <table align="right" border="0" cellspacing="0" cellpadding="0" style="padding: 0px 0px 4px 0px; font-size: 14px; line-height: 36px"><tr>
        <td style="direction: ltr">  <a target="_blank" href="https://us-phishalarm-ewt.proofpoint.com/EWT/v1/ACWV5N9M2RV99hQ!Op20OCbEMWKmWRormTOV0F3vlRSWbst377z90YIWc6uBlf-Muh9juQY-52LlpyQnTaIDk0ftTvG3WFm1JrxBqrFcqjsRV6Y59hWE5m3s-trHCzcC0gQrHDwPH9t3R2azVexz$" style="mso-padding-alt: 7px; padding: 7px; border-radius: 2px; border: 1px solid #666666; "><strong style="font-weight: normal; color: #000000; text-decoration: none; font-family: 'Arial', sans-serif; font-size: 14px;">  Report Suspicious  </strong></a>  ‌ </td>
      </tr></table>
    </td></tr></table>
  </td></tr></table>
<![endif]-->

<![if !((ie)|(mso))]>
  <div dir="ltr" lang="en" id="pfptBannerbqkotcz" style="all: revert !important; display:block !important; text-align: left !important; margin: 0 0 10px 0 !important; padding:7px 16px 8px 16px !important; border-radius: 4px !important; min-width: 200px !important; background-color: #60beeb !important; background-color: #60beeb; border-top: 4px solid #8193a0 !important; border-top: 4px solid #8193a0;">
    <div id="pfptBannerbqkotcz" style="all: unset !important; float:left !important; display:block !important; margin: 1px 0 1px 0 !important; max-width: 600px !important;">
      <div id="pfptBannerbqkotcz" style="all: unset !important; display:block !important; visibility: visible !important; background-color: #60beeb !important; color:#000000 !important; color:#000000; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-weight:bold !important; font-weight:bold; font-size:14px !important; line-height:1.29 !important; line-height:1.29">
        This Message Is From an External Sender
      </div>
      <div id="pfptBannerbqkotcz" style="all: unset !important; display:block !important; visibility: visible !important; background-color: #60beeb !important; color:#000000 !important; color:#000000; font-weight:normal; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-size:12px !important; line-height:1.5 !important; line-height:1.5; margin-top:2px !important;">
This message came from outside your organization.
      </div>

    </div>
    <div id="pfptBannerbqkotcz" style="all: unset !important; float: right !important; display: block !important; display: block; margin-left: 16px !important; margin-top: 1px !important; text-align: right !important; width: fit-content !important; font-size: 12px !important">
<a id="pfptBannerbqkotcz" href="https://us-phishalarm-ewt.proofpoint.com/EWT/v1/ACWV5N9M2RV99hQ!Op20OCbEMWKmWRormTOV0F3vlRSWbst377z90YIWc6uBlf-Muh9juQY-52LlpyQnTaIDk0ftTvG3WFm1JrxBqrFcqjsRV6Y59hWE5m3s-trHCzcC0gQrHDwPH9t3R2azVexz$"
    style="all: unset !important; display: inline-block !important; text-decoration: none">
    <div class="pfptPrimaryButtonbqkotcz" style="display: inline-block !important; display: inline-block; visibility: visible !important; opacity: 1 !important; color: #000000 !important; color: #000000; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-size: 14px !important;  font-weight: normal !important; text-decoration: none !important; border-radius: 2px !important; margin-top: 3px !important; margin-bottom: 3px !important; margin-left: 16px !important; padding: 7.5px 16px !important; white-space: nowrap !important; width: fit-content !important;
        border: 1px solid #666666">
        Report Suspicious
    </div>
</a>
    </div>
    <div style="clear: both !important; display: block !important; visibility: hidden !important; line-height: 0 !important; font-size: 0.01px !important; height: 0px"> </div>
  </div>
<![endif]>

<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;opacity:0;overflow:hidden;">ZjQcmQRYFpfptBannerEnd</div>
<!-- Email Banner : END -->

<!-- BaNnErBlUrFlE-BoDy-end -->
<html>
<head><!-- BaNnErBlUrFlE-HeAdEr-start -->
<style>
  #pfptBannerbqkotcz { all: revert !important; display: block !important;
    visibility: visible !important; opacity: 1 !important;
    background-color: #60beeb !important;
    max-width: none !important; max-height: none !important }
  .pfptPrimaryButtonbqkotcz:hover, .pfptPrimaryButtonbqkotcz:focus {
    background-color: #77a8c4 !important; }
  .pfptPrimaryButtonbqkotcz:active {
    background-color: #8193a0 !important; }
  html:root, html:root>body { all: revert !important; display: block !important;
    visibility: visible !important; opacity: 1 !important; }
</style>

<!-- BaNnErBlUrFlE-HeAdEr-end -->
</head><body><pre style="font-family: sans-serif; font-size: 100%; white-space: pre-wrap; word-wrap: break-word">I've encountered a problem with errno handling which I think is a bug.

For some standard C functions, there is no way to detect whether an
error has occurred, other than reading `errno` after the call is
complete. However, these functions do *not* set `errno` to zero on
success (and in fact are forbidden from doing so by specification).
One example which is easy to test with is `strtol`. I used this
function because it doesn't have any external side-effects, which
makes it ideal for testing.

At first I assumed it would be sufficient to initialize my call state
capture buffer to zero and read the `errno` value after the function
call (as stated in the documentation); however, this does not pre-set
`errno` itself, so if the function does not set `errno`, then the
stale `errno` value is populated back into the call state capture
buffer.

This problem manifested for me as `strtol` returning an errno value of
ENOENT on success, but only when running my test from the framework
which does some filesystem stuff before calling my test (thus leaving
this stale value in errno).

Here's a relevant quote from a recent publicly available draft C
standard (the final language is essentially unchanged):

> The value of `errno` in the initial thread is zero at program startup (the initial representation of the object designated by `errno` in other threads is indeterminate), but is never set to zero by any library function.[237]) The value of `errno` may be set to nonzero by a library function call whether or not there is an error, provided the use of `errno` is not documented in the description of the function in this document.
> [237] Thus, a program that uses `errno` for error checking would set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value of `errno` on entry and then set it to zero, as long as the original value is restored if `errno`’s value is still zero just before the return.

I believe that one of the following solutions could be considered:

1. Always populate `errno` from the call state buffer before the call,
if the `errno` call state is captured
2. Clear `errno` before any call which captures it
3. Create a new linker option which does one of the above (this has
the disadvantage of treating this as an enhancement rather than a fix,
leaving users of previous JDKs out in the cold)
4. As a more radical solution, reconsider adding permanent fields to
`java.lang.Thread` for this purpose to replace the captured call state
buffer, allowing more direct access to `errno` and its
Windows-specific brethren

As a workaround, you could conceivably bind the (platform-specific)
function or symbol which returns the pointer to the "real" errno and
push a zero value into the resultant memory segment. I did confirm
that this "works". However, this could potentially be disastrous in
the face of virtual threads, which could cause the OS thread to change
in the time between when the errno value is cleared and the actual
downcall is executed, or in the case where the JVM performs some work
at a safepoint that may indirectly cause `errno` to be reset.

Since this is a problem that users cannot feasibly solve on their own,
which prevents interaction with functions that use `errno` in a
standard way, I consider that this is a bug in the current `errno`
support.

Thoughts?

-- 
- DML • he/him

</pre></body></html>