Changeset 731e68a3 in rtems


Ignore:
Timestamp:
Aug 25, 2017, 1:22:37 PM (2 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
fae59c9
Parents:
3663be53
git-author:
Sebastian Huber <sebastian.huber@…> (08/25/17 13:22:37)
git-committer:
Sebastian Huber <sebastian.huber@…> (09/06/17 05:42:25)
Message:

Fix integer overflow problems in times()

An integer overflow may still happen, however, only after 68 years of
system uptime.

Close #2135.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libcsupport/src/__times.c

    r3663be53 r731e68a3  
    4444)
    4545{
    46   rtems_interval ticks, us_per_tick;
     46  uint32_t       tick_interval;
     47  struct bintime binuptime;
     48  sbintime_t     uptime;
     49  struct bintime bin_cpu_time_used;
     50  sbintime_t     cpu_time_used;
    4751
    4852  if ( !ptms )
    4953    rtems_set_errno_and_return_minus_one( EFAULT );
    5054
    51   memset( ptms, 0, sizeof( *ptms ) );
     55  tick_interval = (uint32_t)
     56    (SBT_1US * rtems_configuration_get_microseconds_per_tick());
    5257
    53   /*
    54    *  This call does not depend on TOD being initialized and can't fail.
    55    */
     58  ptms = memset( ptms, 0, sizeof( *ptms ) );
    5659
    57   ticks = rtems_clock_get_ticks_since_boot();
    58   us_per_tick = rtems_configuration_get_microseconds_per_tick();
     60  _TOD_Get_zero_based_uptime( &binuptime );
     61  uptime = bttosbt( binuptime );
     62  ptms->tms_stime = ((clock_t) uptime) / tick_interval;
    5963
    6064  /*
     
    6569   *  this thread.
    6670   */
    67   {
    68     Timestamp_Control  cpu_time_used;
    69     Timestamp_Control  per_tick;
    70     uint32_t           ticks_of_executing;
    71     uint32_t           fractional_ticks;
     71  _Thread_Get_CPU_time_used( _Thread_Get_executing(), &bin_cpu_time_used );
     72  cpu_time_used = bttosbt( bin_cpu_time_used );
     73  ptms->tms_utime = ((clock_t) cpu_time_used) / tick_interval;
    7274
    73     _Thread_Get_CPU_time_used( _Thread_Get_executing(), &cpu_time_used );
    74     _Timestamp_Set(
    75       &per_tick,
    76       rtems_configuration_get_microseconds_per_tick() /
    77           TOD_MICROSECONDS_PER_SECOND,
    78       (rtems_configuration_get_nanoseconds_per_tick() %
    79           TOD_NANOSECONDS_PER_SECOND)
    80     );
    81     _Timestamp_Divide(
    82       &cpu_time_used,
    83       &per_tick,
    84       &ticks_of_executing,
    85       &fractional_ticks
    86     );
    87 
    88     ptms->tms_utime = ticks_of_executing * us_per_tick;
    89   }
    90 
    91   ptms->tms_stime  = ticks * us_per_tick;
    92 
    93   return ticks * us_per_tick;
     75  return ptms->tms_stime;
    9476}
    9577
Note: See TracChangeset for help on using the changeset viewer.