#6037 Slightly improve error checking in vformat()

This commit is contained in:
Andrew Nelless 2017-05-12 00:45:59 +01:00
parent 0290583ab9
commit 46231cd35c
1 changed files with 10 additions and 8 deletions

View File

@ -30,6 +30,7 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <algorithm> #include <algorithm>
#include <cerrno>
namespace synergy { namespace synergy {
namespace string { namespace string {
@ -50,8 +51,8 @@ vformat(const char* fmt, va_list args)
// find highest indexed substitution and the locations of substitutions // find highest indexed substitution and the locations of substitutions
std::vector<size_t> pos; std::vector<size_t> pos;
std::vector<size_t> width; std::vector<size_t> width;
std::vector<int> index; std::vector<size_t> index;
int maxIndex = 0; size_t maxIndex = 0;
for (const char* scan = fmt; *scan != '\0'; ++scan) { for (const char* scan = fmt; *scan != '\0'; ++scan) {
if (*scan == '%') { if (*scan == '%') {
++scan; ++scan;
@ -61,21 +62,22 @@ vformat(const char* fmt, va_list args)
else if (*scan == '%') { else if (*scan == '%') {
// literal // literal
index.push_back(0); index.push_back(0);
pos.push_back(static_cast<int>(scan - 1 - fmt)); pos.push_back(static_cast<size_t>((scan - 1) - fmt));
width.push_back(2); width.push_back(2);
} }
else if (*scan == '{') { else if (*scan == '{') {
// get argument index // get argument index
char* end; char* end;
int i = static_cast<int>(strtol(scan + 1, &end, 10)); errno = 0;
if (*end != '}') { long i = strtol(scan + 1, &end, 10);
if (errno || (i < 0) || (*end != '}')) {
// invalid index -- ignore // invalid index -- ignore
scan = end - 1; scan = end - 1; // BUG if there are digits?
} }
else { else {
index.push_back(i); index.push_back(i);
pos.push_back(static_cast<int>(scan - 1 - fmt)); pos.push_back(static_cast<size_t>((scan - 1) - fmt));
width.push_back(static_cast<int>(end - scan + 2)); width.push_back(static_cast<size_t>((end - scan) + 2));
if (i > maxIndex) { if (i > maxIndex) {
maxIndex = i; maxIndex = i;
} }