Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/cpp/src/thrift/transport/THeaderTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TCompactProtocol.h>

#include <boost/numeric/conversion/cast.hpp>

#include <limits>
#include <utility>
#include <string>
Expand All @@ -40,6 +42,20 @@ using std::shared_ptr;

namespace transport {

/**
* Legacy code in transport implementations have overflow issues
* that need to be enforced.
*/
template <typename To, typename From> To safe_numeric_cast(From i) {
try {
return boost::numeric_cast<To>(i);
}
catch (const std::bad_cast& bc) {
throw TTransportException(TTransportException::CORRUPTED_DATA,
bc.what());
}
}

using namespace apache::thrift::protocol;
using apache::thrift::protocol::TBinaryProtocol;

Expand Down Expand Up @@ -607,6 +623,11 @@ uint32_t THeaderTransport::writeVarint32(int32_t n, uint8_t* pkt) {
uint32_t THeaderTransport::writeVarint16(int16_t n, uint8_t* pkt) {
return writeVarint32(n, pkt);
}

uint16_t THeaderTransport::getNumTransforms() const {
return safe_numeric_cast<uint16_t>(writeTrans_.size());
}

}
}
} // apache::thrift::transport
6 changes: 2 additions & 4 deletions lib/cpp/src/thrift/transport/THeaderTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class THeaderTransport : public TVirtualTransport<THeaderTransport, TFramedTrans
static const int THRIFT_MAX_VARINT32_BYTES = 5;

/// Use default buffer sizes.
explicit THeaderTransport(const std::shared_ptr<TTransport>& transport,
explicit THeaderTransport(const std::shared_ptr<TTransport>& transport,
std::shared_ptr<TConfiguration> config = nullptr)
: TVirtualTransport(transport, config),
outTransport_(transport),
Expand Down Expand Up @@ -140,9 +140,7 @@ class THeaderTransport : public TVirtualTransport<THeaderTransport, TFramedTrans
*/
void transform(uint8_t* ptr, uint32_t sz);

uint16_t getNumTransforms() const {
return safe_numeric_cast<uint16_t>(writeTrans_.size());
}
uint16_t getNumTransforms() const;

void setTransform(uint16_t transId) { writeTrans_.push_back(transId); }

Expand Down
15 changes: 0 additions & 15 deletions lib/cpp/src/thrift/transport/TTransportException.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_
#define _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ 1

#include <boost/numeric/conversion/cast.hpp>
#include <string>
#include <thrift/Thrift.h>

Expand Down Expand Up @@ -86,20 +85,6 @@ class TTransportException : public apache::thrift::TException {
TTransportExceptionType type_;
};

/**
* Legacy code in transport implementations have overflow issues
* that need to be enforced.
*/
template <typename To, typename From> To safe_numeric_cast(From i) {
try {
return boost::numeric_cast<To>(i);
}
catch (const std::bad_cast& bc) {
throw TTransportException(TTransportException::CORRUPTED_DATA,
bc.what());
}
}

}
}
} // apache::thrift::transport
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/src/TestServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class TestHandler : public ThriftTestIf {
void testBinary(std::string& _return, const std::string& thing) override {
std::ostringstream hexstr;
hexstr << std::hex << thing;
printf("testBinary(%lu: %s)\n", safe_numeric_cast<unsigned long>(thing.size()), hexstr.str().c_str());
printf("testBinary(%lu: %s)\n", static_cast<unsigned long>(thing.size()), hexstr.str().c_str());
_return = thing;
}

Expand Down
Loading