Skip to content

Commit 030f6d1

Browse files
committed
Symbol.cc and FileStream.cc: Fix -Wconversion and -Wuseless-cast on 32-bit builds
Symbol.cc: it - rs.begin() returns a value of type std::ptrdiff_t. On 32-bit systems, this is typically int, so casting it to int is redundant and triggers -Wuseless-cast. FileStream.cc: size_t is 32-bit on 32-bit systems, while position is int64_t. Casting int64_t to size_t can lead to truncation, causing -Wconversion errors. The fix ensures the offset and position are checked for negativity before conversion. Fix: lib32-avro-c++/1.12/sources/avro-c++-1.12/lang/c++/impl/parsing/Symbol.cc:91:27: error: useless cast to type 'int' [-Werror=useless-cast] 91 | adj.push_back(static_cast<int>(it - rs.begin())); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1plus: all warnings being treated as errors lib32-avro-c++/1.12/sources/avro-c++-1.12/lang/c++/impl/FileStream.cc:208:41: error: conversion from 'int64_t' {aka 'long long int'} to 'size_t' {aka 'unsigned int'} may change value [-Werror=conversion] 208 | in_->seek(position - byteCount_ - available_); | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~ lib32-avro-c++/1.12/sources/avro-c++-1.12/lang/c++/impl/FileStream.cc:209:22: error: conversion from 'int64_t' {aka 'long long int'} to 'size_t' {aka 'unsigned int'} may change value [-Werror=conversion] 209 | byteCount_ = position; | ^~~~~~~~ cc1plus: all warnings being treated as errors These changes fix build failures on 32-bit systems and ensure safe type conversions. Signed-off-by: Alper Ak <alperyasinak1@gmail.com>
1 parent 0997c33 commit 030f6d1

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lang/c++/impl/FileStream.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,15 @@ class BufferCopyInInputStream : public SeekableInputStream {
205205
void seek(int64_t position) final {
206206
// BufferCopyIn::seek is relative to byteCount_, whereas position is
207207
// absolute.
208-
in_->seek(position - byteCount_ - available_);
209-
byteCount_ = position;
208+
int64_t offset = position - static_cast<int64_t>(byteCount_) - static_cast<int64_t>(available_);
209+
if (offset < 0) {
210+
throw Exception("Negative offset in seek");
211+
}
212+
in_->seek(static_cast<size_t>(offset));
213+
if (position < 0) {
214+
throw Exception("Negative position not allowed");
215+
}
216+
byteCount_ = static_cast<size_t>(position);
210217
available_ = 0;
211218
}
212219

lang/c++/impl/parsing/Symbol.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ Symbol Symbol::enumAdjustSymbol(const NodePtr &writer, const NodePtr &reader) {
8888
adj.push_back(static_cast<int>(-pos));
8989
err.push_back(s);
9090
} else {
91-
adj.push_back(static_cast<int>(it - rs.begin()));
91+
auto index = it - rs.begin();
92+
if constexpr (std::is_same_v<decltype(index), int>) {
93+
adj.push_back(index); // 32-bit: already int
94+
} else {
95+
adj.push_back(static_cast<int>(index)); // 64-bit: long to int
96+
}
9297
}
9398
}
9499
return Symbol(Kind::EnumAdjust, make_pair(adj, err));

0 commit comments

Comments
 (0)