From d696339577dee258aad733bd1320f585c5848b02 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 22 Oct 2013 17:23:21 +0900 Subject: [PATCH] [bugfix] pic_get_args should ignore optional args when i > argc --- src/vm.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/vm.c b/src/vm.c index 046c3624..4a185ea3 100644 --- a/src/vm.c +++ b/src/vm.c @@ -27,6 +27,11 @@ pic_get_args(pic_state *pic, const char *format, ...) case '|': break; } + + /* in order to run out of all arguments passed to this function + (i.e. do va_arg for each argument), optional argument existence + check is done in every case closure */ + switch (c) { case '|': opt = true; @@ -36,8 +41,10 @@ pic_get_args(pic_state *pic, const char *format, ...) pic_value *p; p = va_arg(ap, pic_value*); - *p = GET_OPERAND(pic,i); - i++; + if (i < argc) { + *p = GET_OPERAND(pic,i); + i++; + } } break; case 'f': @@ -45,8 +52,10 @@ pic_get_args(pic_state *pic, const char *format, ...) double *f; f = va_arg(ap, double *); - *f = pic_float(GET_OPERAND(pic,i)); - i++; + if (i < argc) { + *f = pic_float(GET_OPERAND(pic,i)); + i++; + } } break; case 's': @@ -57,10 +66,12 @@ pic_get_args(pic_state *pic, const char *format, ...) cstr = va_arg(ap, char **); len = va_arg(ap, size_t *); - str = GET_OPERAND(pic,i); - *cstr = pic_str_ptr(str)->str; - *len = pic_str_ptr(str)->len; - i++; + if (i < argc) { + str = GET_OPERAND(pic,i); + *cstr = pic_str_ptr(str)->str; + *len = pic_str_ptr(str)->len; + i++; + } } break; }