From acf826eb7ab445c0a42d1f1f3222ff574ebf68bd Mon Sep 17 00:00:00 2001 From: cremno Date: Sun, 31 Aug 2014 04:31:46 +0200 Subject: [PATCH 1/2] fix some integer conversions in the Table class Sizes (x, y, z) are clamped to a non-negative int value. Use NUM2INT instead of FIX2INT as the argument might not be a Fixnum. --- binding-mri/table-binding.cpp | 75 ++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/binding-mri/table-binding.cpp b/binding-mri/table-binding.cpp index 97831f2..8a8d724 100644 --- a/binding-mri/table-binding.cpp +++ b/binding-mri/table-binding.cpp @@ -23,14 +23,39 @@ #include "binding-util.h" #include "serializable-binding.h" +static int num2TableSize(VALUE v) +{ + int i = NUM2INT(v); + return i >= 0 ? i : 0; +} + +static void parseArgsTableSizes(int argc, VALUE *argv, int *x, int *y, int *z) +{ + *y = *z = 1; + + switch (argc) + { + case 3: + *z = num2TableSize(argv[2]); + /* fall through */ + case 2: + *y = num2TableSize(argv[1]); + /* fall through */ + case 1: + *x = num2TableSize(argv[0]); + break; + default: + rb_error_arity(argc, 1, 3); + } +} + DEF_TYPE(Table); RB_METHOD(tableInitialize) { int x, y, z; - x = y = z = 1; - rb_get_args(argc, argv, "i|ii", &x, &y, &z RB_ARG_END); + parseArgsTableSizes(argc, argv, &x, &y, &z); Table *t = new Table(x, y, z); @@ -43,24 +68,12 @@ RB_METHOD(tableResize) { Table *t = getPrivateData(self); - switch (argc) - { - default: - case 1: - t->resize(FIX2INT(argv[0])); - return Qnil; + int x, y, z; + parseArgsTableSizes(argc, argv, &x, &y, &z); - case 2: - t->resize(FIX2INT(argv[0]), - FIX2INT(argv[1])); - return Qnil; + t->resize(x, y, z); - case 3: - t->resize(FIX2INT(argv[0]), - FIX2INT(argv[1]), - FIX2INT(argv[2])); - return Qnil; - } + return Qnil; } #define TABLE_SIZE(d, D) \ @@ -68,7 +81,7 @@ RB_METHOD(tableResize) { \ RB_UNUSED_PARAM \ Table *t = getPrivateData
(self); \ - return rb_int2inum(t->d##Size()); \ + return INT2NUM(t->d##Size()); \ } TABLE_SIZE(x, X) @@ -98,9 +111,9 @@ RB_METHOD(tableGetAt) return Qnil; } - int result = t->get(x, y, z); + short result = t->get(x, y, z); - return rb_int2inum(result); + return INT2FIX(result); /* short always fits in a Fixnum */ } RB_METHOD(tableSetAt) @@ -117,28 +130,28 @@ RB_METHOD(tableSetAt) { default: case 2 : - x = FIX2INT(argv[0]); - value = FIX2INT(argv[1]); + x = NUM2INT(argv[0]); + value = NUM2INT(argv[1]); break; case 3 : - x = FIX2INT(argv[0]); - y = FIX2INT(argv[1]); - value = FIX2INT(argv[2]); + x = NUM2INT(argv[0]); + y = NUM2INT(argv[1]); + value = NUM2INT(argv[2]); break; case 4 : - x = FIX2INT(argv[0]); - y = FIX2INT(argv[1]); - z = FIX2INT(argv[2]); - value = FIX2INT(argv[3]); + x = NUM2INT(argv[0]); + y = NUM2INT(argv[1]); + z = NUM2INT(argv[2]); + value = NUM2INT(argv[3]); break; } t->set(value, x, y, z); - return rb_int2inum(value); + return argv[argc - 1]; } MARSH_LOAD_FUN(Table) From 53cd5e1fbc3d3ee02397ed0d78542b92dee6a853 Mon Sep 17 00:00:00 2001 From: cremno Date: Sun, 31 Aug 2014 11:10:57 +0200 Subject: [PATCH 2/2] use std::max instead of ?: --- binding-mri/table-binding.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/binding-mri/table-binding.cpp b/binding-mri/table-binding.cpp index 8a8d724..87a9b58 100644 --- a/binding-mri/table-binding.cpp +++ b/binding-mri/table-binding.cpp @@ -19,6 +19,7 @@ ** along with mkxp. If not, see . */ +#include #include "table.h" #include "binding-util.h" #include "serializable-binding.h" @@ -26,7 +27,7 @@ static int num2TableSize(VALUE v) { int i = NUM2INT(v); - return i >= 0 ? i : 0; + return std::max(0, i); } static void parseArgsTableSizes(int argc, VALUE *argv, int *x, int *y, int *z)