Skip to content

Commit

Permalink
check return code from std::snprintf, save/restore DoubleFormat for t…
Browse files Browse the repository at this point in the history
…est.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>
  • Loading branch information
fujitatomoya committed Aug 4, 2020
1 parent dea37d4 commit 5ee3a8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
28 changes: 17 additions & 11 deletions utilities/xmlrpcpp/src/XmlRpcValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,28 +615,34 @@ namespace XmlRpc {
int required_size;
char buf[128]; // Should be long enough
required_size = std::snprintf(buf, sizeof(buf)-1,
getDoubleFormat().c_str(), _value.asDouble);
if (required_size < (int) sizeof(buf)) {
getDoubleFormat().c_str(), _value.asDouble);
if (required_size >= 0 && required_size < (int) sizeof(buf)) {
buf[sizeof(buf)-1] = 0;
os << buf;
} else {
} else if (required_size >= (int) sizeof(buf)) {
int ret;
char required_buf[required_size+1];
std::snprintf(required_buf, required_size,
getDoubleFormat().c_str(), _value.asDouble);
required_buf[required_size] = 0;
os << required_buf;
ret = std::snprintf(required_buf, required_size,
getDoubleFormat().c_str(), _value.asDouble);
if (ret == required_size) {
required_buf[required_size] = 0;
os << required_buf;
}
}
break;
}
case TypeString: os << *_value.asString; break;
case TypeDateTime:
{
struct tm* t = _value.asTime;
int ret;
char buf[20];
std::snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d",
t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
buf[sizeof(buf)-1] = 0;
os << buf;
ret = std::snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d",
t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
if (ret > 0) {
buf[sizeof(buf)-1] = 0;
os << buf;
}
break;
}
case TypeBase64:
Expand Down
19 changes: 19 additions & 0 deletions utilities/xmlrpcpp/test/TestValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ TEST(XmlRpc, testDouble) {
// Test format
const XmlRpc::XmlRpcValue a(2.0);
ASSERT_EQ(XmlRpcValue::TypeDouble, d.getType());
const std::string save_format = XmlRpc::XmlRpcValue::getDoubleFormat();

XmlRpc::XmlRpcValue::setDoubleFormat("%32.10f");
ss << a;
Expand All @@ -141,6 +142,24 @@ TEST(XmlRpc, testDouble) {
ss << a;
EXPECT_EQ("2.00000000000000000000000000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat("%128.10f");
ss << a;
EXPECT_EQ(" "
" "
" "
" 2.000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat("%10.128f");
ss << a;
EXPECT_EQ("2.000000000000000000000000000000"
"00000000000000000000000000000000"
"00000000000000000000000000000000"
"000000000000000000000000000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat(save_format.c_str());
}

TEST(XmlRpc, testString) {
Expand Down

0 comments on commit 5ee3a8d

Please sign in to comment.