hexagonal-sun / bic

A C interpreter and API explorer.
GNU General Public License v2.0
815 stars 36 forks source link

Segmentation fault when calling external function without arguments #47

Closed Andrepuel closed 4 years ago

Andrepuel commented 4 years ago
// oi.h
int get_number();
// oi.c
int get_number() {
    return 42;
}

Compile with gcc oi.c -shared -o liboi. Run LD_LIBRARY_PATH=$PWD bic -loi -I$PWD

Execute on the REPL:

#include <oi.h>
get_number();

The segmentation fault happens because this specific function tree has ARGS as null (t1 = 0x0000000000000000). However, evalute.c:552 tries to iterate over this tree:

                for_each_tree(i, tFN_ARGS(function))
                    if (is_T_VARIADIC(i)){
                        is_variadic = true;
                        break;
                    }

A quick workaround for this problem was to check if args was set before iterating over it:

            if (tFN_ARGS(function)) {
                for_each_tree(i, tFN_ARGS(function))
                    if (is_T_VARIADIC(i)){
                        is_variadic = true;
                        break;
                    }
            }

Perhaps a better solution would be to include this check on the definition of for_each_tree?

hexagonal-sun commented 4 years ago

Thanks for the bug report and tracking down the problem. I've pushed changes that implements your suggestion of not entering the for_each_tree loop if head is null.