On Thu, 5 Nov 2026 16:10:03 GMT, Ioi Lam <[email protected]> wrote:

>> Btw. we do the buffer enlargement attempt also here  
>> https://github.com/openjdk/jdk/blob/8073b51f4827b66ef056952a5f0a152aee09a92e/src/java.base/windows/native/libjava/WinNTFileSystem_md.c#L91
>
> Thanks for the explanation. It makes sense.
> 
> I find it hard to see what cause the error condition. Maybe it's better to 
> change the comment to
> 
> 
> } else {
>   // Either of the above GetFinalPathNameByHandleW() calls has failed
>   ...

Here's an alternative that avoids the else branch. This should handle the 
potential for the final path length to change between the two calls.

-        if (len >= size) {
-            if ((finalPath = (WCHAR*)malloc(len * sizeof(WCHAR))) == NULL)
+        if (len != 0 && len >= size) {
+            DWORD pathBufferSize = len;
+            finalPath = (WCHAR*)malloc((size_t)pathBufferSize * sizeof(WCHAR));
+            if (finalPath == NULL) {
+                CloseHandle(h);
                 return NULL;
-            len = GetFinalPathNameByHandleW(h, finalPath, len, 0);
+            }
+            len = GetFinalPathNameByHandleW(h, finalPath, pathBufferSize, 0);
+            if (len == 0 || len >= pathBufferSize) {
+                CloseHandle(h);
+                free(finalPath);
+                return NULL;
+            }

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

PR Review Comment: https://git.openjdk.org/jdk/pull/33752#discussion_r4484699084

Reply via email to