avro icon indicating copy to clipboard operation
avro copied to clipboard

Symbol.cc and FileStream.cc: Fix -Wconversion and -Wuseless-cast on 32-bit builds

Open alperak opened this issue 5 months ago • 3 comments

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 that the offset and position are checked to be non-negative 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(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.

alperak avatar Jul 16 '25 15:07 alperak

Does Avro itself enable -Wuseless-cast or does that come from some surrounding build system?

I doubt that warning will reveal any actual bug, so my first inclination would be to disable it altogether. But I'm not using the Avro C++ library so my opinion shouldn't have much weight here.

KalleOlaviNiemitalo avatar Jul 16 '25 17:07 KalleOlaviNiemitalo

Does Avro itself enable -Wuseless-cast or does that come from some surrounding build system?

I doubt that warning will reveal any actual bug, so my first inclination would be to disable it altogether. But I'm not using the Avro C++ library so my opinion shouldn't have much weight here.

-Wuseless-cast is enabled by Avro itself in its CMakeLists.txt.

alperak avatar Jul 16 '25 17:07 alperak

useless-cast warning is gcc specific, if one is using clang then this will be flagged as unknown-warning-option

kraj avatar Jul 20 '25 18:07 kraj