From bd194901dbbd2abebfe2510a1e97bb8d3a75102b Mon Sep 17 00:00:00 2001 From: Ajay Panicker Date: Thu, 5 May 2016 14:51:34 -0700 Subject: [PATCH] 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 --- btif/src/btif_debug_conn.c | 5 +++-- btif/src/btif_dm.c | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/btif/src/btif_debug_conn.c b/btif/src/btif_debug_conn.c index 87462031d..9c9a9f320 100644 --- a/btif/src/btif_debug_conn.c +++ b/btif/src/btif_debug_conn.c @@ -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; } diff --git a/btif/src/btif_dm.c b/btif/src/btif_dm.c index 39b552427..36346c912 100644 --- a/btif/src/btif_dm.c +++ b/btif/src/btif_dm.c @@ -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)); -- 2.11.0