From 1ee1b58b62c683cfe368ba4f9b1b0598297bf758 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 6 May 2013 19:16:45 +0200 Subject: [PATCH] *: use StringFormat() instead of _sntprintf() Prepare for enforcing null-termination (TRAC #2813). --- src/Dialogs/Airspace/dlgAirspaceWarnings.cpp | 6 ++--- src/Dialogs/Device/DeviceEditWidget.cpp | 5 ++-- src/Formatter/AngleFormatter.cpp | 12 +++++----- src/Formatter/ByteSizeFormatter.cpp | 4 ++-- src/Formatter/GeoPointFormatter.cpp | 36 ++++++++++++++-------------- src/Formatter/GlideRatioFormatter.cpp | 5 ++-- src/Logger/ExternalLogger.cpp | 11 +++++---- src/Profile/DeviceConfig.cpp | 13 +++++----- src/RadioFrequency.cpp | 9 ++++--- src/unix/tchar.h | 1 - 10 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/Dialogs/Airspace/dlgAirspaceWarnings.cpp b/src/Dialogs/Airspace/dlgAirspaceWarnings.cpp index 8f6756fd8..34c246fce 100644 --- a/src/Dialogs/Airspace/dlgAirspaceWarnings.cpp +++ b/src/Dialogs/Airspace/dlgAirspaceWarnings.cpp @@ -361,9 +361,9 @@ AirspaceWarningListHandler::OnPaintItem(Canvas &canvas, canvas.SetTextColor(COLOR_GRAY); { // name, altitude info - _sntprintf(buffer, 21, _T("%s %s"), - airspace.GetName(), - AirspaceFormatter::GetClass(airspace)); + StringFormat(buffer, 21, _T("%s %s"), + airspace.GetName(), + AirspaceFormatter::GetClass(airspace)); canvas.DrawClippedText(paint_rc.left + left0, paint_rc.top + Layout::Scale(text_top), diff --git a/src/Dialogs/Device/DeviceEditWidget.cpp b/src/Dialogs/Device/DeviceEditWidget.cpp index b72b78e64..c2d94e6de 100644 --- a/src/Dialogs/Device/DeviceEditWidget.cpp +++ b/src/Dialogs/Device/DeviceEditWidget.cpp @@ -25,6 +25,7 @@ #include "UIGlobals.hpp" #include "Compiler.h" #include "Util/Macros.hpp" +#include "Util/StringUtil.hpp" #include "Util/NumberParser.hpp" #include "Language/Language.hpp" #include "Form/DataField/Enum.hpp" @@ -269,8 +270,8 @@ FillAndroidIOIOPorts(DataFieldEnum &df, const DeviceConfig &config) TCHAR tempID[4]; TCHAR tempName[15]; for (unsigned i = 0; i < AndroidIOIOUartPort::getNumberUarts(); i++) { - _sntprintf(tempID, sizeof(tempID), _T("%d"), i); - _sntprintf(tempName, sizeof(tempName), _T("IOIO Uart %d"), i); + StringFormatUnsafe(tempID, _T("%u"), i); + StringFormat(tempName, sizeof(tempName), _T("IOIO Uart %u"), i); unsigned id = AddPort(df, DeviceConfig::PortType::IOIOUART, tempID, tempName, AndroidIOIOUartPort::getPortHelp(i)); diff --git a/src/Formatter/AngleFormatter.cpp b/src/Formatter/AngleFormatter.cpp index de379bbca..dd4e0fd98 100644 --- a/src/Formatter/AngleFormatter.cpp +++ b/src/Formatter/AngleFormatter.cpp @@ -23,8 +23,8 @@ Copyright_License { #include "AngleFormatter.hpp" #include "Math/Angle.hpp" +#include "Util/StringUtil.hpp" -#include #include void @@ -35,9 +35,9 @@ FormatBearing(TCHAR *buffer, size_t size, unsigned value_degrees, assert(size >= 8); if (suffix != NULL) - _sntprintf(buffer, size, _T("%u° %s"), value_degrees, suffix); + StringFormat(buffer, size, _T("%u° %s"), value_degrees, suffix); else - _sntprintf(buffer, size, _T("%u°"), value_degrees); + StringFormat(buffer, size, _T("%u°"), value_degrees); } void @@ -54,9 +54,9 @@ FormatAngleDelta(TCHAR *buffer, size_t size, Angle value) int degrees = iround(value.AsDelta().Degrees()); if (degrees > 1) - _sntprintf(buffer, size, _T("%u°»"), degrees); + StringFormat(buffer, size, _T("%u°»"), degrees); else if (degrees < -1) - _sntprintf(buffer, size, _T("«%u°"), -degrees); + StringFormat(buffer, size, _T("«%u°"), -degrees); else _tcscpy(buffer, _T("«»")); } @@ -69,7 +69,7 @@ FormatVerticalAngleDelta(TCHAR *buffer, size_t size, Angle value) int degrees = iround(value.AsDelta().Degrees()); if (degrees < -1 || degrees > 1) - _sntprintf(buffer, size, _T("%+d°"), degrees); + StringFormat(buffer, size, _T("%+d°"), degrees); else _tcscpy(buffer, _T("--")); } diff --git a/src/Formatter/ByteSizeFormatter.cpp b/src/Formatter/ByteSizeFormatter.cpp index 8608f915c..d6aa01157 100644 --- a/src/Formatter/ByteSizeFormatter.cpp +++ b/src/Formatter/ByteSizeFormatter.cpp @@ -24,8 +24,8 @@ Copyright_License { #include "ByteSizeFormatter.hpp" #include "Math/fixed.hpp" #include "Util/Macros.hpp" +#include "Util/StringUtil.hpp" -#include #include void @@ -52,5 +52,5 @@ FormatByteSize(TCHAR *buffer, size_t size, unsigned long bytes, bool simple) else format = simple ? _T("%.1f%s") : _T("%.2f %s"); - _sntprintf(buffer, size, format, (double)value, unit); + StringFormat(buffer, size, format, (double)value, unit); } diff --git a/src/Formatter/GeoPointFormatter.cpp b/src/Formatter/GeoPointFormatter.cpp index 215903981..93f38e804 100644 --- a/src/Formatter/GeoPointFormatter.cpp +++ b/src/Formatter/GeoPointFormatter.cpp @@ -28,9 +28,6 @@ Copyright_License { #include "Geo/GeoPoint.hpp" #include "Geo/UTM.hpp" -#include -#include - bool FormatLongitude(Angle longitude, TCHAR *buffer, size_t size, CoordinateFormat format) @@ -61,8 +58,8 @@ FormatLongitude(Angle longitude, TCHAR *buffer, size_t size, mm -= 60; } // Save the string to the buffer - _sntprintf(buffer, size, _T("%03d")_T(DEG)_T("%02d'%02d\" %c"), - dd, mm, ss, sign); + StringFormat(buffer, size, _T("%03d")_T(DEG)_T("%02d'%02d\" %c"), + dd, mm, ss, sign); break; case CoordinateFormat::DDMMSS_SS: @@ -74,8 +71,8 @@ FormatLongitude(Angle longitude, TCHAR *buffer, size_t size, // Calculate seconds mlong = (mlong - mm) * 60.0; // Save the string to the buffer - _sntprintf(buffer, size, _T("%03d")_T(DEG)_T("%02d'%05.2f\" %c"), - dd, mm, mlong, sign); + StringFormat(buffer, size, _T("%03d")_T(DEG)_T("%02d'%05.2f\" %c"), + dd, mm, mlong, sign); break; case CoordinateFormat::DDMM_MMM: @@ -84,12 +81,13 @@ FormatLongitude(Angle longitude, TCHAR *buffer, size_t size, // Calculate minutes mlong = (mlong - dd) * 60.0; // Save the string to the buffer - _sntprintf(buffer, size, _T("%03d")_T(DEG)_T("%06.3f' %c"), dd, mlong, sign); + StringFormat(buffer, size, _T("%03d")_T(DEG)_T("%06.3f' %c"), + dd, mlong, sign); break; case CoordinateFormat::DD_DDDD: // Save the string to the buffer - _sntprintf(buffer, size, _T("%08.4f" DEG " %c"), mlong, sign); + StringFormat(buffer, size, _T("%08.4f" DEG " %c"), mlong, sign); break; case CoordinateFormat::UTM: @@ -129,8 +127,8 @@ FormatLatitude(Angle latitude, TCHAR *buffer, size_t size, mm -= 60; } // Save the string to the buffer - _sntprintf(buffer, size, _T("%02d")_T(DEG)_T("%02d'%02d\" %c"), - dd, mm, ss, sign); + StringFormat(buffer, size, _T("%02d")_T(DEG)_T("%02d'%02d\" %c"), + dd, mm, ss, sign); break; case CoordinateFormat::DDMMSS_SS: @@ -142,8 +140,8 @@ FormatLatitude(Angle latitude, TCHAR *buffer, size_t size, // Calculate seconds mlat = (mlat - mm) * 60.0; // Save the string to the buffer - _sntprintf(buffer, size, _T("%02d")_T(DEG)_T("%02d'%05.2f\" %c"), - dd, mm, mlat, sign); + StringFormat(buffer, size, _T("%02d")_T(DEG)_T("%02d'%05.2f\" %c"), + dd, mm, mlat, sign); break; case CoordinateFormat::DDMM_MMM: @@ -152,12 +150,13 @@ FormatLatitude(Angle latitude, TCHAR *buffer, size_t size, // Calculate minutes mlat = (mlat - dd) * 60.0; // Save the string to the buffer - _sntprintf(buffer, size, _T("%02d")_T(DEG)_T("%06.3f' %c"), dd, mlat, sign); + StringFormat(buffer, size, _T("%02d")_T(DEG)_T("%06.3f' %c"), + dd, mlat, sign); break; case CoordinateFormat::DD_DDDD: // Save the string to the buffer - _sntprintf(buffer, size, _T("%07.4f" DEG " %c"), mlat, sign); + StringFormat(buffer, size, _T("%07.4f" DEG " %c"), mlat, sign); break; case CoordinateFormat::UTM: @@ -172,9 +171,10 @@ FormatUTM(const GeoPoint &location, TCHAR *buffer, size_t size, TCHAR seperator = _T(' ')) { UTM utm = UTM::FromGeoPoint(location); - _sntprintf(buffer, size, _T("%u%c%c%.0f%c%.0f"), utm.zone_number, - utm.zone_letter, seperator, (double)utm.easting, seperator, - (double)utm.northing); + StringFormat(buffer, size, _T("%u%c%c%.0f%c%.0f"), + utm.zone_number, utm.zone_letter, seperator, + (double)utm.easting, seperator, + (double)utm.northing); return buffer; } diff --git a/src/Formatter/GlideRatioFormatter.cpp b/src/Formatter/GlideRatioFormatter.cpp index a4b957e27..95dda0b7c 100644 --- a/src/Formatter/GlideRatioFormatter.cpp +++ b/src/Formatter/GlideRatioFormatter.cpp @@ -22,8 +22,7 @@ Copyright_License { */ #include "GlideRatioFormatter.hpp" - -#include +#include "Util/StringUtil.hpp" void FormatGlideRatio(TCHAR *buffer, size_t size, fixed gr) @@ -31,6 +30,6 @@ FormatGlideRatio(TCHAR *buffer, size_t size, fixed gr) assert(buffer != NULL); assert(size >= 8); - _sntprintf(buffer, size, + StringFormat(buffer, size, fabs(gr) < fixed(100) ? _T("%.1f") : _T("%.0f"), (double) gr); } diff --git a/src/Logger/ExternalLogger.cpp b/src/Logger/ExternalLogger.cpp index e30794b6b..4cd8dc98a 100644 --- a/src/Logger/ExternalLogger.cpp +++ b/src/Logger/ExternalLogger.cpp @@ -43,6 +43,7 @@ #include "IGC/IGCHeader.hpp" #include "Formatter/IGCFilenameFormatter.hpp" #include "Time/BrokenDate.hpp" +#include "Util/StringUtil.hpp" #include /* for MAX_PATH */ @@ -243,11 +244,11 @@ ShowFlightList(const RecordedFlightList &flight_list) for (unsigned i = 0; i < flight_list.size(); ++i) { const RecordedFlightInfo &flight = flight_list[i]; - TCHAR buffer[64]; - _sntprintf(buffer, 64, _T("%04u/%02u/%02u %02u:%02u-%02u:%02u"), - flight.date.year, flight.date.month, flight.date.day, - flight.start_time.hour, flight.start_time.minute, - flight.end_time.hour, flight.end_time.minute); + StaticString<64> buffer; + buffer.UnsafeFormat(buffer, _T("%04u/%02u/%02u %02u:%02u-%02u:%02u"), + flight.date.year, flight.date.month, flight.date.day, + flight.start_time.hour, flight.start_time.minute, + flight.end_time.hour, flight.end_time.minute); combo.Append(i, buffer); } diff --git a/src/Profile/DeviceConfig.cpp b/src/Profile/DeviceConfig.cpp index 9192cc146..9cd40fd37 100644 --- a/src/Profile/DeviceConfig.cpp +++ b/src/Profile/DeviceConfig.cpp @@ -144,16 +144,15 @@ DeviceConfig::GetPortName(TCHAR *buffer, size_t max_size) const return path.c_str(); case PortType::RFCOMM: - _sntprintf(buffer, max_size, _T("Bluetooth %s"), - bluetooth_mac.c_str()); + StringFormat(buffer, max_size, _T("Bluetooth %s"), + bluetooth_mac.c_str()); return buffer; case PortType::RFCOMM_SERVER: return _("Bluetooth server"); case PortType::IOIOUART: - _sntprintf(buffer, max_size, _T("IOIO UART %d"), - ioio_uart_id); + StringFormat(buffer, max_size, _T("IOIO UART %d"), ioio_uart_id); return buffer; case PortType::DROIDSOAR_V2: @@ -175,15 +174,15 @@ DeviceConfig::GetPortName(TCHAR *buffer, size_t max_size) const return _("Built-in GPS & sensors"); case PortType::TCP_LISTENER: - _sntprintf(buffer, max_size, _T("TCP port %d"), tcp_port); + StringFormat(buffer, max_size, _T("TCP port %d"), tcp_port); return buffer; case PortType::UDP_LISTENER: - _sntprintf(buffer, max_size, _T("UDP port %d"), tcp_port); + StringFormat(buffer, max_size, _T("UDP port %d"), tcp_port); return buffer; case PortType::PTY: - _sntprintf(buffer, max_size, _T("Pseudo-terminal %s"), path.c_str()); + StringFormat(buffer, max_size, _T("Pseudo-terminal %s"), path.c_str()); return buffer; } diff --git a/src/RadioFrequency.cpp b/src/RadioFrequency.cpp index 59b4b8af2..f60019357 100644 --- a/src/RadioFrequency.cpp +++ b/src/RadioFrequency.cpp @@ -22,9 +22,8 @@ Copyright_License { */ #include "RadioFrequency.hpp" - -#include -#include +#include "Util/StringUtil.hpp" +#include "Util/NumberParser.hpp" TCHAR * RadioFrequency::Format(TCHAR *buffer, size_t max_size) const @@ -36,7 +35,7 @@ RadioFrequency::Format(TCHAR *buffer, size_t max_size) const unsigned mhz = khz / 1000; khz %= 1000; - _sntprintf(buffer, max_size, _T("%u.%03u"), mhz, khz); + StringFormat(buffer, max_size, _T("%u.%03u"), mhz, khz); return buffer; } @@ -44,7 +43,7 @@ RadioFrequency RadioFrequency::Parse(const TCHAR *p) { TCHAR *endptr; - double mhz = _tcstod(p, &endptr); + double mhz = ParseDouble(p, &endptr); RadioFrequency frequency; if (mhz < 100 || *endptr != _T('\0')) diff --git a/src/unix/tchar.h b/src/unix/tchar.h index 26c13a7c4..1986397be 100644 --- a/src/unix/tchar.h +++ b/src/unix/tchar.h @@ -34,7 +34,6 @@ Copyright_License { typedef char TCHAR; #define _stprintf sprintf #define _vstprintf vsprintf -#define _sntprintf snprintf #define _vsntprintf vsnprintf #define _tprintf printf #define _ftprintf fprintf -- 2.11.4.GIT