Commit a51ae10b8ddada900c1abacd6284d35f6e65aa08
1 parent
66c3c8fd
Remove all calls to sprintf
Showing
6 changed files
with
43 additions
and
22 deletions
ChangeLog
| 1 | +2013-02-28 Jay Berkenbilt <ejb@ql.org> | ||
| 2 | + | ||
| 3 | + * Remove all calls to sprintf | ||
| 4 | + | ||
| 5 | + * New method QUtil::int_to_string_base to convert to octal or | ||
| 6 | + hexademical (or decimal) strings without using sprintf | ||
| 7 | + | ||
| 1 | 2013-02-26 Jay Berkenbilt <ejb@ql.org> | 8 | 2013-02-26 Jay Berkenbilt <ejb@ql.org> |
| 2 | 9 | ||
| 3 | * Rewrite QUtil::int_to_string and QUtil::double_to_string to | 10 | * Rewrite QUtil::int_to_string and QUtil::double_to_string to |
include/qpdf/QUtil.hh
| @@ -22,6 +22,8 @@ namespace QUtil | @@ -22,6 +22,8 @@ namespace QUtil | ||
| 22 | QPDF_DLL | 22 | QPDF_DLL |
| 23 | std::string int_to_string(long long, int length = 0); | 23 | std::string int_to_string(long long, int length = 0); |
| 24 | QPDF_DLL | 24 | QPDF_DLL |
| 25 | + std::string int_to_string_base(long long, int base, int length = 0); | ||
| 26 | + QPDF_DLL | ||
| 25 | std::string double_to_string(double, int decimal_places = 0); | 27 | std::string double_to_string(double, int decimal_places = 0); |
| 26 | 28 | ||
| 27 | QPDF_DLL | 29 | QPDF_DLL |
libqpdf/QPDF_String.cc
| @@ -95,7 +95,6 @@ QPDF_String::unparse(bool force_binary) | @@ -95,7 +95,6 @@ QPDF_String::unparse(bool force_binary) | ||
| 95 | else | 95 | else |
| 96 | { | 96 | { |
| 97 | result += "("; | 97 | result += "("; |
| 98 | - char num[5]; | ||
| 99 | for (unsigned int i = 0; i < this->val.length(); ++i) | 98 | for (unsigned int i = 0; i < this->val.length(); ++i) |
| 100 | { | 99 | { |
| 101 | char ch = this->val[i]; | 100 | char ch = this->val[i]; |
| @@ -140,8 +139,9 @@ QPDF_String::unparse(bool force_binary) | @@ -140,8 +139,9 @@ QPDF_String::unparse(bool force_binary) | ||
| 140 | } | 139 | } |
| 141 | else | 140 | else |
| 142 | { | 141 | { |
| 143 | - sprintf(num, "\\%03o", static_cast<unsigned char>(ch)); // XXXX | ||
| 144 | - result += num; | 142 | + result += "\\" + QUtil::int_to_string_base( |
| 143 | + static_cast<int>(static_cast<unsigned char>(ch)), | ||
| 144 | + 8, 3); | ||
| 145 | } | 145 | } |
| 146 | break; | 146 | break; |
| 147 | } | 147 | } |
libqpdf/QUtil.cc
| @@ -24,11 +24,23 @@ | @@ -24,11 +24,23 @@ | ||
| 24 | std::string | 24 | std::string |
| 25 | QUtil::int_to_string(long long num, int length) | 25 | QUtil::int_to_string(long long num, int length) |
| 26 | { | 26 | { |
| 27 | - // Backward compatibility -- this function used to use sprintf | ||
| 28 | - // with %0*d, so we interpret length such that a negative value | ||
| 29 | - // appends spaces and a positive value prepends zeroes. | 27 | + return int_to_string_base(num, 10, length); |
| 28 | +} | ||
| 29 | + | ||
| 30 | +std::string | ||
| 31 | +QUtil::int_to_string_base(long long num, int base, int length) | ||
| 32 | +{ | ||
| 33 | + // Backward compatibility -- int_to_string, which calls this | ||
| 34 | + // function, used to use sprintf with %0*d, so we interpret length | ||
| 35 | + // such that a negative value appends spaces and a positive value | ||
| 36 | + // prepends zeroes. | ||
| 37 | + if (! ((base == 8) || (base == 10) || (base == 16))) | ||
| 38 | + { | ||
| 39 | + throw std::logic_error( | ||
| 40 | + "int_to_string_base called with unsupported base"); | ||
| 41 | + } | ||
| 30 | std::ostringstream buf; | 42 | std::ostringstream buf; |
| 31 | - buf << num; | 43 | + buf << std::setbase(base) << std::nouppercase << num; |
| 32 | std::string result; | 44 | std::string result; |
| 33 | if ((length > 0) && | 45 | if ((length > 0) && |
| 34 | (buf.str().length() < static_cast<size_t>(length))) | 46 | (buf.str().length() < static_cast<size_t>(length))) |
| @@ -152,16 +164,13 @@ QUtil::copy_string(std::string const& str) | @@ -152,16 +164,13 @@ QUtil::copy_string(std::string const& str) | ||
| 152 | std::string | 164 | std::string |
| 153 | QUtil::hex_encode(std::string const& input) | 165 | QUtil::hex_encode(std::string const& input) |
| 154 | { | 166 | { |
| 155 | - size_t input_size = input.length(); | ||
| 156 | - size_t hex_size = 1 + (2 * input_size); | ||
| 157 | - PointerHolder<char> bufp(true, new char[hex_size]); | ||
| 158 | - char* buf = bufp.getPointer(); | ||
| 159 | - buf[hex_size - 1] = '\0'; | ||
| 160 | - for (unsigned int i = 0; i < input_size; ++i) | 167 | + std::string result; |
| 168 | + for (unsigned int i = 0; i < input.length(); ++i) | ||
| 161 | { | 169 | { |
| 162 | - sprintf(buf + i * 2, "%02x", static_cast<unsigned char>(input[i])); // XXXX | 170 | + result += QUtil::int_to_string_base( |
| 171 | + static_cast<int>(static_cast<unsigned char>(input[i])), 16, 2); | ||
| 163 | } | 172 | } |
| 164 | - return buf; | 173 | + return result; |
| 165 | } | 174 | } |
| 166 | 175 | ||
| 167 | void | 176 | void |
libtests/qtest/qutil/qutil.out
libtests/qutil.cc
| @@ -23,7 +23,10 @@ void string_conversion_test() | @@ -23,7 +23,10 @@ void string_conversion_test() | ||
| 23 | << QUtil::double_to_string(.1234, 5) << std::endl | 23 | << QUtil::double_to_string(.1234, 5) << std::endl |
| 24 | << QUtil::double_to_string(.0001234, 5) << std::endl | 24 | << QUtil::double_to_string(.0001234, 5) << std::endl |
| 25 | << QUtil::double_to_string(.123456, 5) << std::endl | 25 | << QUtil::double_to_string(.123456, 5) << std::endl |
| 26 | - << QUtil::double_to_string(.000123456, 5) << std::endl; | 26 | + << QUtil::double_to_string(.000123456, 5) << std::endl |
| 27 | + << QUtil::int_to_string_base(16059, 10) << std::endl | ||
| 28 | + << QUtil::int_to_string_base(16059, 8) << std::endl | ||
| 29 | + << QUtil::int_to_string_base(16059, 16) << std::endl; | ||
| 27 | 30 | ||
| 28 | std::string embedded_null = "one"; | 31 | std::string embedded_null = "one"; |
| 29 | embedded_null += '\0'; | 32 | embedded_null += '\0'; |
| @@ -86,10 +89,8 @@ void getenv_test() | @@ -86,10 +89,8 @@ void getenv_test() | ||
| 86 | 89 | ||
| 87 | static void print_utf8(unsigned long val) | 90 | static void print_utf8(unsigned long val) |
| 88 | { | 91 | { |
| 89 | - char t[20]; | ||
| 90 | - sprintf(t, "%lx", val); // XXXX | ||
| 91 | std::string result = QUtil::toUTF8(val); | 92 | std::string result = QUtil::toUTF8(val); |
| 92 | - std::cout << "0x" << t << " ->"; | 93 | + std::cout << "0x" << QUtil::int_to_string_base(val, 16) << " ->"; |
| 93 | if (val < 0xfffe) | 94 | if (val < 0xfffe) |
| 94 | { | 95 | { |
| 95 | std::cout << " " << result; | 96 | std::cout << " " << result; |
| @@ -102,9 +103,8 @@ static void print_utf8(unsigned long val) | @@ -102,9 +103,8 @@ static void print_utf8(unsigned long val) | ||
| 102 | for (std::string::iterator iter = result.begin(); | 103 | for (std::string::iterator iter = result.begin(); |
| 103 | iter != result.end(); ++iter) | 104 | iter != result.end(); ++iter) |
| 104 | { | 105 | { |
| 105 | - char t[3]; | ||
| 106 | - sprintf(t, "%02x", static_cast<unsigned char>(*iter)); // XXXX | ||
| 107 | - std::cout << " " << t; | 106 | + std::cout << " " << QUtil::int_to_string_base( |
| 107 | + static_cast<int>(static_cast<unsigned char>(*iter)), 16, 2); | ||
| 108 | } | 108 | } |
| 109 | } | 109 | } |
| 110 | std::cout << std::endl; | 110 | std::cout << std::endl; |