Lack of return causes abort() in Emscripten

I received the following Javascript exception from some code compiled with Emscripten. The code was working correctly as a standalone C++ program. This confused me for a while:

uncaught exception: abort() at jsStackTrace@file:///home/new/gitcode/repeatanalysis/progs/r.js:1049:7
__Z15test_badret_auxiii [test_badret_aux(int, int, int)]@file:///home/new/gitcode/repeatanalysis/progs/r.js:8079:2

The cause was a non-void function failing to return a value. Here’s the complete example:

#include <iostream>
#include <fstream>
#include <vector>
#include "malloc.h"

using namespace std;

int test_badret_aux(int arg1,int arg2,int arg3) {
// return 0;

extern "C" {
int test_badret(char *inputstring);

int test_badret(char *inputstring) {

return 0;

Compiled with (I tried a bunch of options):

 emcc ./r.cpp -o r.js -s EXPORTED_FUNCTIONS="['_test_badret']" -s SAFE_HEAP=1 -g -s ASSERTIONS=1

And driven with the following html:

 <!DOCTYPE html>
<script src="r.js"></script>


var mystring = "test";
var strptr = Module._malloc(mystring.length);
Module.writeAsciiToMemory(mystring, strptr);

var ret = Module.ccall('test_badret', // name of C function
  'number', // return type
  ['number'], // argument types
  [strptr]); // arguments



If I uncomment the return in the above C program, the code no longer aborts. Understandable perhaps. But seemed unusual to me. This was using Firefox (Iceweasel 31.2.0 on Debian Jessie).

Comments are closed.