caveats reinterpret_cast' in char* to nosigned char*?

发布于 2025-01-22 18:17:22 字数 1458 浏览 2 评论 0原文

我正在尝试将我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2025-01-29 18:17:22

如果我只使用reinterpret_cast&lt; unsigned char&gt;在最坏的情况下会发生什么?

您始终可以reinterpret_cast 无符号char,并且您始终能够reinterpret_cast 签名 unsigned << /code>和反之亦然,因此您是双重安全的。

为什么C-API甚至需要未签名的char?

问作者。猜测,由于对未签名值的算术更安全,因此定义了下流和溢出。

如果这是字符的正确表示形式,那么为什么std ::字符串默认?

因为您通常不需要对角色进行算术。

What could happen in the worst possible case if I just use reinterpret_cast<unsigned char> on my input strings?

You can always reinterpret_cast to unsigned char, and you are always able to reinterpret_cast signed to unsigned and vice-versa, so you are doubly safe.

Why is the C-API even requiring unsigned char?

Ask the author. At a guess, because arithmetic on unsigned values is safer, underflow and overflow are defined.

If that's the right representation for characters, then why isn't std::string the default?

Because you generally don't need to do arithmetic on characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文