gpt4 book ai didi

c - C 中的 malloc() 内存损坏

转载 作者:行者123 更新时间:2023-11-30 19:15:04 26 4
gpt4 key购买 nike

malloc() 在以下代码中给我一个错误。我用了 valgrind 还是没用。我是 C 语言的初学者,我和我的队友正在尝试实现 SHA1 算法,但有一些东西我们谁都无法理解,并且为此花费了数小时。这是有错误的函数 -

/*
* Returns an array of chunks on the heap from the message
*/

static unsigned char **sha1_chunkify(const unsigned char *message, const uint64_t message_length)
{
long num_chunks = message_length / 64; //breaking it down into 64 byte chunks
printf("%lu %ld\n", message_length, num_chunks);
unsigned char **chunks = malloc (num_chunks * sizeof(*chunks)); //Error is coming over here

for (int i = 0; i < num_chunks; i++) {
chunks[i] = malloc (64 * sizeof(*chunks[i])); //Or sometimes over here.

for (int j = 0; j < 64; j++) {
chunks[i][j] = message[64 * i + j];
}
}

return chunks;
}

这是这些行的 gdb 输出 -

117     printf("%lu %ld\n", message_length, num_chunks);
(gdb)
1472 23
118 unsigned char **chunks = (unsigned char **) malloc (num_chunks * sizeof(*chunks));
(gdb)
*** Error in `/home/username/Desktop/Project_VCS/a.out': malloc(): memory corruption: 0x000000000060cac0 ***

Program received signal SIGABRT, Aborted.
0x00007ffff7a4bcc9 in __GI_raise (sig=sig@entry=6)
at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

Valgrind 显示以下内容 -

3
mytry.c
==16688== Invalid write of size 1
==16688== at 0x401EA6: append_zeroes (sha1.c:155)
==16688== by 0x401F1D: sha1_pad (sha1.c:177)
==16688== by 0x401FB7: sha1 (sha1.c:200)
==16688== by 0x400C72: snap_file (snap.c:40)
==16688== by 0x4012F7: snap (snap.c:143)
==16688== by 0x401852: snap_all (snap.c:220)
==16688== by 0x40222E: main (nako.c:24)
==16688== Address 0x5220720 is 0 bytes after a block of size 1,408 alloc'd
==16688== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16688== by 0x401F7B: sha1 (sha1.c:195)
==16688== by 0x400C72: snap_file (snap.c:40)
==16688== by 0x4012F7: snap (snap.c:143)
==16688== by 0x401852: snap_all (snap.c:220)
==16688== by 0x40222E: main (nako.c:24)
==16688==
==16688== Invalid write of size 1
==16688== at 0x401E6A: append_msg_len (sha1.c:142)
==16688== by 0x401F34: sha1_pad (sha1.c:179)
==16688== by 0x401FB7: sha1 (sha1.c:200)
==16688== by 0x400C72: snap_file (snap.c:40)
==16688== by 0x4012F7: snap (snap.c:143)
==16688== by 0x401852: snap_all (snap.c:220)
==16688== by 0x40222E: main (nako.c:24)
==16688== Address 0x5220758 is not stack'd, malloc'd or (recently) free'd
==16688==
1472 23

valgrind: m_mallocfree.c:277 (mk_plain_bszB): Assertion 'bszB != 0' failed.
valgrind: This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata. If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away. Please try that before reporting this as a bug.

==16688== at 0x38050BAC: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x38050D06: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x3805B36A: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x3805D2D7: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x380216D4: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x380218A2: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x3809DC03: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==16688== by 0x380AC87C: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

sched status:
running_tid=1

Thread 1: status = VgTs_Runnable
==16688== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16688== by 0x401D94: sha1_chunkify (sha1.c:118)
==16688== by 0x401FCA: sha1 (sha1.c:202)
==16688== by 0x400C72: snap_file (snap.c:40)
==16688== by 0x4012F7: snap (snap.c:143)
==16688== by 0x401852: snap_all (snap.c:220)
==16688== by 0x40222E: main (nako.c:24)

我已经经历了几个线程,但错误仍然存​​在。如果之前有人问过,我深表歉意。我找不到相同的。预先感谢您。

这里是函数append_zeroes 和append_msg_len -

/*
* Pad the message with its length.
* The input lengths are in bytes, while the padding, in accordance with
* the SHA1 algorithm, is done in bits.
*/
static inline void append_msg_len(unsigned char *message,
uint64_t *message_length,
uint64_t original_msglen)
{
int shift = 56;
while (shift >= 0) {
/* Add the next 8 bits. */
message[*message_length] = (8 * original_msglen >> shift) & 0xff;
shift -= 8;
*message_length += 1;
}
}

/*
* Appends enough zeroes until the message has just enough room for appending
* the message length, i.e, length 448 mod 512
*/
static inline void append_zeroes(unsigned char *message, uint64_t *message_length)
{
while (*message_length % 64 != 56) {
message[*message_length] = (unsigned char) 0x00;
*message_length += 1;
}
}

最佳答案

查看这段代码:

long num_chunks = message_length / 64;    //breaking it down into 64 byte chunks

例如,如果我们使用 message_length = 63,num_chunks 就会变成零,这显然是错误的。如果您使用:

long num_chunks = (message_length + 63) / 64;    //breaking it down into 64 byte chunks

您将获得正确数量的 block ,并且希望其余代码表现良好。

关于c - C 中的 malloc() 内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com