Skip to content

Commit

Permalink
Fix pointer reuse bug causing off-by-1 day of month
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoslot committed Oct 20, 2023
1 parent 306a680 commit c9dedd3
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/pg_cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -967,15 +967,19 @@ ShouldRunTask(entry *schedule, TimestampTz currentTime, bool doWild,
pg_time_t currentTime_t = timestamptz_to_time_t(currentTime);
pg_time_t tomorrowTime_t = timestamptz_to_time_t(currentTime + USECS_PER_DAY);
struct pg_tm* cur_tm = pg_localtime(&currentTime_t, pg_tzset(cron_timezone));
struct pg_tm* tom_tm = pg_localtime(&tomorrowTime_t, pg_tzset(cron_timezone));

int minute = cur_tm->tm_min -FIRST_MINUTE;
int hour = cur_tm->tm_hour -FIRST_HOUR;
int dayOfMonth = cur_tm->tm_mday -FIRST_DOM;
int month = cur_tm->tm_mon +1 -FIRST_MONTH;
int dayOfWeek = cur_tm->tm_wday -FIRST_DOW;

bool lastdom = (schedule->flags & DOM_LAST) != 0 && tom_tm->tm_mday == 1;
/*
* pg_localtime returns a pointer to a global struct,
* so cur_tm cannot be used after this point.
*/
struct pg_tm* tomorrow_tm = pg_localtime(&tomorrowTime_t, pg_tzset(cron_timezone));
bool lastdom = (schedule->flags & DOM_LAST) != 0 && tomorrow_tm->tm_mday == 1;
bool thisdom = lastdom || bit_test(schedule->dom, dayOfMonth) != 0;
bool thisdow = bit_test(schedule->dow, dayOfWeek);

Expand Down

0 comments on commit c9dedd3

Please sign in to comment.