Probleme with latest ARM Toolchain and time functions

Bug #1856208 reported by Julien REYNAUD on 2019-12-12
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Medium
Joey Ye

Bug Description

Hello,

I have a project based on a STM32 using the internal RTC.

I was using the toolchain “arm-eabi-gcc7.2.0-r3” (from Visual GDB) to translate a timestamp UNIX to a tm struct and then set my RTC like this :

#include
#include <sys\time.h>

void RTC_SetTimeByTimeStamp(uint32_t u32_TimeStamp)
{
struct tm* p_time;
/* convert into unix time struct */
p_time = gmtime((time_t*)&u32_TimeStamp);
...
}

All was OK. If I call this function with a timestamp at 1576158671, the p_time struct contain :

12/12/2019 13:51:11

But with the latset toolchain “9-2019-q4-major”, the same function, call with the same timestamp value set the p_time struct with random value like :

2/1/1971721 11:25:06

Thanks for your help.

Joey Ye (jinyun-ye) wrote :

Julien,

Can you please share a simple but compilable/runable test case for us to reproduce?

Julien REYNAUD (julien-innovel) wrote :

Hello,

here you will find a test case that reproduce the probleme :

#include <time.h>
#include <sys\time.h>

void main(void)
{
 struct tm* p_time;
 uint32_t u32_TimeStamp = 1576158671; // time stamp at 12/12/2019 13:51:11
 /* convert into unix time struct */
 p_time = gmtime((time_t*)&u32_TimeStamp);

  printf("Date : %s", ctime((time_t*)&u32_TimeStamp));
  printf("Converted to %s", asctime(p_time));

}

Joey Ye (jinyun-ye) wrote :

Julien,

time_t is a 64-bit type. So using uint32_t to define TimeStamp and casting it to time_t* is not safe. With gcc7 it may happen to pass with random leading zeros.

I modified your case to replace uint32_t with uint64_t, then it ran as expected with both gcc7 and gcc9.

Can you please give a try?

Thanks,
Joey

Changed in gcc-arm-embedded:
status: New → Invalid
importance: Undecided → Medium
assignee: nobody → Joey Ye (jinyun-ye)
Julien REYNAUD (julien-innovel) wrote :

Hello,

I've try with uint64_t and it's working perfectly !

Thanks a lot for your help !

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers