caveats reinterpret_cast' in char* to nosigned char*?
我正在尝试将我的C ++应用程序适合C-API。所讨论的API是MBED_TL,其中包含一个base64解码器:
int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen )
问题是我使用std :: String到达现场,而我不能将其C-string作为函数参数。如果我收到此错误:(
<source>:17:27: error: invalid conversion from 'char*' to 'unsigned char*' [-fpermissive]
17 | mbedtls_base64_decode(buf, out_buf_size, &written, token.c_str(), in_buf_size);
| ^~~
| |
| char*
输入字符串的相同重复)
代码进行评论(在):,
#include <string>
extern "C" int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen )
{
/* dummy */
return 0;
}
std::string token = "some token that I received over the internet";
int main()
{
constexpr size_t in_buf_size = 10, out_buf_size = 10;
size_t written;
char buf[out_buf_size];
mbedtls_base64_decode(buf, out_buf_size, &written, token.c_str(), in_buf_size);
}
我的代码围绕一个字符串&lt; char*&gt;。当我尝试将std :: string的c string推入
问题:
如果我只使用reinterpret_cast&lt; unsigned char&gt;
在最坏情况下会发生什么情况。字符串?为什么C-API甚至需要未签名的字符?如果这是字符的正确表示形式,那么为什么不std :: String&lt; unsigned char&gt;
默认值呢?
I'm trying to fit my C++ app to a C-API. The API in question is mbed_tls which contains a base64 decoder:
int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen )
The problem is that I'm arriving in site with a std::string and I can't put its c-string as a function argument. If I do I get this error:
<source>:17:27: error: invalid conversion from 'char*' to 'unsigned char*' [-fpermissive]
17 | mbedtls_base64_decode(buf, out_buf_size, &written, token.c_str(), in_buf_size);
| ^~~
| |
| char*
(same repeats for the input string)
Code for review (view on godbolt):
#include <string>
extern "C" int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen )
{
/* dummy */
return 0;
}
std::string token = "some token that I received over the internet";
int main()
{
constexpr size_t in_buf_size = 10, out_buf_size = 10;
size_t written;
char buf[out_buf_size];
mbedtls_base64_decode(buf, out_buf_size, &written, token.c_str(), in_buf_size);
}
, my code carries around a string<char*>. When I try to push my std::string's c-string into
Question:
What could happen in the worst possible case if I just use reinterpret_cast<unsigned char>
on my input strings? Why is the C-API even requiring unsigned char? If that's the right representation for characters, then why isn't std::string<unsigned char>
the default?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以
reinterpret_cast
无符号char
,并且您始终能够reinterpret_cast
签名
unsigned <<
/code>和反之亦然,因此您是双重安全的。问作者。猜测,由于对未签名值的算术更安全,因此定义了下流和溢出。
因为您通常不需要对角色进行算术。
You can always
reinterpret_cast
tounsigned char
, and you are always able toreinterpret_cast
signed
tounsigned
and vice-versa, so you are doubly safe.Ask the author. At a guess, because arithmetic on unsigned values is safer, underflow and overflow are defined.
Because you generally don't need to do arithmetic on characters.