I've implemented walk through directory hierarchy using 'readdir_r', it works as expected when I pass my function a string ending with '/' symbols, for example: "./../../".
But when I pass something like "./.." the program fails silently though it should throw an exception of type IOFileAccessException. It happens here (after call to stat fails)
IOFileAccessException::IOFileAccessException(
const std::string& filename,
const std::string& operation,
int errcode)
: runtime_error("Error on access to file \'" + filename +
"\'. Failed operation: \'" + operation + "\'. Error message: " + strerror(errcode)) {
}
...
// prep dirent struct
long int name_max = pathconf(dir.c_str(), _PC_NAME_MAX);
if (name_max == -1) /* Limit not defined, or error */
name_max = 255; /* Take a guess */
long int len = offsetof(struct dirent, d_name) + name_max + 1;
struct dirent *thedir = static_cast<struct dirent*>(malloc(len));
while (!dirs.empty()) {
...
DIR* d = opendir(currentdir.c_str());
...
struct dirent *result = NULL;
do {
int res = readdir_r(d, thedir, &result);
if (res > 0) { // error processing and throw exception. It does not throw in our case }
struct stat buf;
res = stat(d_name.c_str(), &buf);
if (res == -1) {
int err = errno;
throw IOFileAccessException(d_name, "stat (directory is \'" + currentdir + "\')", err);
}
} while (result != NULL);
...
While stepping through code I've found that it fails in exception constructor, after calling ': runtime_error(...'.
The other strange thing is that with input (starting directory) "./.." the d_name var has a value of "./../throw declaration" and result->d_name has value "throw declaration". The result pointer points at the same address as thedir pointer.
Looks like enigma for me, hope you'll help me solve it.
I've pasted the hole code to pastbin here. It's not that big, I just think the message will be cleaner this way.
You can even compile and test it with online compiler if you want here.
Any advice on the case is appreciated. Thank you.