Looking at
operations.cpp, the function get_full_path_name_template has a couple minor issues
that could use improvement.
First, the
original buffer size used in 128. A value of _MAX_PATH (which is 260) + 1
for the trailing nul would prevent the need to iterate, except in unusual
cases.
The code
adds a nul terminator with buf[len]=’\0’ or big_buf[len]=’\0’,
but the underlying GetFullPathName call already nul-terminated the
string. It bugs me that big_buf[len] is used without checking that the
new value of len is OK, and that the char form of the literal is used.
Removing these statements would avoid both issues.
In the past,
I’ve found some APIs that don’t record the desired length
correctly, so I’ve learned to be more defensive and give a little extra
padding rather than the exact amount specified. The specific bug I’ve
found in Windows concerned the character set conversion code, which takes more
space for intermediate results than for the final result. They are also
known for being inconsistent about counting or not counting the trailing nul.
In my own
code, I dealt with “\\?\” specially. Any time the string
begins with “\\” (two actual backslashes) it is either a UNC name
or this special escape prefix, so the function is a no-op. The
documentation for GetFullPathName says that it handles UNC names, and one of
the examples is taking the escape prefix. But the reason for using the
prefix is to get around length limits and avoid automatic processing of the
name, so just shorting out and returning the input unchanged will avoid the
big_buf stuff and prevent any accidental unwanted alteration.
--John
(huge
company footer follows)