Генератор случайного пароля. Утечка памяти - C (СИ)
Формулировка задачи:
Написал программу для генерации случайного пароля/ключа.
Все работает. Проблема только в утечке памяти.
То есть, как я понимаю, проблема совсем не в malloc, а в чем-то другом... Как это исправить. И скажите, какие у вас есть замечания по коду.
Спасибо.
#include <stdio.h> #include <stdlib.h> #define DEFAULT_KEY_LENGTH 500 #define MAX_KEY_LENGTH 1000000 int main(int argc, char **argv) { FILE *fp; char *password; char ch; unsigned int i, length; fp = fopen( "/dev/urandom", "r" ); if( fp == NULL ) { fprintf( stderr, "Error while opening /dev/urandom\n" ); return 1; } length = DEFAULT_KEY_LENGTH; if( argc > 1 ) { length = atoi(argv[1]); if( length < 1 || length > MAX_KEY_LENGTH ) { fprintf( stderr, "Variable number must be between 1 and %d\n", MAX_KEY_LENGTH ); return 2; } } i = 0; password = malloc( (length+1)*sizeof(char) ); if (password == NULL) { fprintf( stderr, "Malloc error\n" ); return 3; } while (i <= length) { ch = fgetc(fp); if ( (ch>=48 && ch<=57) || (ch>=65 && ch<=90) || (ch>=97 && ch<=122) ) { password[i] = ch; i++; } } fclose( fp ); while( i ) { fputc( password[i], stdout ); i--; } free( password ); fputc( '\n', stdout ); return 0; }
==7796== Memcheck, a memory error detector
==7796== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==7796== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==7796== Command: ./password 10
==7796==
--7796-- Valgrind options:
--7796-- --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--7796-- -v
--7796-- --leak-check=yes
--7796-- Contents of /proc/version:
--7796-- Linux version 3.11.0-12-generic (buildd@allspice) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu7) ) #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013
--7796-- Arch and hwcaps: AMD64, amd64-sse3-cx16-avx
--7796-- Page sizes: currently 4096, max supported 4096
--7796-- Valgrind library directory: /usr/lib/valgrind
--7796-- Reading syms from /home/dmitry/Desktop/password/password
--7796-- Reading syms from /lib/x86_64-linux-gnu/ld-2.17.so
--7796-- Considering /lib/x86_64-linux-gnu/ld-2.17.so ..
--7796-- .. CRC mismatch (computed 51e60a7a wanted 521e3c74)
--7796-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.17.so ..
--7796-- .. CRC is valid
--7796-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.17.so ..
--7796-- .. CRC is valid
--7796-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux
--7796-- Considering /usr/lib/valgrind/memcheck-amd64-linux ..
--7796-- .. CRC mismatch (computed c436d3d9 wanted 99aa3883)
--7796-- object doesn't have a symbol table
--7796-- object doesn't have a dynamic symbol table
--7796-- Scheduler: using generic scheduler lock implementation.
--7796-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--7796-- Reading suppressions file: /usr/lib/valgrind/default.supp
==7796== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-7796-by-dmitry-on-???
==7796== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-7796-by-dmitry-on-???
==7796== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-7796-by-dmitry-on-???
==7796==
==7796== TO CONTROL THIS PROCESS USING vgdb (which you probably
==7796== don't want to do, unless you know exactly what you're doing,
==7796== or are doing some strange experiment):
==7796== /usr/lib/valgrind/../../bin/vgdb --pid=7796 ...command...
==7796==
==7796== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==7796== /path/to/gdb ./password
==7796== and then give GDB the following command
==7796== target remote | /usr/lib/valgrind/../../bin/vgdb --pid=7796
==7796== --pid is optional if only one valgrind process is running
==7796==
--7796-- REDIR: 0x4018f40 (strlen) redirected to 0x3806e491 (???)
--7796-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so
--7796-- Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
--7796-- .. CRC mismatch (computed 826470ab wanted a8b7e9f3)
--7796-- object doesn't have a symbol table
--7796-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
--7796-- Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
--7796-- .. CRC mismatch (computed d4fd7975 wanted b90a895a)
--7796-- object doesn't have a symbol table
--7796-- REDIR: 0x4018db0 (index) redirected to 0x4c2d480 (index)
--7796-- REDIR: 0x4018e30 (strcmp) redirected to 0x4c2e560 (strcmp)
--7796-- Reading syms from /lib/x86_64-linux-gnu/libc-2.17.so
--7796-- Considering /lib/x86_64-linux-gnu/libc-2.17.so ..
--7796-- .. CRC mismatch (computed 2c8ee98b wanted da4a7364)
--7796-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.17.so ..
--7796-- .. CRC is valid
--7796-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.17.so ..
--7796-- .. CRC is valid
--7796-- REDIR: 0x4ec1980 (strcasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--7796-- REDIR: 0x4ebdd10 (strnlen) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--7796-- REDIR: 0x4ec3c50 (strncasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--7796-- REDIR: 0x4ec0790 (memset) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--7796-- REDIR: 0x4ec0740 (memcpy@GLIBC_2.2.5) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--7796-- REDIR: 0x4ebf700 (__GI_strrchr) redirected to 0x4c2d2a0 (__GI_strrchr)
--7796-- REDIR: 0x4eb7470 (malloc) redirected to 0x4c2a270 (malloc)
--7796-- REDIR: 0x4eb7900 (free) redirected to 0x4c2b5a0 (free)
==7796== Invalid read of size 1
==7796== at 0x400952: main (in /home/dmitry/Desktop/password/password)
==7796== Address 0x51fc2cb is 0 bytes after a block of size 11 alloc'd
==7796== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7796== by 0x4008AE: main (in /home/dmitry/Desktop/password/password)
==7796==
JrdDd7hUAO
==7796==
==7796== HEAP SUMMARY:
==7796== in use at exit: 0 bytes in 0 blocks
==7796== total heap usage: 2 allocs, 2 frees, 579 bytes allocated
==7796==
==7796== All heap blocks were freed -- no leaks are possible
==7796==
==7796== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
==7796==
==7796== 1 errors in context 1 of 1:
==7796== Invalid read of size 1
==7796== at 0x400952: main (in /home/dmitry/Desktop/password/password)
==7796== Address 0x51fc2cb is 0 bytes after a block of size 11 alloc'd
==7796== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7796== by 0x4008AE: main (in /home/dmitry/Desktop/password/password)
==7796==
--7796--
--7796-- used_suppression: 2 dl-hack3-cond-1
==7796==
==7796== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Решение задачи: «Генератор случайного пароля. Утечка памяти»
textual
Листинг программы
#include <stdio.h> #include <stdlib.h> #define DEFAULT_KEY_LENGTH 500 #define MAX_KEY_LENGTH 1000000 int main(int argc, char **argv) { FILE *fp; char *password; char ch; unsigned int i, length; fp = fopen( "/dev/urandom", "r" ); if( fp == NULL ) { fprintf( stderr, "Error while opening /dev/urandom\n" ); return 1; } length = DEFAULT_KEY_LENGTH; if( argc > 1 ) { length = atoi(argv[1]); if( length < 1 || length > MAX_KEY_LENGTH ) { fprintf( stderr, "Variable number must be between 1 and %d\n", MAX_KEY_LENGTH ); return 2; } } i = 0; password = calloc( length+1, sizeof(char) ); if (password == NULL) { fprintf( stderr, "Malloc error\n" ); return 3; } while (i <= length) { ch = fgetc(fp); if ( isalpha( ch ) ) { password[i] = ch; i++; } } fclose( fp ); printf("%s\n", password); free( password ); return 0; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д