Skip to content
Closed
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
61 changes: 56 additions & 5 deletions src/console/dlt-convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ int main(int argc, char *argv[])
struct dirent **files = { 0 };
int n = 0;

/* Fix: Introduce variables to handle the effective argument list safely */
char **work_argv = argv; /* Pointer to the arguments we will actually use */
int work_argc = argc; /* The number of arguments we will use */
int work_argv_allocated = 0; /* Flag to check if we need to free work_argv */

struct iovec iov[2];
int bytes_written = 0;
int syserr = 0;
Expand Down Expand Up @@ -397,20 +402,45 @@ int main(int argc, char *argv[])
}

/* do not include ./ and ../ in the files */
argc = optind + (n - 2);
/* FIX: Calculate new argc but DO NOT assign to local 'argc' yet to avoid confusion.
Instead, allocate a new pointer array large enough to hold the file list. */
work_argc = optind + (n - 2);

/* Allocate memory for the new argument list.
We need indices from 0 to work_argc-1 to be accessible. */
work_argv = (char **)calloc((size_t)work_argc, sizeof(char *));
if (work_argv == NULL) {
fprintf(stderr, "ERROR: Memory allocation failed for file list!\n");
if (ovalue) {
close(ohandle);
ohandle = -1;
}
/* Clean up scandir results */
if (files) {
for (int i = 0; i < n ; i++) {
if (files[i]) free(files[i]);
}
free(files);
}
return -1;
}
work_argv_allocated = 1;
}

for (index = optind; index < argc; index++) {
/* FIX: Loop using the safe work_argc and work_argv */
for (index = optind; index < work_argc; index++) {
if (tflag) {
memset(tmp_filename, 0, FILENAME_SIZE);
snprintf(tmp_filename, FILENAME_SIZE, "%s%s",
DLT_CONVERT_WS, files[index - optind + 2]->d_name);
DLT_CONVERT_WS, files[index - optind + 2]->d_name);

argv[index] = tmp_filename;
/* FIX: Write to the dynamically allocated array, not the original fixed argv */
work_argv[index] = tmp_filename;
}

/* load, analyze data file and create index list */
if (dlt_file_open(&file, argv[index], vflag) >= DLT_RETURN_OK) {
/* FIX: Use work_argv[index] */
if (dlt_file_open(&file, work_argv[index], vflag) >= DLT_RETURN_OK) {
while (dlt_file_read(&file, vflag) >= DLT_RETURN_OK) {
}
}
Expand All @@ -433,6 +463,12 @@ int main(int argc, char *argv[])
ohandle = -1;
}
dlt_file_free(&file, vflag);
/* Fix: cleanup memory before return */
if (work_argv_allocated && work_argv) free(work_argv);
if (tflag && files) {
for (int i = 0; i < n ; i++) if (files[i]) free(files[i]);
free(files);
}
return -1;
}

Expand All @@ -443,6 +479,12 @@ int main(int argc, char *argv[])
ohandle = -1;
}
dlt_file_free(&file, vflag);
/* Fix: cleanup memory before return */
if (work_argv_allocated && work_argv) free(work_argv);
if (tflag && files) {
for (int i = 0; i < n ; i++) if (files[i]) free(files[i]);
free(files);
}
return -1;
}

Expand Down Expand Up @@ -496,6 +538,8 @@ int main(int argc, char *argv[])
close(ohandle);
ohandle = -1;
dlt_file_free(&file, vflag);
/* Fix: cleanup memory before return */
if (work_argv_allocated && work_argv) free(work_argv);
return -1;
}
}
Expand Down Expand Up @@ -547,6 +591,13 @@ int main(int argc, char *argv[])
}
rmdir(DLT_CONVERT_WS);
}

/* Fix: Free the allocated work_argv if it was used */
if (work_argv_allocated && work_argv) {
free(work_argv);
work_argv = NULL;
}

if (index == optind) {
/* no file selected, show usage and terminate */
fprintf(stderr, "ERROR: No file selected\n");
Expand Down
Loading