none
About MAX_PATH RRS feed

  • Question

  • Hello

    I use below code to save a path location for a file specified by the user:

    wchar_t filePath[MAX_PATH] = getFilePath();

    MAX_PATH is set to be 260, but is there any possibility if the file path is over 260 characters? What happens if the file path gets over 260 characters?

    Cheers


    Monday, March 21, 2016 1:31 PM

Answers

  • On 3/21/2016 9:55 AM, oloom wrote:

    So I how should I handle long paths?

    Very, very carefully. For details, see

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#maxpath

    In the first approximation, it would probably be easiest to just bail out with an error if the user attempts to pass a long path (unless you have a compelling reason to go the extra mile to handle them). Long paths are rare in practice - in part because most tools can't deal with them, so users learned to avoid creating them; it's a bit of a self-fulfilling prophecy.

    Or how do I detect long paths are passed to the command line argument?

    Are you asking how to obtain the length of a string?

    Does something like below may help?

    wchar_t filePath[1000];

    Help to do what? It's still not clear to me how filePath variable is initialized or used.

    • Marked as answer by oloom Tuesday, March 22, 2016 9:52 AM
    Monday, March 21, 2016 2:09 PM

All replies

  • On 3/21/2016 9:31 AM, oloom wrote:

    I use below code to save a path location for a file specified by the user:

    wchar_t filePath[MAX_PATH] = getFilePath();

    I can't think of any declaration for getFilePath that would allow this line to compile. I suggest you show your actual code.

    MAX_PATH is set to be 260, but is there any possibility if the file path is over 260 characters? What happens if the file path gets over 260 characters?

    This depends on how getFilePath is implemented. Most API functions fail on paths longer than MAX_PATH, unless certain precautions are taken. If the program is not specifically designed to work with long paths, chances are high it'll start seeing these failures (and if it doesn't handle errors from API calls appropriately, it'll likely crash or otherwise misbehave).

    Monday, March 21, 2016 1:46 PM
  • See Naming Files, Paths, and Namespaces for a full discussion of path lengths and related limitations.  It is possible for UNICODE path lengths to be up to 32,767 characters. 

    Generally speaking, in your above example if your function returns a path length that exceeds MAX_PATH there will be a buffer overrun.  Buffer overruns can corrupt the stack, the heap or result in other undefined behavior.


    • Edited by RLWA32 Monday, March 21, 2016 1:52 PM
    Monday, March 21, 2016 1:51 PM
  • Thanks Igor,

    No I just wanted to describe the case. I know it does not compile.

    I just get the string from 

    GetCommandLineW()

    So I how should I handle long paths? Or how do I detect long paths are passed to the command line argument?

    Does something like below may help?

    wchar_t filePath[1000];

    Cheers

    Monday, March 21, 2016 1:55 PM
  • On 3/21/2016 9:31 AM, oloom wrote:

    I use below code to save a path location for a file specified by the user:

    wchar_t filePath[MAX_PATH] = getFilePath();

    I can't think of any declaration for getFilePath that would allow this line to compile. I suggest you show your actual code.

    (Ok, this is more an intellectual exercise, than anything useful) Wouldn't this definition compile:

    #define getFilePath() "This\\Is\\A\\Path"

    Monday, March 21, 2016 1:56 PM
  • On 3/21/2016 9:56 AM, SimonRev wrote:

    On 3/21/2016 9:31 AM, oloom wrote:

    I use below code to save a path location for a file specified by the user:

    wchar_t filePath[MAX_PATH] = getFilePath();

    I can't think of any declaration for getFilePath that would allow this line to compile. I suggest you show your actual code.

    (Ok, this is more an intellectual exercise, than anything useful) Wouldn't this definition compile:

    #define getFilePath() "This\\Is\\A\\Path"

    Well, yes, I suppose. I was thinking about an actual function, one capable of obtaining "a path location for a file specified by the user".

    Monday, March 21, 2016 2:00 PM
  • Thanks Igor,

    No I just wanted to describe the case. I know it does not compile.

    I just get the string from 

    GetCommandLineW()

    So I how should I handle long paths? Or how do I detect long paths are passed to the command line argument?

    Does something like below may help?

    wchar_t filePath[1000];

    Cheers

    See https://blogs.msdn.microsoft.com/oldnewthing/20031210-00/?p=41553/

    for a discussion of command line length limitations.

    Monday, March 21, 2016 2:01 PM
  • On 3/21/2016 9:55 AM, oloom wrote:

    So I how should I handle long paths?

    Very, very carefully. For details, see

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#maxpath

    In the first approximation, it would probably be easiest to just bail out with an error if the user attempts to pass a long path (unless you have a compelling reason to go the extra mile to handle them). Long paths are rare in practice - in part because most tools can't deal with them, so users learned to avoid creating them; it's a bit of a self-fulfilling prophecy.

    Or how do I detect long paths are passed to the command line argument?

    Are you asking how to obtain the length of a string?

    Does something like below may help?

    wchar_t filePath[1000];

    Help to do what? It's still not clear to me how filePath variable is initialized or used.

    • Marked as answer by oloom Tuesday, March 22, 2016 9:52 AM
    Monday, March 21, 2016 2:09 PM
  • Thanks again.

    No, I suppose, if file path is more than 260 characters,

    wchar_t filePath[MAX_PATH];

    it gets truncated for the above case.

    So I thought if I assign the length more than 260 characters, say as 1000

    as below, I could fix the issue.

    wchar_t filePath[1000];

    But appearantly as you described it is not about the array length but the

    API problems that creates the limitation.

    Thanks again.

    Monday, March 21, 2016 2:17 PM
  • On 3/21/2016 10:17 AM, oloom wrote:

    No, I suppose, if file path is more than 260 characters,

    wchar_t filePath[MAX_PATH];it gets truncated for the above case.

    What do you mean, "gets truncated"? Gets truncated where, how and by whom?

    Again, if you need any further assistance, show the code by which filePath variable gets initialized, and the code where it's used.

    Monday, March 21, 2016 2:45 PM

  • // if proto is  wchar_t *getFilePath(void);

    include <string> ; // for wstring class

    wstring filePath = getFilePath();

    // string or wstring class, not need care MAX_PATH
    Tuesday, August 27, 2019 8:50 AM