Skip to content

Commit

Permalink
UCS/LOG: convert stack allocation at ucs_log_file_rotate() to heap
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-shalev committed Jun 18, 2024
1 parent 0fafd4d commit 90260f5
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/ucs/debug/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,20 @@ static void ucs_log_get_file_name(char *log_file_name, size_t max, int idx)

static void ucs_log_file_rotate()
{
char old_log_file_name[PATH_MAX];
char new_log_file_name[PATH_MAX];
char *old_log_file_name = ucs_malloc(PATH_MAX, "old_log_file_name");
char *new_log_file_name = ucs_malloc(PATH_MAX, "new_log_file_name");
int idx, ret;

if ((old_log_file_name == NULL) || (new_log_file_name == NULL)) {
ucs_error("Failed to allocate memory for log file names");
goto err_out;
}

if (ucs_log_file_last_idx == ucs_global_opts.log_file_rotate) {
/* remove the last file and log rotation from the
* `log_file_rotate - 1` file */
ucs_log_get_file_name(old_log_file_name,
sizeof(old_log_file_name),
PATH_MAX,
ucs_log_file_last_idx);
unlink(old_log_file_name);
} else {
Expand All @@ -164,30 +169,42 @@ static void ucs_log_file_rotate()

for (idx = ucs_log_file_last_idx - 1; idx >= 0; --idx) {
ucs_log_get_file_name(old_log_file_name,
sizeof(old_log_file_name), idx);
PATH_MAX, idx);
ucs_log_get_file_name(new_log_file_name,
sizeof(new_log_file_name), idx + 1);
PATH_MAX, idx + 1);

if (access(old_log_file_name, W_OK) != 0) {
ucs_fatal("unable to write to %s", old_log_file_name);
ucs_error("unable to write to %s", old_log_file_name);
goto err_out;
}

/* coverity[toctou] */
ret = rename(old_log_file_name, new_log_file_name);
if (ret) {
ucs_fatal("failed to rename %s to %s: %m",
ucs_error("failed to rename %s to %s: %m",
old_log_file_name, new_log_file_name);
goto err_out;
}


if (access(old_log_file_name, F_OK) != -1) {
ucs_fatal("%s must not exist on the filesystem", old_log_file_name);
ucs_error("%s must not exist on the filesystem", old_log_file_name);
goto err_out;
}

if (access(new_log_file_name, W_OK) != 0) {
ucs_fatal("unable to write to %s", new_log_file_name);
ucs_error("unable to write to %s", new_log_file_name);
goto err_out;
}
}

ucs_free(old_log_file_name);
ucs_free(new_log_file_name);
return;

err_out:
ucs_free(old_log_file_name);
ucs_free(new_log_file_name);
ucs_fatal("ucs_log_file_rotate() fatal error (see logs before)");
}

static void ucs_log_handle_file_max_size(int log_entry_len)
Expand Down

0 comments on commit 90260f5

Please sign in to comment.