Ashim Kapoor: handle %i,%f,%d,%x,%X better (#694)

This commit is contained in:
Daniel Marjamäki 2009-09-26 16:19:18 +02:00
parent a919f4541a
commit 3da779725d
2 changed files with 17 additions and 10 deletions

View File

@ -756,6 +756,7 @@ int CheckBufferOverrun::count(const std::string &input_string)
int on_on_next = 0; int on_on_next = 0;
std::string digits_string = ""; std::string digits_string = "";
int digits = 0; int digits = 0;
int check_for_i_d_x_f = 0;
for (std::string::size_type i = 0; i != input_string.size(); i++) for (std::string::size_type i = 0; i != input_string.size(); i++)
{ {
@ -767,18 +768,20 @@ int CheckBufferOverrun::count(const std::string &input_string)
} }
switch (input_string[i]) switch (input_string[i])
{ {
case 'f':
case 'x':
case 'X':
case 'd': case 'd':
case 'i': case 'i':
check_for_i_d_x_f = 1;
case 'c': case 'c':
case 'e': case 'e':
case 'E': case 'E':
case 'f':
case 'g': case 'g':
case 'o': case 'o':
case 's': case 's':
case 'u': case 'u':
case 'x':
case 'X':
case 'p': case 'p':
case 'n': case 'n':
if (flag == 0) on_on_next = 1; if (flag == 0) on_on_next = 1;
@ -797,13 +800,17 @@ int CheckBufferOverrun::count(const std::string &input_string)
if (on_on_next == 1 && flag == 0) if (on_on_next == 1 && flag == 0)
{ {
std::cout << digits_string;
digits_string = digits_string.substr(1, digits_string.size()); digits_string = digits_string.substr(1, digits_string.size());
digits += abs(atoi(digits_string.c_str())); if (check_for_i_d_x_f == 1) digits += std::max(abs(atoi(digits_string.c_str())), 1);
else
digits += abs(atoi(digits_string.c_str()));
digits_string = ""; digits_string = "";
check_for_i_d_x_f = 0;
} }
} }
@ -811,5 +818,3 @@ int CheckBufferOverrun::count(const std::string &input_string)
} }

View File

@ -790,10 +790,14 @@ private:
std::string str1 = "Hello"; std::string str1 = "Hello";
std::string str2 = "Hello %2.2d %1d"; std::string str2 = "Hello %2.2d %1d";
std::string str3 = "Test \" "; std::string str3 = "Test \" ";
std::string str4 = "Try %s";
std::string str5 = "%d";
ASSERT_EQUALS(6, CheckBufferOverrun::count(str1)); ASSERT_EQUALS(6, CheckBufferOverrun::count(str1));
ASSERT_EQUALS(11, CheckBufferOverrun::count(str2)); ASSERT_EQUALS(11, CheckBufferOverrun::count(str2));
ASSERT_EQUALS(8, CheckBufferOverrun::count(str3)); ASSERT_EQUALS(8, CheckBufferOverrun::count(str3));
ASSERT_EQUALS(5, CheckBufferOverrun::count(str4));
ASSERT_EQUALS(2, CheckBufferOverrun::count(str5));
} }
void strncpy1() void strncpy1()
@ -844,5 +848,3 @@ private:
REGISTER_TEST(TestBufferOverrun) REGISTER_TEST(TestBufferOverrun)