Skip to content

Commit 7c94335

Browse files
bandleJens-G
authored andcommitted
THRIFT-5853: Remove oldstyle casts from TBufferTransports and TCompactProtocol
Client: cpp Removes all oldstyle casts from the library parts needed to parse with TCompactProtocol in memory, like when using thrift for parquet. Thus, it is now possible to compile it with -Wno-old-style-casts
1 parent 09ced04 commit 7c94335

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

lib/cpp/src/thrift/protocol/TCompactProtocol.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ namespace protocol {
3535
template <class Transport_>
3636
class TCompactProtocolT : public TVirtualProtocol<TCompactProtocolT<Transport_> > {
3737
public:
38-
static const int8_t PROTOCOL_ID = (int8_t)0x82u;
38+
static const int8_t PROTOCOL_ID = static_cast<int8_t>(0x82u);
3939
static const int8_t VERSION_N = 1;
4040
static const int8_t VERSION_MASK = 0x1f; // 0001 1111
4141

4242
protected:
43-
static const int8_t TYPE_MASK = (int8_t)0xE0u; // 1110 0000
43+
static const int8_t TYPE_MASK = static_cast<int8_t>(0xE0u); // 1110 0000
4444
static const int8_t TYPE_BITS = 0x07; // 0000 0111
4545
static const int32_t TYPE_SHIFT_AMOUNT = 5;
4646

lib/cpp/src/thrift/protocol/TCompactProtocol.tcc

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
9393
const int32_t seqid) {
9494
uint32_t wsize = 0;
9595
wsize += writeByte(PROTOCOL_ID);
96-
wsize += writeByte((VERSION_N & VERSION_MASK) | (((int32_t)messageType << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
96+
wsize += writeByte((VERSION_N & VERSION_MASK) | ((static_cast<int32_t>(messageType) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
9797
wsize += writeVarint32(seqid);
9898
wsize += writeString(name);
9999
return wsize;
@@ -221,7 +221,7 @@ uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
221221

222222
template <class Transport_>
223223
uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
224-
trans_->write((uint8_t*)&byte, 1);
224+
trans_->write(reinterpret_cast<const uint8_t*>(&byte), 1);
225225
return 1;
226226
}
227227

@@ -259,7 +259,7 @@ uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
259259

260260
auto bits = bitwise_cast<uint64_t>(dub);
261261
bits = THRIFT_htolell(bits);
262-
trans_->write((uint8_t*)&bits, 8);
262+
trans_->write(reinterpret_cast<const uint8_t*>(&bits), 8);
263263
return 8;
264264
}
265265

@@ -282,7 +282,7 @@ uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
282282
if(ssize > (std::numeric_limits<uint32_t>::max)() - wsize)
283283
throw TProtocolException(TProtocolException::SIZE_LIMIT);
284284
wsize += ssize;
285-
trans_->write((uint8_t*)str.data(), ssize);
285+
trans_->write(reinterpret_cast<const uint8_t*>(str.data()), ssize);
286286
return wsize;
287287
}
288288

@@ -350,10 +350,10 @@ uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
350350

351351
while (true) {
352352
if ((n & ~0x7F) == 0) {
353-
buf[wsize++] = (int8_t)n;
353+
buf[wsize++] = static_cast<int8_t>(n);
354354
break;
355355
} else {
356-
buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
356+
buf[wsize++] = static_cast<int8_t>((n & 0x7F) | 0x80);
357357
n >>= 7;
358358
}
359359
}
@@ -371,10 +371,10 @@ uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
371371

372372
while (true) {
373373
if ((n & ~0x7FL) == 0) {
374-
buf[wsize++] = (int8_t)n;
374+
buf[wsize++] = static_cast<int8_t>(n);
375375
break;
376376
} else {
377-
buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
377+
buf[wsize++] = static_cast<int8_t>((n & 0x7F) | 0x80);
378378
n >>= 7;
379379
}
380380
}
@@ -431,12 +431,12 @@ uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
431431
}
432432

433433
rsize += readByte(versionAndType);
434-
version = (int8_t)(versionAndType & VERSION_MASK);
434+
version = static_cast<int8_t>(versionAndType & VERSION_MASK);
435435
if (version != VERSION_N) {
436436
throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version");
437437
}
438438

439-
messageType = (TMessageType)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
439+
messageType = static_cast<TMessageType>((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
440440
rsize += readVarint32(seqid);
441441
rsize += readString(name);
442442

@@ -489,12 +489,12 @@ uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
489489
}
490490

491491
// mask off the 4 MSB of the type header. it could contain a field id delta.
492-
auto modifier = (int16_t)(((uint8_t)byte & 0xf0) >> 4);
492+
auto modifier = static_cast<int16_t>(static_cast<uint8_t>(byte & 0xf0) >> 4);
493493
if (modifier == 0) {
494494
// not a delta, look ahead for the zigzag varint field id.
495495
rsize += readI16(fieldId);
496496
} else {
497-
fieldId = (int16_t)(lastFieldId_ + modifier);
497+
fieldId = static_cast<int16_t>(lastFieldId_ + modifier);
498498
}
499499
fieldType = getTType(type);
500500

@@ -535,9 +535,9 @@ uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
535535
throw TProtocolException(TProtocolException::SIZE_LIMIT);
536536
}
537537

538-
keyType = getTType((int8_t)((uint8_t)kvType >> 4));
539-
valType = getTType((int8_t)((uint8_t)kvType & 0xf));
540-
size = (uint32_t)msize;
538+
keyType = getTType(static_cast<int8_t>(static_cast<uint8_t>(kvType) >> 4));
539+
valType = getTType(static_cast<int8_t>(static_cast<uint8_t>(kvType) & 0xf));
540+
size = static_cast<uint32_t>(msize);
541541

542542
TMap map(keyType, valType, size);
543543
checkReadBytesAvailable(map);
@@ -560,7 +560,7 @@ uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
560560

561561
rsize += readByte(size_and_type);
562562

563-
lsize = ((uint8_t)size_and_type >> 4) & 0x0f;
563+
lsize = (static_cast<uint8_t>(size_and_type) >> 4) & 0x0f;
564564
if (lsize == 15) {
565565
rsize += readVarint32(lsize);
566566
}
@@ -571,8 +571,8 @@ uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
571571
throw TProtocolException(TProtocolException::SIZE_LIMIT);
572572
}
573573

574-
elemType = getTType((int8_t)(size_and_type & 0x0f));
575-
size = (uint32_t)lsize;
574+
elemType = getTType(static_cast<int8_t>(size_and_type & 0x0f));
575+
size = static_cast<uint32_t>(lsize);
576576

577577
TList list(elemType, size);
578578
checkReadBytesAvailable(list);
@@ -618,7 +618,7 @@ template <class Transport_>
618618
uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
619619
uint8_t b[1];
620620
trans_->readAll(b, 1);
621-
byte = *(int8_t*)b;
621+
byte = static_cast<int8_t>(b[0]);
622622
return 1;
623623
}
624624

@@ -629,7 +629,7 @@ template <class Transport_>
629629
uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
630630
int32_t value;
631631
uint32_t rsize = readVarint32(value);
632-
i16 = (int16_t)zigzagToI32(value);
632+
i16 = static_cast<int16_t>(zigzagToI32(value));
633633
return rsize;
634634
}
635635

@@ -702,21 +702,21 @@ uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
702702
}
703703

704704
// Check against MaxMessageSize before alloc
705-
trans_->checkReadBytesAvailable((uint32_t)size);
705+
trans_->checkReadBytesAvailable(static_cast<uint32_t>(size));
706706

707707
// Use the heap here to prevent stack overflow for v. large strings
708708
if (size > string_buf_size_ || string_buf_ == nullptr) {
709-
void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
709+
void* new_string_buf = std::realloc(string_buf_, static_cast<uint32_t>(size));
710710
if (new_string_buf == nullptr) {
711711
throw std::bad_alloc();
712712
}
713-
string_buf_ = (uint8_t*)new_string_buf;
713+
string_buf_ = static_cast<uint8_t*>(new_string_buf);
714714
string_buf_size_ = size;
715715
}
716716
trans_->readAll(string_buf_, size);
717-
str.assign((char*)string_buf_, size);
717+
str.assign(reinterpret_cast<char*>(string_buf_), size);
718718

719-
return rsize + (uint32_t)size;
719+
return rsize + static_cast<uint32_t>(size);
720720
}
721721

722722
/**
@@ -727,7 +727,7 @@ template <class Transport_>
727727
uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
728728
int64_t val;
729729
uint32_t rsize = readVarint64(val);
730-
i32 = (int32_t)val;
730+
i32 = static_cast<int32_t>(val);
731731
return rsize;
732732
}
733733

@@ -749,7 +749,7 @@ uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
749749
while (true) {
750750
uint8_t byte = borrowed[rsize];
751751
rsize++;
752-
val |= (uint64_t)(byte & 0x7f) << shift;
752+
val |= static_cast<uint64_t>(byte & 0x7f) << shift;
753753
shift += 7;
754754
if (!(byte & 0x80)) {
755755
i64 = val;
@@ -768,7 +768,7 @@ uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
768768
while (true) {
769769
uint8_t byte;
770770
rsize += trans_->readAll(&byte, 1);
771-
val |= (uint64_t)(byte & 0x7f) << shift;
771+
val |= static_cast<uint64_t>(byte & 0x7f) << shift;
772772
shift += 7;
773773
if (!(byte & 0x80)) {
774774
i64 = val;
@@ -827,7 +827,7 @@ TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
827827
case detail::compact::CT_STRUCT:
828828
return T_STRUCT;
829829
default:
830-
throw TException(std::string("don't know what type: ") + (char)type);
830+
throw TException(std::string("don't know what type: ") + static_cast<char>(type));
831831
}
832832
}
833833

lib/cpp/src/thrift/protocol/TProtocol.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ static inline To bitwise_cast(From from) {
170170
| (((n) & 0x0000ff00ul) << 8) \
171171
| (((n) & 0x000000fful) << 24) )
172172
# define bswap_16(n) \
173-
( (((n) & ((unsigned short)0xff00ul)) >> 8) \
174-
| (((n) & ((unsigned short)0x00fful)) << 8) )
173+
( (((n) & (static_cast<unsigned short>(0xff00ul)) >> 8) \
174+
| (((n) & (static_cast<unsigned short>(0x00fful)) << 8) )
175175
# define THRIFT_htolell(n) bswap_64(n)
176176
# define THRIFT_letohll(n) bswap_64(n)
177177
# define THRIFT_htolel(n) bswap_32(n)
@@ -191,11 +191,11 @@ static inline To bitwise_cast(From from) {
191191
# define THRIFT_ntohll(n) bswap_64(n)
192192
# define THRIFT_htonll(n) bswap_64(n)
193193
# elif defined(_MSC_VER) /* Microsoft Visual C++ */
194-
# define THRIFT_ntohll(n) ( _byteswap_uint64((uint64_t)n) )
195-
# define THRIFT_htonll(n) ( _byteswap_uint64((uint64_t)n) )
194+
# define THRIFT_ntohll(n) ( _byteswap_uint64(static_cast<uint64_t>(n)) )
195+
# define THRIFT_htonll(n) ( _byteswap_uint64(static_cast<uint64_t>(n)) )
196196
# elif !defined(THRIFT_ntohll) /* Not GNUC/GLIBC or MSVC */
197-
# define THRIFT_ntohll(n) ( (((uint64_t)ntohl((uint32_t)n)) << 32) + ntohl((uint32_t)(n >> 32)) )
198-
# define THRIFT_htonll(n) ( (((uint64_t)htonl((uint32_t)n)) << 32) + htonl((uint32_t)(n >> 32)) )
197+
# define THRIFT_ntohll(n) ( (static_cast<uint64_t>(ntohl(static_cast<uint32_t>(n))) << 32) + ntohl(static_cast<uint32_t>(n >> 32)) )
198+
# define THRIFT_htonll(n) ( (static_cast<uint64_t>(htonl(static_cast<uint32_t>(n))) << 32) + htonl(static_cast<uint32_t>(n >> 32)) )
199199
# endif /* GNUC/GLIBC or MSVC or something else */
200200
#else /* __THRIFT_BYTE_ORDER */
201201
# error "Can't define THRIFT_htonll or THRIFT_ntohll!"

lib/cpp/src/thrift/transport/TBufferTransports.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ void TFramedTransport::flush() {
256256

257257
// Slip the frame size into the start of the buffer.
258258
sz_hbo = static_cast<uint32_t>(wBase_ - (wBuf_.get() + sizeof(sz_nbo)));
259-
sz_nbo = (int32_t)htonl((uint32_t)(sz_hbo));
260-
memcpy(wBuf_.get(), (uint8_t*)&sz_nbo, sizeof(sz_nbo));
259+
sz_nbo = static_cast<int32_t>(htonl(static_cast<uint32_t>(sz_hbo)));
260+
memcpy(wBuf_.get(), reinterpret_cast<uint8_t*>(&sz_nbo), sizeof(sz_nbo));
261261

262262
if (sz_hbo > 0) {
263263
// Note that we reset wBase_ (with a pad for the frame size)
@@ -347,7 +347,7 @@ uint32_t TMemoryBuffer::readAppendToString(std::string& str, uint32_t len) {
347347
computeRead(len, &start, &give);
348348

349349
// Append to the provided string.
350-
str.append((char*)start, give);
350+
str.append(reinterpret_cast<char*>(start), give);
351351

352352
return give;
353353
}

lib/cpp/src/thrift/transport/TBufferTransports.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class TFramedTransport : public TVirtualTransport<TFramedTransport, TBufferBase>
419419

420420
// Pad the buffer so we can insert the size later.
421421
int32_t pad = 0;
422-
this->write((uint8_t*)&pad, sizeof(pad));
422+
this->write(reinterpret_cast<uint8_t*>(&pad), sizeof(pad));
423423
}
424424

425425
std::shared_ptr<TTransport> transport_;
@@ -468,7 +468,7 @@ class TMemoryBuffer : public TVirtualTransport<TMemoryBuffer, TBufferBase> {
468468

469469
if (buf == nullptr && size != 0) {
470470
assert(owner);
471-
buf = (uint8_t*)std::malloc(size);
471+
buf = static_cast<uint8_t*>(std::malloc(size));
472472
if (buf == nullptr) {
473473
throw std::bad_alloc();
474474
}
@@ -593,7 +593,7 @@ class TMemoryBuffer : public TVirtualTransport<TMemoryBuffer, TBufferBase> {
593593
uint8_t* buf;
594594
uint32_t sz;
595595
getBuffer(&buf, &sz);
596-
return std::string((char*)buf, (std::string::size_type)sz);
596+
return {reinterpret_cast<char*>(buf), static_cast<std::string::size_type>(sz)};
597597
}
598598

599599
void appendBufferToString(std::string& str) {
@@ -603,7 +603,7 @@ class TMemoryBuffer : public TVirtualTransport<TMemoryBuffer, TBufferBase> {
603603
uint8_t* buf;
604604
uint32_t sz;
605605
getBuffer(&buf, &sz);
606-
str.append((char*)buf, sz);
606+
str.append(reinterpret_cast<char*>(buf), sz);
607607
}
608608

609609
void resetBuffer() {

0 commit comments

Comments
 (0)