librelist archives

« back to archive

[PATCH] avoid downsizing casts

[PATCH] avoid downsizing casts

From:
Eric Wong
Date:
2014-01-20 @ 21:23
It's always safe to cast a 32-bit value to a 64-bit value, but
compilers can warn going the other way (because VALUE is a pointer).

Tested with `ruby -v`
ruby 2.2.0dev (2014-01-19 trunk 44646) [x86_64-linux]
which enables -Wpointer-to-int-cast
---
 pushed as 809b9716a2593c0bfcaa115e7e560615c6357b55

 ext/kgio/poll.c    | 4 ++--
 ext/kgio/tryopen.c | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/ext/kgio/poll.c b/ext/kgio/poll.c
index 15774d8..ae8d235 100644
--- a/ext/kgio/poll.c
+++ b/ext/kgio/poll.c
@@ -139,13 +139,13 @@ static VALUE poll_result(int nr, struct poll_args *a)
 static VALUE do_poll(VALUE args)
 {
 	struct poll_args *a = (struct poll_args *)args;
-	int nr;
+	long nr;
 
 	Check_Type(a->ios, T_HASH);
 
 retry:
 	hash2pollfds(a);
-	nr = (int)rb_thread_blocking_region(nogvl_poll, a, RUBY_UBF_IO, NULL);
+	nr = (long)rb_thread_blocking_region(nogvl_poll, a, RUBY_UBF_IO, NULL);
 	if (nr < 0) {
 		if (interrupted()) {
 			if (retryable(a)) {
diff --git a/ext/kgio/tryopen.c b/ext/kgio/tryopen.c
index 1d8fffd..5889294 100644
--- a/ext/kgio/tryopen.c
+++ b/ext/kgio/tryopen.c
@@ -81,7 +81,7 @@ static VALUE my_thread_blocking_region(
  */
 static VALUE s_tryopen(int argc, VALUE *argv, VALUE klass)
 {
-	int fd;
+	long fd;
 	VALUE pathname, flags, mode;
 	struct open_args o;
 	int retried = 0;
@@ -106,7 +106,7 @@ static VALUE s_tryopen(int argc, VALUE *argv, VALUE klass)
 	}
 
 retry:
-	fd = (int)rb_thread_blocking_region(nogvl_open, &o, RUBY_UBF_IO, 0);
+	fd = (long)rb_thread_blocking_region(nogvl_open, &o, RUBY_UBF_IO, 0);
 	if (fd < 0) {
 		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
 			rb_gc();
@@ -125,7 +125,7 @@ retry:
 			return rv;
 		}
 	}
-	rv = rb_funcall(klass, id_for_fd, 1, INT2FIX(fd));
+	rv = rb_funcall(klass, id_for_fd, 1, LONG2FIX(fd));
 	set_file_path(rv, pathname);
 	return rv;
 }
-- 
Eric Wong