Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ea5dfa6e authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Fix undefined usage of snprintf

Using a buffer as both the format and output for snprintf could
cause undefinied behaviour on certain platforms. Instead just
use a temporary variable.

Bug: 27882028
Change-Id: If9f96fba4b3447b3248917ab9fb994bd80cbca0f
parent 9a6ac4cd
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -42,8 +42,9 @@ static char *format_ts(const uint64_t ts, char *buffer, int len) {
  const time_t secs = ms / 1000;
  struct tm *ptm = localtime(&secs);

  strftime(buffer, len, "%m-%d %H:%M:%S.%%03u", ptm);
  snprintf(buffer, len, buffer, (uint16_t)(ms % 1000));
  char tempbuff[20];
  strftime(tempbuff, sizeof(tempbuff), "%m-%d %H:%M:%S", ptm);
  snprintf(buffer, len, "%s.%03u", tempbuff, (uint16_t)(ms % 1000));

  return buffer;
}
+5 −3
Original line number Diff line number Diff line
@@ -3447,10 +3447,12 @@ void btif_debug_bond_event_dump(int fd) {
         i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) {
        btif_bond_event_t* event = &btif_dm_bond_events[i];

        char eventtime[15];
        char eventtime[20];
        char temptime[20];
        struct tm *tstamp = localtime(&event->timestamp.tv_sec);
        strftime(eventtime, sizeof(eventtime), "%H:%M:%S.%%03u", tstamp);
        snprintf(eventtime, sizeof(eventtime), eventtime, (event->timestamp.tv_nsec) / 1000000);
        strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
        snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
                 event->timestamp.tv_nsec / 1000000);

        char bdaddr[18];
        bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr));