私はCプログラミングに不慣れで、ポインターの計算に混乱しています。サイズ32の文字の配列があります。これは、文字変数が1バイト大きいため、配列も32バイトであることを意味すると理解しています32 characters * 1 byte = 32 bytes
。問題は、前に説明したように、文字の配列を指しているvoidポインターを持つ関数がある場合です。コードセグメントだと思います
for (count = 0; count < size; count++)
*((int*) raw_sk + count) = 0
raw_skバッファのすべてのスロットを0に設定する必要があります。ただし、プログラムを実行すると、セグメンテーション違反が発生します。住所にカウントを追加しているのかもしれないと思いました。アドレスに1つ追加すると、配列の次のスロットに移動すると思いました。誰かが私がどこで間違っているのか指摘できますか?私が使用している機能は以下のとおりです。ありがとう!
void
write_skfile (const char *skfname, void *raw_sk, size_t raw_sklen)
{
int fdsk = 0;
char *s = NULL;
int status = 0;
int count = 0;
int size = (raw_sklen);
/* armor the raw symmetric key in raw_sk using armor64 */
s = armor64(raw_sk, raw_sklen);
/* now let's write the armored symmetric key to skfname */
if ((fdsk = open (skfname, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
perror (getprogname ());
/*scrubs the armored buffer*/
for(count = 0; count < armor64len(s); count++)
s[count] = '0';
free (s);
/* scrub the buffer that's holding the key before exiting */
for (count = 0; count < size; count++)
*((int*)raw_sk + count) = 0;
exit (-1);
}
else {
status = write (fdsk, s, strlen (s));
if (status != -1) {
status = write (fdsk, "\n", 1);
}
for (count = 0; (size_t)count < 22; count++)
*((int*)raw_sk + count) = 0;
free (s);
close (fdsk);
/* do not scrub the key buffer under normal circumstances
(it's up to the caller) */
if (status == -1) {
printf ("%s: trouble writing symmetric key to file %s\n",
getprogname (), skfname);
perror (getprogname ());
/* scrub the buffer that's holding the key before exiting */
/* scrub the buffer that's holding the key before exiting MY CODE
for (count = 0; count < size; count++)
*((int*)raw_sk + count) = 0;*/
exit (-1);
}
}
}