Skip to content

Commit

Permalink
tests: Fix failing after year 2038 (#442)
Browse files Browse the repository at this point in the history
Fixes #439

* Avoid 32-bit integer overflow after 2038

Co-authored-by: Bernhard M. Wiedemann <[email protected]>
  • Loading branch information
andy5995 and bmwiedemann committed Mar 26, 2024
1 parent b69205f commit f5262a3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
=== rmw ChangeLog ===

2024-03-26

* Fix for tests failing after the Epochalypse (#439)

2024-03-21

+ Add French translation
Expand Down
55 changes: 29 additions & 26 deletions src/purging_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,52 @@ bool
is_time_to_purge(st_time *st_time_var, const char *file)
{
const int BUF_TIME = 80;
char time_prev[BUF_TIME];
bool purge_needed = true;

// Open the file for reading
FILE *fp = fopen(file, "r");
bool init = (fp);
if (fp)
{
char time_prev[BUF_TIME];

if (fgets(time_prev, sizeof time_prev, fp) == NULL)
// Read the previous time from the file
if (fgets(time_prev, sizeof(time_prev), fp) != NULL)
{
trim_whitespace(time_prev);
// Check if the difference is less than a day
purge_needed =
((st_time_var->now - atoll(time_prev)) < SECONDS_IN_A_DAY) ? false : true;
}
else
{
print_msg_error();
printf("while getting line from %s\n", file);
perror(__func__);
close_file(&fp, file, __func__);
exit(EXIT_FAILURE);
}

trim_whitespace(time_prev);
close_file(&fp, file, __func__);

if ((st_time_var->now - atoi(time_prev)) < SECONDS_IN_A_DAY)
return false;
fclose(fp); // Close the file after reading
}
else if (errno == ENOENT)
purge_needed = false;

// Open the file for writing to update the time
fp = fopen(file, "w");
if (fp)
{
fprintf(fp, "%ld\n", st_time_var->now);
close_file(&fp, file, __func__);

/*
* if this is the first time the file got created, it's very likely
* indeed that purge does not need to run. Only return FALSE if the
* file didn't previously exist.
*/
return init;
// Check for errors when closing the file after writing
if (fclose(fp) != 0)
{
perror("Error closing file after write");
exit(EXIT_FAILURE);
}
}
else
{
open_err(file, __func__);
exit(errno); // Exit if unable to write the file
}

/*
* if we can't even write this file to the config directory, something
* is not right. Make it fatal.
*/
open_err(file, __func__);
exit(errno);
return purge_needed;
}


Expand Down Expand Up @@ -195,7 +198,7 @@ do_file_purge(char *purge_target, const rmw_options *cli_user_options,
// sleep(1);
}
else
printf("-%s\n", pt_basename);
printf("-%s\n", pt_basename);
}
else
msg_err_remove(trashinfo_entry_realpath, __func__);
Expand Down
11 changes: 10 additions & 1 deletion src/time_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ set_time_string(char *tm_str, const size_t len, const char *format,
time_t time_t_now)
{
struct tm result;
localtime_r(&time_t_now, &result);
if (localtime_r(&time_t_now, &result) == NULL)
{
fputs
("Error: localtime_r() failed for time_t value beyond 32-bit limit.\n",
stderr);
exit(EXIT_FAILURE);
}
strftime(tm_str, len, format, &result);
trim_whitespace(tm_str);

return;
}

/*!
Expand Down Expand Up @@ -64,6 +72,7 @@ void
init_time_vars(st_time *x)
{
x->now = time(NULL);
// x->now = 0x80000000;

set_which_deletion_date(x->t_fmt);

Expand Down
23 changes: 23 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ add_test_setup(
env: 'RMW_FAKE_MEDIA_ROOT=true'
)

#
# This does not work
#
#faketime_lib = cc.find_library(
# 'faketime',
# dirs: [
# '/usr/lib/faketime',
# '/lib/x86_64-linux-gnu/faketime',
# '/lib/aarch64-linux-gnu/faketime'
# ],
# required: false
# )

# TODO: Detect whether ubsan is required or not, determine the path
add_test_setup(
'epochalypse',
env: [
#'LD_PRELOAD=' + faketime_lib.full_path(),
'LD_PRELOAD=/usr/lib/faketime/libfaketime.so.1',
'FAKETIME=+14y'
]
)

test_cases = [
'strings_rmw',
'utils_rmw',
Expand Down

0 comments on commit f5262a3

Please sign in to comment.