summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorGordon Sim <gsim@apache.org>2011-04-26 13:42:24 +0000
committerGordon Sim <gsim@apache.org>2011-04-26 13:42:24 +0000
commit19f093d982161547005593e57f47111a4c578667 (patch)
tree01ab339c5ad0ffb9d116a43e4dfb7bc789ae993a /cpp/src
parent680e70ade373cb0b6575f440be57a61ad562b5c3 (diff)
downloadqpid-python-19f093d982161547005593e57f47111a4c578667.tar.gz
QPID-3222: Prevent ttl overflow; also adds equality operators for qpid::messaging::Duration.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1096751 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/qpid/broker/Message.cpp9
-rw-r--r--cpp/src/qpid/messaging/Duration.cpp10
-rw-r--r--cpp/src/tests/MessagingSessionTests.cpp14
3 files changed, 30 insertions, 3 deletions
diff --git a/cpp/src/qpid/broker/Message.cpp b/cpp/src/qpid/broker/Message.cpp
index 812d856416..763dc55e40 100644
--- a/cpp/src/qpid/broker/Message.cpp
+++ b/cpp/src/qpid/broker/Message.cpp
@@ -374,7 +374,8 @@ void Message::setTimestamp(const boost::intrusive_ptr<ExpiryPolicy>& e)
props->setExpiration(now + (props->getTtl()/1000));
}
// Use higher resolution time for the internal expiry calculation.
- expiration = AbsTime(AbsTime::now(), Duration(props->getTtl() * TIME_MSEC));
+ Duration ttl(std::min(props->getTtl() * TIME_MSEC, (uint64_t) std::numeric_limits<int64_t>::max()));//Prevent overflow
+ expiration = AbsTime(AbsTime::now(), ttl);
setExpiryPolicy(e);
}
}
@@ -384,8 +385,10 @@ void Message::adjustTtl()
DeliveryProperties* props = getProperties<DeliveryProperties>();
if (props->getTtl()) {
sys::Mutex::ScopedLock l(lock);
- sys::Duration d(sys::AbsTime::now(), getExpiration());
- props->setTtl(int64_t(d) > 0 ? int64_t(d)/1000000 : 1); // convert from ns to ms; set to 1 if expired
+ if (expiration < FAR_FUTURE) {
+ sys::Duration d(sys::AbsTime::now(), getExpiration());
+ props->setTtl(int64_t(d) > 0 ? int64_t(d)/1000000 : 1); // convert from ns to ms; set to 1 if expired
+ }
}
}
diff --git a/cpp/src/qpid/messaging/Duration.cpp b/cpp/src/qpid/messaging/Duration.cpp
index a2c443c746..a23e9f5bcb 100644
--- a/cpp/src/qpid/messaging/Duration.cpp
+++ b/cpp/src/qpid/messaging/Duration.cpp
@@ -37,6 +37,16 @@ Duration operator*(uint64_t multiplier, const Duration& duration)
return Duration(duration.getMilliseconds() * multiplier);
}
+bool operator==(const Duration& a, const Duration& b)
+{
+ return a.getMilliseconds() == b.getMilliseconds();
+}
+
+bool operator!=(const Duration& a, const Duration& b)
+{
+ return a.getMilliseconds() != b.getMilliseconds();
+}
+
const Duration Duration::FOREVER(std::numeric_limits<uint64_t>::max());
const Duration Duration::IMMEDIATE(0);
const Duration Duration::SECOND(1000);
diff --git a/cpp/src/tests/MessagingSessionTests.cpp b/cpp/src/tests/MessagingSessionTests.cpp
index 20ed16dece..6aa4c63ed7 100644
--- a/cpp/src/tests/MessagingSessionTests.cpp
+++ b/cpp/src/tests/MessagingSessionTests.cpp
@@ -978,6 +978,20 @@ QPID_AUTO_TEST_CASE(testRejectAndCredit)
sender.close();
}
+QPID_AUTO_TEST_CASE(testTtlForever)
+{
+ QueueFixture fix;
+ Sender sender = fix.session.createSender(fix.queue);
+ Message out("I want to live forever!");
+ out.setTtl(Duration::FOREVER);
+ sender.send(out, true);
+ Receiver receiver = fix.session.createReceiver(fix.queue);
+ Message in = receiver.fetch(Duration::IMMEDIATE);
+ fix.session.acknowledge();
+ BOOST_CHECK_EQUAL(in.getContent(), out.getContent());
+ BOOST_CHECK(in.getTtl() == Duration::FOREVER);
+}
+
QPID_AUTO_TEST_SUITE_END()
}} // namespace qpid::tests