This is an archive of the discontinued LLVM Phabricator instance.

[Support][Windows] Fix incorrect GetFinalPathNameByHandleW() return value check in realPathFromHandle()
ClosedPublic

Authored by ArcsinX on Aug 25 2020, 11:52 AM.

Details

Summary

GetFinalPathNameByHandleW(,,N,) returns:

  • < N on success (this value does not include the size of the terminating null character)
  • >= N if buffer is too small (this value includes the size of the terminating null character)

So, when N == Buffer.capacity() - 1, we need to resize buffer if return value is > Buffer.capacity() - 2.
Also, we can set N to Buffer.capacity().

Thus, without this patch realPathFromHandle() returns unfilled buffer when length of the final path of the file is equal to Buffer.capacity() or Buffer.capacity() - 1.

Diff Detail

Event Timeline

ArcsinX created this revision.Aug 25 2020, 11:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2020, 11:52 AM
ArcsinX requested review of this revision.Aug 25 2020, 11:52 AM
andrewng added inline comments.Aug 26 2020, 2:34 AM
llvm/lib/Support/Windows/Path.inc
355

If Buffer.capacity() == 0 this check will fail. This case would have also caused issues with the previous code.

ArcsinX updated this revision to Diff 287894.Aug 26 2020, 3:06 AM

Handle Buffer.capacity() == 0 case correcly.

ArcsinX added inline comments.Aug 26 2020, 3:07 AM
llvm/lib/Support/Windows/Path.inc
355

Thanks. Fixed.

andrewng added inline comments.Aug 26 2020, 6:01 AM
llvm/lib/Support/Windows/Path.inc
355

Perhaps this would be better still?

ArcsinX updated this revision to Diff 287948.Aug 26 2020, 6:09 AM

Check GetFinalPathNameByHandleW() return value for zero first

ArcsinX updated this revision to Diff 287953.Aug 26 2020, 6:18 AM

Check GetFinalPathNameByHandleW() return value for non-zero first

ArcsinX added inline comments.Aug 26 2020, 6:19 AM
llvm/lib/Support/Windows/Path.inc
355

Yes, I think it's reasonable. Thanks, fixed.

andrewng added inline comments.Aug 26 2020, 6:20 AM
llvm/lib/Support/Windows/Path.inc
355–356

I did consider this change too, but then I decided not to assume that the call to GetFinalPathNameByHandleW below is going to succeed.

ArcsinX added inline comments.Aug 26 2020, 6:40 AM
llvm/lib/Support/Windows/Path.inc
355–356

Yes, you are right.
Every of GetFinalPathNameByHandleW() calls could return 0 in case of failure, so we need to check CountChars for 0 for both calls.
So, I have reverted that change.

andrewng accepted this revision.Aug 26 2020, 7:21 AM

LGTM with that last one change.

llvm/lib/Support/Windows/Path.inc
360

Sorry, just noticed that this invocation uses Buffer.data() whereas the call above uses Buffer.begin(). Would be good to make it the same. Looks like begin() is the preferred style in this file.

This revision is now accepted and ready to land.Aug 26 2020, 7:21 AM
ArcsinX updated this revision to Diff 287972.Aug 26 2020, 7:44 AM

Buffer.data() => Buffer.begin()

Thank you for review.

llvm/lib/Support/Windows/Path.inc
360

Thanks, fixed.

amccarth requested changes to this revision.Aug 26 2020, 8:57 AM
amccarth added a subscriber: amccarth.

I know it's not part of this patch, but Phabricator is showing a clang-tidy lint error on the inclusion of <io.h>. While Windows does have one of these, it's only a compatibility hack, and it doesn't seem to be needed. Can you yank that #include <io.h> line before submitting?

This revision now requires changes to proceed.Aug 26 2020, 8:57 AM
ArcsinX updated this revision to Diff 288002.Aug 26 2020, 9:10 AM

Remove #include <io.h>

I know it's not part of this patch, but Phabricator is showing a clang-tidy lint error on the inclusion of <io.h>. While Windows does have one of these, it's only a compatibility hack, and it doesn't seem to be needed. Can you yank that #include <io.h> line before submitting?

Done

This revision is now accepted and ready to land.Aug 26 2020, 9:47 AM
This revision was landed with ongoing or failed builds.Aug 26 2020, 12:12 PM
This revision was automatically updated to reflect the committed changes.