From c77b8be01172c36ee949880a3c60e5da23bdea1d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 28 Oct 2013 00:40:10 +0900 Subject: [PATCH] insert type checks in pic_get_args --- include/picrin/value.h | 1 + src/vm.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/include/picrin/value.h b/include/picrin/value.h index 7181813e..8474710b 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -92,5 +92,6 @@ pic_value pic_int_value(int); #define pic_int_p(v) ((v).type == PIC_VTYPE_INT) #define pic_pair_p(v) (pic_type(v) == PIC_TT_PAIR) #define pic_symbol_p(v) (pic_type(v) == PIC_TT_SYMBOL) +#define pic_str_p(v) (pic_type(v) == PIC_TT_STRING) #endif diff --git a/src/vm.c b/src/vm.c index cefae199..9ba037b8 100644 --- a/src/vm.c +++ b/src/vm.c @@ -56,14 +56,17 @@ pic_get_args(pic_state *pic, const char *format, ...) pic_value v; v = GET_OPERAND(pic, i); - if (pic_type(v) == PIC_TT_FLOAT) { + switch (pic_type(v)) { + case PIC_TT_FLOAT: *f = pic_float(v); - i++; - } - else { + break; + case PIC_TT_INT: *f = pic_int(v); - i++; + break; + default: + pic_error(pic, "pic_get_args: expected float or int"); } + i++; } } break; @@ -78,13 +81,17 @@ pic_get_args(pic_state *pic, const char *format, ...) pic_value v; v = GET_OPERAND(pic, i); - if (pic_type(v) == PIC_TT_FLOAT) { + switch (pic_type(v)) { + case PIC_TT_FLOAT: *f = pic_float(v); *e = false; - } - else { + break; + case PIC_TT_INT: *f = pic_int(v); *e = true; + break; + default: + pic_error(pic, "pic_get_args: expected float or int"); } i++; } @@ -101,13 +108,17 @@ pic_get_args(pic_state *pic, const char *format, ...) pic_value v; v = GET_OPERAND(pic, i); - if (pic_type(v) == PIC_TT_FLOAT) { + switch (pic_type(v)) { + case PIC_TT_FLOAT: *k = (int)pic_float(v); *e = false; - } - else { + break; + case PIC_TT_INT: *k = pic_int(v); *e = true; + break; + default: + pic_error(pic, "pic_get_args: expected float or int"); } i++; } @@ -123,6 +134,9 @@ pic_get_args(pic_state *pic, const char *format, ...) len = va_arg(ap, size_t *); if (i < argc) { str = GET_OPERAND(pic,i); + if (! pic_str_p(str)) { + pic_error(pic, "pic_get_args: expected string"); + } *cstr = pic_str_ptr(str)->str; *len = pic_str_ptr(str)->len; i++;