【问题标题】:How to prevent Node from crashing when using node-ffi使用node-ffi时如何防止Node崩溃
【发布时间】:2018-12-26 18:27:34
【问题描述】:

我有一个 node.js 服务器,它使用 Node-ffi 调用 C++ 代码并将其发送到客户端。我面临的问题是客户端可以发送使 C++ 程序崩溃的用户输入,我该如何防止这种情况发生?

当C++程序崩溃时,服务器端出现如下输出:

npm ERR! code ELIFECYCLE
npm ERR! errno 3221225477
npm ERR! ws_send_json_server@1.0.0 start: `node app.js`
npm ERR! Exit status 3221225477
npm ERR!
npm ERR! Failed at the ws_send_json_server@1.0.0 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:

我想知道是否可以在 node-ffi 函数调用周围编写一个 try-catch 块或其他东西,例如:

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.")
}

其中“greenbuild”是 ffi.C++ 代码库。

谢谢。

【问题讨论】:

    标签: c++ node.js error-handling crash node-ffi


    【解决方案1】:

    答案是在 C++ 端进行输入验证,并进行调试以将文本文件写入崩溃输出。

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 1970-01-01
      • 2017-05-12
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多