From 6246c43e22a005d376f758bf92c61c1c71f65936 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 8 May 2017 12:08:48 -0400 Subject: [PATCH] Fix integer division by 0 error. --- Makefile | 2 +- core/stl.c | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index f91590ba..a7954369 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ ###################################################### ##### Set global variables for all gst Makefiles ##### ###################################################### -CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -I./include -g -lreadline +CFLAGS=-std=c99 -Wall -Wextra -Wpedantic -I./include -lreadline -g PREFIX=/usr/local GST_TARGET=client/gst GST_CORELIB=core/libgst.a diff --git a/core/stl.c b/core/stl.c index 1382427c..16ad5f92 100644 --- a/core/stl.c +++ b/core/stl.c @@ -54,7 +54,7 @@ GstValue gst_stl_binop_##name(GstValue lhs, GstValue rhs) {\ return gst_wrap_nil();\ } -#define SIMPLE_ACCUM_FUNCTION(name, start, op)\ +#define SIMPLE_ACCUM_FUNCTION(name, op)\ MAKE_BINOP(name, op)\ int gst_stl_##name(Gst* vm) {\ GstValue lhs, rhs;\ @@ -70,10 +70,27 @@ int gst_stl_##name(Gst* vm) {\ gst_c_return(vm, lhs);\ } -SIMPLE_ACCUM_FUNCTION(add, 0, +) -SIMPLE_ACCUM_FUNCTION(mul, 1, *) -SIMPLE_ACCUM_FUNCTION(sub, 0, -) -SIMPLE_ACCUM_FUNCTION(div, 1, /) +SIMPLE_ACCUM_FUNCTION(add, +) +SIMPLE_ACCUM_FUNCTION(mul, *) +SIMPLE_ACCUM_FUNCTION(sub, -) + +/* Detect division by zero */ +MAKE_BINOP(div, /) +int gst_stl_div(Gst *vm) { + GstValue lhs, rhs; + uint32_t j, count; + count = gst_count_args(vm); + lhs = gst_arg(vm, 0); + for (j = 1; j < count; ++j) { + rhs = gst_arg(vm, j); + if (lhs.type == GST_INTEGER && rhs.type == GST_INTEGER && rhs.data.integer == 0) + gst_c_throwc(vm, "cannot integer divide by 0"); + lhs = gst_stl_binop_div(lhs, rhs); + } + if (lhs.type == GST_NIL) + gst_c_throwc(vm, "expected integer/real"); + gst_c_return(vm, lhs); +} #undef SIMPLE_ACCUM_FUNCTION