Skip to content

Commit fac1470

Browse files
andreasstiegerras0219-msft
authored andcommitted
Fix gcc8 error/warning -Werror=format-truncation= (#787)
utilities::datetime::to_string(): datetime_str and buf were oversized for fitting into output without possible trunctation
1 parent ef7e37e commit fac1470

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Release/src/utilities/asyncrt_utils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,13 @@ utility::string_t datetime::to_string(date_format format) const
691691
{
692692
// Append fractional second, which is a 7-digit value with no trailing zeros
693693
// This way, '1200' becomes '00012'
694-
char buf[9] = { 0 };
694+
const int max_frac_length = 8;
695+
char buf[max_frac_length+1] = { 0 };
695696
snprintf(buf, sizeof(buf), ".%07ld", (long int)frac_sec);
696697
// trim trailing zeros
697-
for (int i = 7; buf[i] == '0'; i--) buf[i] = '\0';
698+
for (int i = max_frac_length-1; buf[i] == '0'; i--) buf[i] = '\0';
698699
// format the datetime into a separate buffer
699-
char datetime_str[max_dt_length+1] = {0};
700+
char datetime_str[max_dt_length-max_frac_length-1+1] = {0};
700701
strftime(datetime_str, sizeof(datetime_str), "%Y-%m-%dT%H:%M:%S", &datetime);
701702
// now print this buffer into the output buffer
702703
snprintf(output, sizeof(output), "%s%sZ", datetime_str, buf);

0 commit comments

Comments
 (0)