Skip to content

Commit

Permalink
Prevent accidentally recursing forever in VSIMkdirRecursive
Browse files Browse the repository at this point in the history
Also moves the existence test up to try to avoid recursion if possible

Patch by Pete Klier at Google


git-svn-id: https://svn.osgeo.org/gdal/trunk@41195 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
schwehr committed Jan 4, 2018
1 parent 03519b1 commit 7531cfd
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions gdal/port/cpl_vsil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,19 +361,28 @@ int VSIMkdirRecursive( const char *pszPathname, long mode )
return -1;
}

VSIStatBufL sStat;
const CPLString osPathname(pszPathname);
VSIStatBufL sStat;
if( VSIStatL(osPathname, &sStat) == 0 &&
VSI_ISDIR(sStat.st_mode) )
{
return 0;
}
const CPLString osParentPath(CPLGetPath(osPathname));

// Prevent crazy paths from recursing forever.
if( osParentPath == osPathname ||
osParentPath.length() >= osPathname.length() )
{
return -1;
}

if( VSIStatL(osParentPath, &sStat) != 0 )
{
if( VSIMkdirRecursive(osParentPath, mode) != 0 )
return -1;
}
if( VSIStatL(osPathname, &sStat) == 0 &&
VSI_ISDIR(sStat.st_mode) )
{
return 0;
}

return VSIMkdir(osPathname, mode);
}

Expand Down

0 comments on commit 7531cfd

Please sign in to comment.