C++ 插件中的 Node.js 缓冲区是否终止? [英] Are Node.js buffers in C++ addon null terminated?

查看:50
本文介绍了C++ 插件中的 Node.js 缓冲区是否终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 .js 文件中创建缓冲区,然后将该缓冲区传递给我将调用 Windows API 的 c++ 插件.

I am trying to create buffer in a .js file, and then passing that buffer to a c++ addon where I will call a Windows API.

在我的 C++ 插件中:

In the c++ addon I have:

auto buf = node::Buffer::Data(args[0]);
auto len = node::Buffer::Length(args[0]);

是否有任何保证 node::Buffers 是空终止的?或者 node::Buffer::Length 是否有任何其他形式的安全检查来防止溢出?

Are there any guarantees that node::Buffers are null-terminated? Or does node::Buffer::Length have any other form of safety check to prevent an overrun?

推荐答案

没有.将缓冲区视为包含长度和内存的最小数据结构;它们不像 malloc() 提供的那样是原始内存.缓冲区可以防止 JavaScript 上下文中的内存溢出,但如果您将指向缓冲区内存的指针传递给 C/C++,则不会.并且因为 Buffer 可用于存储二进制数据(以及字符串),所以它不提供额外的字节来空终止字符串;它不知道"缓冲区中有什么.

No. Think of buffers as a minimal data structure containing length and memory; they are not raw memory like what malloc() provides. A buffer is protected against memory overruns within a JavaScript context but not if you pass a pointer to the buffer memory to C/C++. And because a Buffer can be used to store binary data (as well as a string) it doesn't provide an extra byte to null-terminate a string; it doesn't "know" what is in the buffer.

我认为您发布的代码不会起作用;这篇文章很好地解释了如何在 C/C++ 中使用缓冲区:https://community.risingstack.com/using-buffers-node-js-c-plus-plus/

I don't think the code you've posted will work; This post does a good job of explaining how to use buffers in C/C++: https://community.risingstack.com/using-buffers-node-js-c-plus-plus/

这篇关于C++ 插件中的 Node.js 缓冲区是否终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆