fix out-of-bounds mem access that valgrind reports
fix bug in foreign package