malloc() memory corruption in C -


malloc() giving me error in following code. used valgrind , still no avail. beginner @ c , team-mate , trying implement sha1 algorithm there none of can understand , have spent hours on this. the function had error in -

/*  * returns array of chunks on heap message  */  static unsigned char **sha1_chunkify(const unsigned char *message, const uint64_t message_length) {     long num_chunks = message_length / 64;    //breaking down 64 byte chunks     printf("%lu %ld\n", message_length, num_chunks);     unsigned char **chunks = malloc (num_chunks * sizeof(*chunks)); //error coming on here      (int = 0; < num_chunks; i++) {         chunks[i] = malloc (64 * sizeof(*chunks[i])); //or on here.          (int j = 0; j < 64; j++) {             chunks[i][j] = message[64 * + j];         }     }      return chunks; } 

here gdb output @ lines -

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) @ ../nptl/sysdeps/unix/sysv/linux/raise.c:56 56  ../nptl/sysdeps/unix/sysv/linux/raise.c: no such file or directory. 

and valgrind shows following -

3 mytry.c ==16688== invalid write of size 1 ==16688==    @ 0x401ea6: append_zeroes (sha1.c:155) ==16688==    0x401f1d: sha1_pad (sha1.c:177) ==16688==    0x401fb7: sha1 (sha1.c:200) ==16688==    0x400c72: snap_file (snap.c:40) ==16688==    0x4012f7: snap (snap.c:143) ==16688==    0x401852: snap_all (snap.c:220) ==16688==    0x40222e: main (nako.c:24) ==16688==  address 0x5220720 0 bytes after block of size 1,408 alloc'd ==16688==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==16688==    0x401f7b: sha1 (sha1.c:195) ==16688==    0x400c72: snap_file (snap.c:40) ==16688==    0x4012f7: snap (snap.c:143) ==16688==    0x401852: snap_all (snap.c:220) ==16688==    0x40222e: main (nako.c:24) ==16688==  ==16688== invalid write of size 1 ==16688==    @ 0x401e6a: append_msg_len (sha1.c:142) ==16688==    0x401f34: sha1_pad (sha1.c:179) ==16688==    0x401fb7: sha1 (sha1.c:200) ==16688==    0x400c72: snap_file (snap.c:40) ==16688==    0x4012f7: snap (snap.c:143) ==16688==    0x401852: snap_all (snap.c:220) ==16688==    0x40222e: main (nako.c:24) ==16688==  address 0x5220758 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: caused program erroneously writing past end of heap block , corrupting heap metadata.  if fix invalid writes reported memcheck, assertion failure go away.  please try before reporting bug.  ==16688==    @ 0x38050bac: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x38050d06: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x3805b36a: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x3805d2d7: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x380216d4: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x380218a2: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x3809dc03: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==16688==    0x380ac87c: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)  sched status: running_tid=1  thread 1: status = vgts_runnable ==16688==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==16688==    0x401d94: sha1_chunkify (sha1.c:118) ==16688==    0x401fca: sha1 (sha1.c:202) ==16688==    0x400c72: snap_file (snap.c:40) ==16688==    0x4012f7: snap (snap.c:143) ==16688==    0x401852: snap_all (snap.c:220) ==16688==    0x40222e: main (nako.c:24) 

i have gone through several threads error still persists. apologize if has been asked before. not find same. thanking in advance.

here functions append_zeroes , append_msg_len -

/*  * pad message length.  * input lengths in bytes, while padding, in accordance  * sha1 algorithm, 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 next 8 bits. */          message[*message_length] = (8 * original_msglen >> shift) & 0xff;          shift -= 8;     *message_length += 1;     } }  /*  * appends enough zeroes until message has enough room appending  * 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;     } } 

looking @ code:

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

if use message_length = 63, instance, num_chunks becomes zero, wrong. if use instead:

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

you right number of chunks, , rest of code well-behaved.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -