Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak in 'escapefilename()' #164

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,36 @@ typedef struct _filetree {
struct _filetree *right;
} filetree_t;

void escapefilename(char *escape_list, char **filename_ptr)
void escapefilename(char *esc_chars, char **_filename)
{
int x;
int tx;
char *tmp;
char *filename;

filename = *filename_ptr;

tmp = (char*) malloc(strlen(filename) * 2 + 1);
if (tmp == NULL) {
#define NULCHAR '\0'
#define STR_ESC_CHAR '\\'
char *istr = *_filename;
int slen = strlen(istr);
char *estr = (char*) malloc(2 * slen + 1);
if (NULL == estr) {
errormsg("out of memory!\n");
exit(1);
}

for (x = 0, tx = 0; x < strlen(filename); x++) {
if (strchr(escape_list, filename[x]) != NULL) tmp[tx++] = '\\';
tmp[tx++] = filename[x];
int ix = 0, ox = 0;
while(ix < slen) {
if (strchr(esc_chars, istr[ix]) != NULL)
estr[ox++] = STR_ESC_CHAR;
estr[ox++] = istr[ix++];
}
estr[ox] = NULCHAR;

tmp[tx] = '\0';

if (x != tx) {
*filename_ptr = realloc(*filename_ptr, strlen(tmp) + 1);
if (*filename_ptr == NULL) {
if (ox != ix) {
char *efname = malloc(ox + 1);
if (NULL == efname) {
errormsg("out of memory!\n");
exit(1);
}
strcpy(*filename_ptr, tmp);
memcpy(efname, estr, ox + 1);
free(istr);
*_filename = efname;
}
free(estr);
}

dev_t getdevice(char *filename) {
Expand Down