Skip to content

Commit

Permalink
Fix getcwd to not rely on MAXPATHLEN
Browse files Browse the repository at this point in the history
Dynamically allocate the buffer for getcwd, instead of assuming it is
at most some arbitrarly-fixed size. Also, optionally use PATH_MAX
(which is optional in POSIX) as starting size for the buffer, instead
of MAXPATHLEN (which is not even POSIX).
  • Loading branch information
pinotree authored and malcolmhumphreys committed Sep 17, 2016
1 parent 5794f4f commit 21e55af
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/core/PathUtils.cpp
Expand Up @@ -31,6 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <limits>
#include <sstream>
#include <sys/stat.h>
#include <errno.h>

#include <OpenColorIO/OpenColorIO.h>

Expand Down Expand Up @@ -151,9 +152,17 @@ OCIO_NAMESPACE_ENTER
_getcwd(path, MAXPATHLEN);
return path;
#else
char path[MAXPATHLEN];
::getcwd(path, MAXPATHLEN);
return path;
std::vector<char> current_dir;
#ifdef PATH_MAX
current_dir.resize(PATH_MAX);
#else
current_dir.resize(1024);
#endif
while (::getcwd(&current_dir[0], current_dir.size()) == NULL && errno == ERANGE) {
current_dir.resize(current_dir.size() + 1024);
}

return std::string(&current_dir[0]);
#endif
}

Expand Down

0 comments on commit 21e55af

Please sign in to comment.