RFR: 8338851: Hoist os::Posix::realpath() to os::realpath() and implement on Windows [v11]

David Holmes dholmes at openjdk.org
Fri Sep 20 02:46:39 UTC 2024


On Thu, 19 Sep 2024 07:21:17 GMT, Simon Tooke <stooke at openjdk.org> wrote:

>> test/hotspot/gtest/runtime/test_os.cpp line 433:
>> 
>>> 431:   errno = 0;
>>> 432:   returnedBuffer = os::realpath(tmppath, buffer, MAX_PATH);
>>> 433:   EXPECT_TRUE(returnedBuffer == buffer);
>> 
>> Should we also do `EXPECT_TRUE(errno == 0);` ? Here and below.
>
> This is interesting!  I found that on Linux, errno _was not zero_!  The specifications for POSIX realpath say
> `RETURN VALUE
> Upon successful completion, realpath() shall return a pointer to the resolved name. Otherwise, realpath() shall return a null pointer and set errno to indicate the error, and the contents of the buffer pointed to by resolved_name are undefined.`
> Nowhere does it say errno is unchanged if successful.
> 
> 
>   errno = 0;
>   ::printf("before ::realpath("/tmp",nullptr) errno=%d\n", errno);
>   char* p = ::realpath("/tmp", nullptr);
>   ::printf("after ::realpath p=%s errno=%d\n", p, errno);
> 
> 
> outputs:
> 
> before ::realpath("/tmp",nullptr) errno=0
> after ::realpath /tmp p=/tmp errno=22
> 
> With behaviour like this, one can see why OpenJDK wraps ::realpath()...
> 
> Compiler used: g++ (GCC) 14.2.1 20240801

Right I forgot about this. The Posix spec even states:
>  No function in this volume of POSIX.1-2017 shall set errno to 0.

so reading errno is only valid after calling a function that sets errno on error, and which returns a value that says there was an error. Windows GetLastError is the same. Even setting it to zero before the call does not guarantee the call doesn't modify it even if successful.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/20683#discussion_r1767843602


More information about the serviceability-dev mailing list