views:

67

answers:

3

This is actually a question about a huge number of winapi functions. A typical MS documentation says (from http://msdn.microsoft.com/en-us/library/bb762194%28VS.85%29.aspx ):

BOOL SHGetPathFromIDList(      
    PCIDLIST_ABSOLUTE pidl,
    LPTSTR pszPath
);

pidl [in] The address of an item identifier list that specifies a file
or directory location relative to the root of the namespace (the desktop).

pszPath [out] The address of a buffer to receive the file system path.
This buffer must be at least MAX_PATH characters in size.

Nowhere does it say about whether a terminating 0 is written to pszPath. Also, it doesn't say whether the path can fill the pszPath, leaving no room for 0 there.

Googling around yeidls about 50/50 distribution of users who allocate a buffer with MAX_PATH+1 chars and users who only deal with MAX_PATH.

While I can certainly do something like char buf[MAX_PATH+1]={0} to be on the safe side, I would really like to know - is there some place where this stuff is described? Some page for all path-related functions maybe, I don't know...

+1  A: 

I don't know how this function (or the others) actually behaves, but I'd recommend writing a few unit tests against this function... What happens when you don't use all of the buffer? What happens if you do? etc. Not only will these document your assumptions, but if the function ever changes how it behaves, you'll get a warning from your unit tests instead of experiencing a nasty bug report coming in.

Bill
Thanks for the suggestion, but there are several issues here:1. I'm cross-compiling, so runtime checks are mostly unavailable.2. I've no idea how to make this particular function return a directory of MAX_PATH size.3. Unit tests cover only a specific number of cases. I can't possibly enumerate every condition MS may or may not have used when implementing this function (since these conditions are simply unknown).4. If this function ever changes how it behaves, a bug report is going the MS way :)
Alex
Cross-compiling is an aspect of unit testing I hadn't thought about before. The link that ChrisN gave in his comment indicates that the terminating null is a part of MAX_PATH, so you don't need the +1.
Bill
+3  A: 

It says "This buffer must be at least MAX_PATH characters in size" for pszPath parameter so MAX_PATH buffer size should be always enough. Also I believe that all Win32 functions dealing with LPCTSTR / LPTSTR parameters expect or return null-terminated strings.

pingw33n
Thanks, do you have a link?While I do believe this too, a search reveals that some people don't (they allocate MAX_PATH+1), so a definitive answer would be welcome.
Alex
Here I've googled something: https://buildsecurityin.us-cert.gov/daisy/bsi-rules/home/g1/830-BSI.html
pingw33n
MAX_PATH is described here: http://msdn.microsoft.com/en-us/library/aa365247.aspx#maximum_path_length
ChrisN
ChrisN, thanks, that link seems to answer the question completely, there is always space available for \0 in MAX_PATH.I wonder if there's a way to accept a comment as an answer :)
Alex
A: 

To answer the title question: Yes. It's part of the definition of LPTSTR - a pointer to a string. It is also reflected in the prefix: psz - "Pointer (to) String (terminated by) Zero".

There is a non-null-terminated stringtype as well, but it's rare in userland API's: UNICODE_STRING. You see it mostly in kernel-level APIs

MSalters