gpt4 book ai didi

C 程序吃掉了我所有的 RAM+SWAP 资源

转载 作者:行者123 更新时间:2023-11-30 20:15:16 49 4
gpt4 key购买 nike

我做了一个简单的程序来找出用户输入的md5校验和(输入[1;6])问题是这些 for 循环似乎耗尽了我所有的 RAM + 交换资源,导调用脑挂起。我做错了什么?管理系统内存的更好方法是什么?

/* 

C Example w/o mpi

mpicc md5.c -o md5 -lcrypto -lssl
./md5

Single process on c2d laptop
String finding matchin md5 of string "hello" tooks

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif


char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);

MD5_Init(&c);

while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}

MD5_Final(digest, &c);

for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}

return out;
}

int main (int argc, char* argv[])
{

#ifdef COUNT // Very bad name, not long enough, too general, etc..
static int const count = COUNT;
#else
static int const count = 6; // default value
#endif
clock_t begin, end;
double time_spent;
begin = clock();

int bflag = 0;
int sflag = 0;
int index;
int c;
char input[count];
char action[2]; // char + \n
char *inputResult = (char*)malloc(33);
char *tmpResult = (char*)malloc(33);
char inputGuess[6];
int i,j,k,l,m,n;
char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

printf("Please enter string to guess (max 6 char - latin letters and numbers): ");
scanf("%s", input);
inputResult = str2md5(input, strlen(input));
printf("Md5 to find = %s \n", inputResult);
printf("Continue? (Y/n) ");
scanf("%s", action);
if(action == "n\n"){
return 0;
}


/* for 1 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
/* printf("%s\n", guess1);*/
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}

/* for 2 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}

/* for 3 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
char guess3[] = {letters[i], letters[j], letters[k], '\0'};
/* printf("%s\n", guess3);*/
if(strcmp(str2md5(guess3, strlen(guess3)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess3);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}

/* for 4 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
for(m=0; m<sizeof(letters); m++){
char guess4[] = {letters[i], letters[j], letters[k], letters[m], '\0'};
/* printf("%s\n", guess4);*/
if(strcmp(str2md5(guess4, strlen(guess4)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess4);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}

/* for 5 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
for(m=0; m<sizeof(letters); m++){
for(n=0; n<sizeof(letters); n++){
char guess5[] = {letters[i], letters[j], letters[k], letters[m], letters[n], '\0'};
/* printf("%s\n", guess5);*/
if(strcmp(str2md5(guess5, strlen(guess5)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess5);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
}

/* for 6 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
for(m=0; m<sizeof(letters); m++){
for(n=0; n<sizeof(letters); n++){
for(l=0; l<sizeof(letters); l++){
char guess6[] = {letters[i], letters[j], letters[k], letters[m], letters[n], letters[l], '\0'};
/* printf("%s\n", guess6);*/
if(strcmp(str2md5(guess6, strlen(guess6)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess6);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
}
}

return 0;
}

最佳答案

在不检查整个代码的情况下:您至少有一个 malloc没有相应的free .

str2md5out 分配 33 个字节返回的。有几行 strcmp(str2md5(guess3, strlen(guess3)),inputResult) 形式其中 malloc 产生的指针对于 out是暂时的、丢失的,因此永远无法再次释放。

考虑到您在多个嵌套循环中调用这些行,这可能会导致您的问题。

通过分配str2md5的结果可以避免这个问题。到本地指针并在执行 strcmp 后释放它。这是前面提到的行的示例:

char* str2md5_ret = str2md5(guess3, strlen(guess3));
int cmp = strcmp(str2md5_ret, inputResult);
free(str2md5_ret);
if(cmp == 0)
//...

再次查看代码,我发现了其他几个问题:

您分配给inputResult就在声明处,但随后您从 str2md5 分配一个新数据 block 到它。所以又有 33 个字节被泄露。然后你分配 tmpResult它没有被释放,实际上甚至从未被再次使用过。这又是 33 字节的泄露。 inputResult如果没有任何循环中断,则不会被释放。

正如其他人指出的那样,深度嵌套循环是不好的风格。实际上,您可以使用单个循环将它们全部编写在单个递归函数中。这可能更具可读性、可调试性、可维护性和灵 active 。

您还可以删除几乎所有 malloc是通过保留一个 char*所有 str2md5 的输出长度为 33 个字节调用并将其作为参数传递而不是返回它。

关于C 程序吃掉了我所有的 RAM+SWAP 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818410/

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