Fix potential buffer overflows when calling sprintf
1. Replace sprintf with snprintf, which is safer 2. An unsigned int or unsigned long int requires 11 bytes to print (including the null terminator) 3. Use PRIu16 macro to print uint16_t 4. Format string "#%2d %2d:%02d:%02d.%02d\n" in StopWatch::stopLapBtnEventHandler() requires at least 17 bytes. The 16-byte buffer would clearly be overrun if sprintf were used.
This commit is contained in:
@@ -140,9 +140,9 @@ std::string DateTime::FormattedTime() {
|
||||
hour12 = (hour == 12) ? 12 : hour - 12;
|
||||
amPmStr = "PM";
|
||||
}
|
||||
sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr);
|
||||
snprintf(buff, sizeof(buff), "%i:%02i %s", hour12, minute, amPmStr);
|
||||
} else {
|
||||
sprintf(buff, "%02i:%02i", hour, minute);
|
||||
snprintf(buff, sizeof(buff), "%02i:%02i", hour, minute);
|
||||
}
|
||||
return std::string(buff);
|
||||
}
|
||||
|
Reference in New Issue
Block a user