访问违规写入位置

发布于 2024-08-13 04:55:58 字数 537 浏览 6 评论 0原文

我有以下代码:

#include <openssl/bn.h>
#include <openssl/rsa.h>

unsigned char* key;
RSA* rsa = RSA_new();
rsa = RSA_generate_key(1024,65537,NULL,NULL);
//init pubkey
key[BN_num_bytes(rsa->n)] = '\0';
BN_bn2bin(rsa->n, key);
printf("RSA Pub: %s\n", key);
RSA_free( rsa );
rsa = NULL;

调试器告诉我,我在该行上遇到了“访问冲突写入位置”问题

key[BN_num_bytes(rsa->n)] = '\0';

如果我注释掉该行,该问题就会向下移动到

BN_bn2bin(rsa->n, key);

有关如何解决此问题的任何建议,那就太好了。

I have the following code:

#include <openssl/bn.h>
#include <openssl/rsa.h>

unsigned char* key;
RSA* rsa = RSA_new();
rsa = RSA_generate_key(1024,65537,NULL,NULL);
//init pubkey
key[BN_num_bytes(rsa->n)] = '\0';
BN_bn2bin(rsa->n, key);
printf("RSA Pub: %s\n", key);
RSA_free( rsa );
rsa = NULL;

The debugger is telling me that I have an issue "Access violation writing location" on the line

key[BN_num_bytes(rsa->n)] = '\0';

If I comment out that line the issue just moves down to

BN_bn2bin(rsa->n, key);

any suggestions on how to fix this issue would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

谁许谁一生繁华 2024-08-20 04:55:58

由于 key 没有指向任何东西,并且您已经使用数组符号下标引用了它,这就是源。 key如何获取value。您正在覆盖或践踏不属于您的其他内存块,因此会被窗口捕获“访问冲突”。仔细检查您的代码并确保该变量已被 malloc 或 new 。

顺便说一句,最好是这样声明它,

unsigned char *key = NULL;

这样如果您尝试访问 key 而没有对其进行 malloc'd/new'd,您将收到内存异常错误(这可以很容易地确定下来)。考虑一下它使调试变得更加容易。

希望这有帮助,
此致,
汤姆.

Since key is not pointing to anything and you have referenced it with array notation subscript, that is the source. How does key get the value. You are overwriting or trampling on some other memory block that is not yours hence the 'Access violation' as trapped by windows. Double check your code and make sure that the variable has been malloc'd or new'd.

As a side note, it is best for your sanity to declare it like this

unsigned char *key = NULL;

In that way if you try access key without it being malloc'd/new'd, you will get a memory exception error (which can easily be nailed down to this). Consider it makes debugging much easier.

Hope this helps,
Best regards,
Tom.

如若梦似彩虹 2024-08-20 04:55:58

您遇到访问冲突,因为您尝试使用空终止符分配键,但尚未为键分配任何内存。我们需要知道您想要实现什么目标。

You have an access violation because you try to assign key with the null terminator but you haven't allocated any memory for key. We would need to know what you are trying to accomplish.

捶死心动 2024-08-20 04:55:58

您没有为该键分配任何内存——第一次使用它是在您尝试将某个元素设置为 0 时。

You aren't allocating any memory for the key -- the first time it's used is when you try to set an element to 0.

妄司 2024-08-20 04:55:58

在这种情况下分配适量内存的正确解决方案是:

key = malloc(BN_num_bytes(rsa->n));
BN_bn2bin(rsa->n, key);

The correct solution to allocate the right amount of memory in this case is:

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