Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion loses int precision #297

Merged
merged 3 commits into from Apr 21, 2023

Conversation

tsimnujhawj
Copy link
Contributor

@tsimnujhawj tsimnujhawj commented Apr 19, 2023

For: #298

compiling bytebuffer.c
bytebuffer.c:308:20: warning: implicit conversion loses integer precision: 'ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
    return INT2NUM(bytes_read);
           ~~~~~~~ ^~~~~~~~~~
bytebuffer.c:338:20: warning: implicit conversion loses integer precision: 'ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
    return INT2NUM(bytes_written);
           ~~~~~~~ ^~~~~~~~~~~~~

Getting this error on gem install nio4r using an Apple M1 computer with Ruby 3.0.6

The value of a long type is being assigned to an int type, which may result in the loss of data. By casting the bytes_read and bytes_written variable to an int type, this will ensure that the bytes_read and bytes_written value is truncated to fit in the range of an int type, and prevent any loss of data, which fixes this error.

Locally, I was able to install this gem on my Apple M1 with Ruby 3.0.6 with these changes.

I'm not a C programmer, so please scrutinize.

Types of Changes

  • Bug fix

Contribution

@@ -305,7 +305,7 @@ static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io)

buffer->position += bytes_read;

return INT2NUM(bytes_read);
return INT2NUM((int)bytes_read);
Copy link
Member

Choose a reason for hiding this comment

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

At this point we can assume the number is positive. So, what about using something like SIZET2NUM?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! Changed it.

@ioquatix ioquatix merged commit e7c0073 into socketry:main Apr 21, 2023
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants