Search code examples
c++node.jserror-handlingcrashnode-ffi

How to prevent Node from crashing when using node-ffi


I have a node.js server that uses Node-ffi to call C++ code and send it to the client. The problem I'm facing is that the client can send user input that crashes the C++ program, how do I prevent this?

When the C++ program crashes, the following output appears in the terminal of the server:

npm ERR! code ELIFECYCLE
npm ERR! errno 3221225477
npm ERR! [email protected] start: `node app.js`
npm ERR! Exit status 3221225477
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

I'm wondering if it's possible to write a try-catch block or something else around the node-ffi function calls, for example:

try {
    greenbuild.GB_SetArchOffset(messageJSON.Item.arch_offset);
    greenbuild.GB_SetArchRadius(messageJSON.Item.arch_radius);
    greenbuild.GB_SetBayLength(messageJSON.Item.bay_length);
    greenbuild.GB_SetBayWidth(messageJSON.Item.bay_width);
    greenbuild.GB_SetPeakHeight(messageJSON.Item.peak_height);
    greenbuild.GB_SetWallHeight(messageJSON.Item.wall_height);
    greenbuild.GB_SetColumnSpacing(messageJSON.Item.column_spacing);
    greenbuild.GB_SetNumBayLength(messageJSON.Item.number_bays_length);
    greenbuild.GB_SetNumBayWidth(messageJSON.Item.number_bays_width);
    greenbuild.GB_SetNumPanelHorzLength(messageJSON.Item.horizontal_panels_length);
    greenbuild.GB_SetNumPanelHorzWidth(messageJSON.Item.horizontal_panels_width)
    greenbuild.GB_SetNumPanelVert(messageJSON.Item.vertical_panels);

} catch (ERROR) {
    console.error("error occurred, but the whole server is still running.")
}

where "greenbuild" is a ffi.Library of C++ code.

Thank you.


Solution

  • The answer is to do input validation on the C++ side, and for debugging to write text files as the crash output.