As evolution of a school exercise I'm making a program that writes a file in every subfolder starting from the location where the program is executed. So there is a recursive function and another function called inside that writes the file.
If I execute this I get "exited with code=3221226356" error the second time I'm writing the file (inside the first subfolder, when I create the ofstream)... only while not in debug. After a bit of experiments I removed the recursive call and the files into all the main directories are written. There are no arrays except the input variable (char*), what could be causing this memory leak?
This is the code:
#include <dirent.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <time.h>
#include <limits.h>
const char * separator_char() {
#ifdef _WIN32
return "\\";
#else
return "/";
#endif
}
void createCopy(const char * path) {
const char * separator = separator_char();
char * file_name_path = strdup(path);
strcat(file_name_path, separator);
printf("Writing Roberto.txt...\n");
strcat(file_name_path, "Roberto.txt");
std::ofstream dst(file_name_path, std::ios::binary | std::ofstream::trunc); -- ERROR HERE
dst << "test";
printf("Wrote %s\n", file_name_path);
dst.close();
}
void multiply(const char * path) {
const char * separator = separator_char();
char * path_2 = strdup(path);
strcat(path_2, separator);
DIR * dir = opendir(path);
struct dirent * entry = readdir(dir);
while (entry != NULL) {
if (strcmp(entry -> d_name, ".") != 0 && strcmp(entry -> d_name, "..") && entry -> d_type == DT_DIR) {
char * path_3 = strdup(path_2);
strcat(path_3, entry -> d_name);
printf("%s\n", path_3);
createCopy(path_3);
multiply(path_3);
}
entry = readdir(dir);
}
closedir(dir);
}
int main(int argc, char ** argv) {
const char * PATH = ".";
multiply(PATH);
getchar();
return 0;
}
Also
CodePudding user response:
These two lines are a likely problem:
char * file_name_path = strdup(path);
strcat(file_name_path, separator);
The strdup
call allocates enough memory for the string you want to duplicate, not a single byte more.
That means the strcat
call will cause your program to write pout of bounds of the allocated memory, leading to undefined behavior (and heap corruption).
One possible C-like solution is to use the snprintf
function to construct your string. It can be used with a null pointer destination string and a zero size, then will return the number of bytes needed for the string (excluding the null-terminator). Then you can allocate memory for the string, and use snprintf
again to actually create the string.
Or since you're programming C , just use std::string
and append as needed. No out-of-bounds problems, no memory leaks (which you also have).
And considering you're dealing with filesystem paths, use std::filesystem::path
instead:
void createCopy(std::filesystem::path const& path) {
auto file_name_path = path / "Roberto.txt";
std::ofstream dst(file_name_path, std::ios::binary | std::ofstream::trunc);
dst << "test";
}