_TOD_Validate(): Fix incorrect return code

This patch fixes bug #4403. Directives

* rtems_timer_fire_when()
* rtems_timer_server_fire_when()
* rtems_task_wake_when()

are documented to return RTEMS_INVALID_ADDRESS when their time-of-day
argument is NULL. But actually they return RTEMS_INVALID_CLOCK. To fix
the issue this patch changes _TOD_Validate() to return a
status code instead of just true/false.

Close #4403
This commit is contained in:
Frank Kühndel
2021-05-11 16:26:55 +02:00
committed by Sebastian Huber
parent c2687666b1
commit 3af2dc7802
8 changed files with 60 additions and 51 deletions

View File

@@ -353,10 +353,9 @@ static int altera_cyclone_v_ds1339_get_time(int minor, rtems_time_of_day* tod)
temp_tod.month = ds1339_get_month(&time); temp_tod.month = ds1339_get_month(&time);
temp_tod.year = ds1339_get_year(&time); temp_tod.year = ds1339_get_year(&time);
if (_TOD_Validate(&temp_tod)) sc = _TOD_Validate(&temp_tod)
if (sc == RTEMS_SUCCESSFUL)
memcpy(tod, &temp_tod, sizeof(temp_tod)); memcpy(tod, &temp_tod, sizeof(temp_tod));
else
sc = RTEMS_INVALID_CLOCK;
} }
return -sc; return -sc;
@@ -737,10 +736,9 @@ static int altera_cyclone_v_m41st87_get_time(int minor, rtems_time_of_day* tod)
temp_tod.month = m41st87_get_month(&time); temp_tod.month = m41st87_get_month(&time);
temp_tod.year = m41st87_get_year(&time); temp_tod.year = m41st87_get_year(&time);
if (_TOD_Validate(&temp_tod)) sc = _TOD_Validate(&temp_tod);
if (sc == RTEMS_SUCCESSFUL)
memcpy(tod, &temp_tod, sizeof(temp_tod)); memcpy(tod, &temp_tod, sizeof(temp_tod));
else
sc = RTEMS_INVALID_CLOCK;
return -sc; return -sc;
} }

View File

@@ -255,7 +255,7 @@ int setRealTime(
if (!RTC_Is_present()) if (!RTC_Is_present())
return -1; return -1;
if ( !_TOD_Validate(tod) ) if (_TOD_Validate(tod) != RTEMS_SUCCESSFUL)
return -1; return -1;
RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, tod); RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, tod);

View File

@@ -37,16 +37,16 @@ extern "C" {
/** /**
* @brief TOD Validate * @brief TOD Validate
* *
* This support function returns true if @a the_tod contains * This support function tests whether @a the_tod references
* a valid time of day, and false otherwise. * a valid time of day.
* *
* @param[in] the_tod is the TOD structure to validate * @param the_tod A reference to the time of day structure to validate.
* *
* @retval This method returns true if the TOD is valid and false otherwise. * @retval RTEMS_SUCCESSFUL @a the_tod references a valid time of day.
* * @retval RTEMS_INVALID_CLOCK @a the_tod references an invalid time of day.
* @note This routine only works for leap-years through 2099. * @retval RTEMS_INVALID_ADDRESS @a the_tod reference is @c NULL.
*/ */
bool _TOD_Validate( rtems_status_code _TOD_Validate(
const rtems_time_of_day *the_tod const rtems_time_of_day *the_tod
); );

View File

@@ -29,26 +29,25 @@ rtems_status_code rtems_clock_set(
const rtems_time_of_day *tod const rtems_time_of_day *tod
) )
{ {
Status_Control status; rtems_status_code status;
Status_Control score_status;
struct timespec tod_as_timespec;
ISR_lock_Context lock_context;
if ( !tod ) status = _TOD_Validate( tod );
return RTEMS_INVALID_ADDRESS;
if ( _TOD_Validate( tod ) ) { if ( status != RTEMS_SUCCESSFUL ) {
struct timespec tod_as_timespec; return status;
ISR_lock_Context lock_context;
tod_as_timespec.tv_sec = _TOD_To_seconds( tod );
tod_as_timespec.tv_nsec = tod->ticks
* rtems_configuration_get_nanoseconds_per_tick();
_TOD_Lock();
_TOD_Acquire( &lock_context );
status = _TOD_Set( &tod_as_timespec, &lock_context );
_TOD_Unlock();
return _Status_Get( status );
} }
return RTEMS_INVALID_CLOCK; tod_as_timespec.tv_sec = _TOD_To_seconds( tod );
tod_as_timespec.tv_nsec = tod->ticks
* rtems_configuration_get_nanoseconds_per_tick();
_TOD_Lock();
_TOD_Acquire( &lock_context );
score_status = _TOD_Set( &tod_as_timespec, &lock_context );
_TOD_Unlock();
return _Status_Get( score_status );
} }

View File

@@ -35,17 +35,20 @@ const uint32_t _TOD_Days_per_month[ 2 ][ 13 ] = {
{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
}; };
bool _TOD_Validate( rtems_status_code _TOD_Validate(
const rtems_time_of_day *the_tod const rtems_time_of_day *the_tod
) )
{ {
uint32_t days_in_month; uint32_t days_in_month;
uint32_t ticks_per_second; uint32_t ticks_per_second;
if ( the_tod == NULL ) {
return RTEMS_INVALID_ADDRESS;
}
ticks_per_second = TOD_MICROSECONDS_PER_SECOND / ticks_per_second = TOD_MICROSECONDS_PER_SECOND /
rtems_configuration_get_microseconds_per_tick(); rtems_configuration_get_microseconds_per_tick();
if ((!the_tod) || if ((the_tod->ticks >= ticks_per_second) ||
(the_tod->ticks >= ticks_per_second) ||
(the_tod->second >= TOD_SECONDS_PER_MINUTE) || (the_tod->second >= TOD_SECONDS_PER_MINUTE) ||
(the_tod->minute >= TOD_MINUTES_PER_HOUR) || (the_tod->minute >= TOD_MINUTES_PER_HOUR) ||
(the_tod->hour >= TOD_HOURS_PER_DAY) || (the_tod->hour >= TOD_HOURS_PER_DAY) ||
@@ -53,8 +56,9 @@ bool _TOD_Validate(
(the_tod->month > TOD_MONTHS_PER_YEAR) || (the_tod->month > TOD_MONTHS_PER_YEAR) ||
(the_tod->year < TOD_BASE_YEAR) || (the_tod->year < TOD_BASE_YEAR) ||
(the_tod->year > TOD_LATEST_YEAR) || (the_tod->year > TOD_LATEST_YEAR) ||
(the_tod->day == 0) ) (the_tod->day == 0) ) {
return false; return RTEMS_INVALID_CLOCK;
}
if (((the_tod->year % 4) == 0 && (the_tod->year % 100 != 0)) || if (((the_tod->year % 4) == 0 && (the_tod->year % 100 != 0)) ||
(the_tod->year % 400 == 0)) (the_tod->year % 400 == 0))
@@ -62,8 +66,9 @@ bool _TOD_Validate(
else else
days_in_month = _TOD_Days_per_month[ 0 ][ the_tod->month ]; days_in_month = _TOD_Days_per_month[ 0 ][ the_tod->month ];
if ( the_tod->day > days_in_month ) if ( the_tod->day > days_in_month ) {
return false; return RTEMS_INVALID_CLOCK;
}
return true; return RTEMS_SUCCESSFUL;
} }

View File

@@ -30,9 +30,10 @@ rtems_status_code rtems_task_wake_when(
rtems_time_of_day *time_buffer rtems_time_of_day *time_buffer
) )
{ {
uint32_t seconds; uint32_t seconds;
Thread_Control *executing; Thread_Control *executing;
Per_CPU_Control *cpu_self; Per_CPU_Control *cpu_self;
rtems_status_code status;
if ( !_TOD_Is_set() ) if ( !_TOD_Is_set() )
return RTEMS_NOT_DEFINED; return RTEMS_NOT_DEFINED;
@@ -41,9 +42,11 @@ rtems_status_code rtems_task_wake_when(
return RTEMS_INVALID_ADDRESS; return RTEMS_INVALID_ADDRESS;
time_buffer->ticks = 0; time_buffer->ticks = 0;
status = _TOD_Validate( time_buffer );
if ( !_TOD_Validate( time_buffer ) ) if ( status != RTEMS_SUCCESSFUL ) {
return RTEMS_INVALID_CLOCK; return status;
}
seconds = _TOD_To_seconds( time_buffer ); seconds = _TOD_To_seconds( time_buffer );

View File

@@ -132,7 +132,8 @@ rtems_status_code _Timer_Fire_when(
Watchdog_Service_routine_entry adaptor Watchdog_Service_routine_entry adaptor
) )
{ {
rtems_interval seconds; rtems_status_code status;
rtems_interval seconds;
if ( !_TOD_Is_set() ) if ( !_TOD_Is_set() )
return RTEMS_NOT_DEFINED; return RTEMS_NOT_DEFINED;
@@ -140,8 +141,11 @@ rtems_status_code _Timer_Fire_when(
if ( !routine ) if ( !routine )
return RTEMS_INVALID_ADDRESS; return RTEMS_INVALID_ADDRESS;
if ( !_TOD_Validate( wall_time ) ) status = _TOD_Validate( wall_time );
return RTEMS_INVALID_CLOCK;
if ( status != RTEMS_SUCCESSFUL ) {
return status;
}
seconds = _TOD_To_seconds( wall_time ); seconds = _TOD_To_seconds( wall_time );
if ( seconds <= _TOD_Seconds_since_epoch() ) if ( seconds <= _TOD_Seconds_since_epoch() )

View File

@@ -277,14 +277,14 @@ static void test_problem_year(void)
static void test_leap_year(void) static void test_leap_year(void)
{ {
bool test_status; rtems_status_code test_status;
const rtems_time_of_day *problem = &problem_2100; const rtems_time_of_day *problem = &problem_2100;
const rtems_time_of_day *problem2 = &problem_2100_2; const rtems_time_of_day *problem2 = &problem_2100_2;
// 2100 is not a leap year, so it should have 28 days // 2100 is not a leap year, so it should have 28 days
test_status = _TOD_Validate(problem); test_status = _TOD_Validate(problem);
rtems_test_assert(test_status == true); rtems_test_assert(test_status == RTEMS_SUCCESSFUL);
test_status = _TOD_Validate(problem2); test_status = _TOD_Validate(problem2);
rtems_test_assert(test_status == false); rtems_test_assert(test_status == RTEMS_INVALID_CLOCK);
} }
static bool test_year_is_leap_year(uint32_t year) static bool test_year_is_leap_year(uint32_t year)