Skip to content

Conversation

@alperak
Copy link

@alperak alperak commented Jul 16, 2025

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.

@github-actions github-actions bot added the C++ Pull Requests for C++ binding label Jul 16, 2025
…2-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 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<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>
@KalleOlaviNiemitalo
Copy link
Contributor

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.

@alperak
Copy link
Author

alperak commented Jul 16, 2025

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.

@kraj
Copy link
Contributor

kraj commented Jul 20, 2025

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

@martin-g martin-g requested a review from Copilot October 23, 2025 11:45
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes compiler warnings (-Wconversion and -Wuseless-cast) that occur when building on 32-bit systems. The changes ensure type-safe conversions between platform-dependent types.

Key Changes:

  • Added compile-time type checking in Symbol.cc to avoid useless casts on 32-bit systems
  • Added validation and explicit casts in FileStream.cc to prevent unsafe int64_t to size_t conversions

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lang/c++/impl/parsing/Symbol.cc Uses if constexpr to conditionally cast based on platform type
lang/c++/impl/FileStream.cc Adds negative value checks and explicit casts for seek operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +208 to +215
int64_t offset = position - static_cast<int64_t>(byteCount_) - static_cast<int64_t>(available_);
if (offset < 0) {
throw Exception("Negative offset in seek");
}
in_->seek(static_cast<size_t>(offset));
if (position < 0) {
throw Exception("Negative position not allowed");
}
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The negative offset check occurs after the subtraction, which means if position is less than byteCount_ + available_, a negative offset will be computed and then converted to a very large size_t value when cast on line 212. The check should occur before the cast, but the cast happens inside the function call. Consider restructuring to avoid passing a potentially negative value through the cast, or ensure the arithmetic cannot produce negative results through earlier validation.

Suggested change
int64_t offset = position - static_cast<int64_t>(byteCount_) - static_cast<int64_t>(available_);
if (offset < 0) {
throw Exception("Negative offset in seek");
}
in_->seek(static_cast<size_t>(offset));
if (position < 0) {
throw Exception("Negative position not allowed");
}
if (position < 0) {
throw Exception("Negative position not allowed");
}
int64_t offset = position - static_cast<int64_t>(byteCount_) - static_cast<int64_t>(available_);
if (offset < 0) {
throw Exception("Negative offset in seek");
}
in_->seek(static_cast<size_t>(offset));

Copilot uses AI. Check for mistakes.
Comment on lines +213 to +215
if (position < 0) {
throw Exception("Negative position not allowed");
}
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The negative position check should occur at the beginning of the function, before any calculations are performed with position. Moving this validation earlier would fail fast and avoid unnecessary computation.

Copilot uses AI. Check for mistakes.
if (offset < 0) {
throw Exception("Negative offset in seek");
}
in_->seek(static_cast<size_t>(offset));
Copy link
Member

@martin-g martin-g Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care about 32-bit systems ?
Update: Actually this PR is specifically for improving the things on 32bit systems.

The cast from int64_t to size_t may truncate on 32bit

if (position < 0) {
throw Exception("Negative position not allowed");
}
byteCount_ = static_cast<size_t>(position);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

} else {
adj.push_back(static_cast<int>(it - rs.begin()));
auto index = it - rs.begin();
if constexpr (std::is_same_v<decltype(index), int>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to import <type_traits> for is_same_v ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ Pull Requests for C++ binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants