diff options
| author | Mingjie Shen <shen497@purdue.edu> | 2023-04-20 22:48:04 -0400 |
|---|---|---|
| committer | NeroBurner <pyro4hell@gmail.com> | 2023-12-01 08:52:42 +0100 |
| commit | c9fbcd88181a61d62cd9c53cb3672ca45700b220 (patch) | |
| tree | 73ba674d1b2745a7414548fbc17cd4250d1da498 /src/components/datetime/DateTimeController.cpp | |
| parent | e89e5e4d66dcfb45c1f5137cd854f49b925de513 (diff) | |
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.
Diffstat (limited to 'src/components/datetime/DateTimeController.cpp')
| -rw-r--r-- | src/components/datetime/DateTimeController.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 9e9fb6e4..8d4a834e 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -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); } |
