【问题标题】:malloc(): corrupted top sizemalloc(): 损坏的顶部大小
【发布时间】:2019-05-26 21:16:43
【问题描述】:

我正在开发一个程序来解密文件中的某些文本行。首先,我创建的另一个源代码要求换档和一些文本。源代码加密文本并将其写入文件。

然后,我尝试使用以下代码解密指定的文件 - 另一个源代码。获得文件名。从文件名中得到移位。文件中写入的每个字符都被复制,借助我也有的函数 caesar_decrypt -reversing caesar encrypt 移动了 shift 的值。但是,给定字符的 ascii 值必须在 32 到 127 之间才能转换为另一个 ascii 值在 32 到 127 之间的字符。

我定义了我的模函数,它封装了余数运算符 - 正数的模 - 和负数的模。我还定义了 strip 函数,它的工作原理与 python 中的 strip 函数相同。除了空格,它会删除所有文字。我也有getIn(),就是python的input()。

我尝试通过打印和使用 gdb 进行调试。没有有效的结果。我在stackoverflow中研究了这个话题。找到一个条目。答案没有解决我的具体问题。

注意:我复制了整个程序,因为我认为您需要必要的详细信息。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>

/*GETIN FUNCTION*/
char* getIn() 
{
    char *line = NULL, *tmp = NULL;
    size_t size = 0, index = 0;
    int ch = EOF;

    while (ch) 
    {
        ch = getc(stdin);

        /* Check if we need to stop. */
        if (ch == EOF || ch == '\n')
        ch = 0;

        /* Check if we need to expand. */
        if (size <= index) 
        {
            size += sizeof(char);
            tmp = realloc(line, size);
            if (!tmp) 
            {
                free(line);
                line = NULL;
                break;
            }
        line = tmp;
        }

    /* Actually store the thing. */
    line[index++] = ch;
    }

    return line;
}
/*FUNCTION THAT CONVERTS TO NUMERIC CHARACTER, PRECURSOR OF DIGITS OF SHIFT*/
char make_numeric(char ch)
{
    if(ch<91 && ch>63) return ch-16;
    if(ch<123 && ch>95) return ch-48;
    if(ch<58 && ch>47) return ch;
    return ch+16;
}

 /*STRIP FUNCTION*/
char* strip(char* str,int length)
{
    char *start=str,*end=&str[length-1];

    while(*start>0 && *start<33 || *start==127) ++start;
    if(!*start) return start;

    while(*end>=0 && *end<33 || *end==127) --end;
    ++end;
    *end=0;

    return start;
}
/*DECRYPTOR FUNCTION*/
char *caesar_decrypt(char *message_to_decrypt, void *shift)
{   
    int length=strlen(message_to_decrypt),i;
    char* decrypted_message;

    if(!message_to_decrypt) return NULL;

    if(!(decrypted_message=calloc(length+1,sizeof(char)))) return NULL;

    for(i=0;i<length;++i)
    {
    decrypted_message[i]=mod((message_to_decrypt[i]-33-*((int*)shift)),94)+33;
    }

return decrypted_message;
}   

/*THE MAIN PROGRAM*/
int main()
{

int shift,len_chshift,init,len_filename,i=0;
char *chshift,*line=NULL,*decrypted_line,*stripped_line,chinit,chlen_chshift;
char *filename=NULL,*newfilename=NULL;
FILE *oldfile=NULL;
FILE *newfile=NULL;
size_t lenline,len=0;

printf("-9");
/*FILENAME*/
printf("Enter the file name: ");
/*printf("-8");*/
filename=getIn();
/*printf("-7");*/
if(!access(filename,F_OK))
{
    len_filename=strlen(filename);
    /*printf("-6");*/
    i=len_filename;
    while(filename[i]!='.') --i;        
    chlen_chshift=filename[i-1];
    chlen_chshift=make_numeric(chlen_chshift);
    len_chshift=chlen_chshift-48;
    /*printf("-5");*/
    chshift=calloc(len_chshift+1,1);
    /*NEWFILENAME*/
    newfilename=calloc(i+1,1);
    /*printf("-4");*/
    strncpy(newfilename,filename,i);
    /*printf("-3");*/
    strcat(newfilename,"(decrypted).txt");
    /*printf("-2");*/
    chinit=make_numeric(filename[0]);
    init=chinit-48;
    /*SHIFT*/
    i=0;
    /*printf("-1");*/
    while(i!=len_chshift)
    {
        chshift[i]=make_numeric(filename[(i+1)*init]);
        ++i;
    }
    /*printf("0");*/
    shift=atoi(chshift);

    /*printf("1");*/
    if(!(oldfile=fopen(filename,"r")))
    {
        perror("Error");
        if(newfilename) free(newfilename);
        if(filename) free(filename);
        if(chshift) free(chshift);
        exit(1);
    }
    /*printf("2");*/

    if(!(newfile=fopen(newfilename,"+ab")))
    {
        perror("Error");
        if(newfilename) free(newfilename);
        if(filename) free(filename);
        if(chshift) free(chshift);
        fclose(oldfile);
        exit(1);
    }
    while ((lenline = getline(&line, &len, oldfile)) != -1)
    {
        stripped_line=strip(line,lenline);
        decrypted_line=caesar_decrypt(stripped_line, &shift);
        if(!decrypted_line)
        {
            printf("Could not allocate memory\n");
            if(newfilename) free(newfilename);
            if(filename) free(filename);
            if(chshift) free(chshift);
            exit(1);
        }

        fprintf(newfile,"%s\n",decrypted_line);

        if(decrypted_line) free(decrypted_line);
        decrypted_line=NULL;

        free(line);
        line=NULL;
        stripped_line=NULL;
        decrypted_line=NULL;
    }
    free(line);
    line=NULL;
}
else
{
    printf("Cannot access the file.");
    if(filename) free(filename);
    exit(0);
}

if(newfilename) free(newfilename);
if(filename) free(filename);
if(chshift) free(chshift);

fclose(oldfile);
fclose(newfile);
return 0;
}

程序需要创建一个文件,其上下文是另一个文件的解密克隆,其名称由用户在开头提供。但是,它会在第一个 fopen 之前抛出:

malloc(): corrupted top size
Aborted (core dumped)

当我取消注释每个 printf 行时,它会在 getIn() 和 printf("-7"); 行之间给出相同的错误。当对 printfs 进行注释时,我观察到文件名包含写入的值,该值由 getIn() 返回。因此,我认为 getIn() 不是问题。

EDIT(1):我添加了 2 个缺少的标头,但仍然出现相同的错误。

EDIT(2):我删除了所有不必要的标题。

EDIT(3):既然有人要求,我就试着举例说明一下:假设我有一个文件名为

2/b*A".txt

两行代码

%52
abcd

我在程序中输入文件名。

1).之前的最后一个字符是",转换成数字2。我们理解shift是一个两位数。

2)第一个字符是发起者,也就是 2 ,是我们需要重复跳转的字符数。我们将跳过两个字符两次,因为我们有两位数的移位数。

3) 我们现在将字符 b 和 A 分别转换为 2 和 1。我们的移位是 21。第一个字符 %(37) 向后移动到 n(110) 等等,我们需要创建一个包含内容的新文件:

n~#
LMNO

感谢您的帮助。先感谢您。另外,我提到的变量是特定区间之间的任意字符。详情请查看源代码,或者我建议你在本地工作,因为我提供了整个程序。

对于 ascii 代码: https://www.asciitable.com/

如果您对凯撒密码感兴趣: https://en.wikipedia.org/wiki/Caesar_cipher

【问题讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • @HakanDemir 你的文件名不能包含/s。

标签: c segmentation-fault malloc caesar-cipher


【解决方案1】:

newfilename 中存在缓冲区溢出:

/* here you declare that the string pointed to by newfilename may contain `i`
   chars + zero-termination...*/ 
newfilename=calloc(i+1,1);

/* Here `i` is less than `strlen(filename)`, so only i characters from `filename`
   will be copied, that does NOT include the zero-termination
   (but it will be zero-terminated because the last byte in the buffer was 
   already cleared by `calloc`) */ 
strncpy(newfilename,filename,i);


/* Here `newfilename` is full, and `strcat` will write outside buffer!! Whatever
   happens after this line is undefined behaviour */   
strcat(newfilename,"(decrypted).txt");

所以你必须为newfilename分配足够的空间:

newfilename = calloc (i + strlen("(decrypted).txt") + 1, 1);

【讨论】:

  • 有可能newfilename 不是nullstrncpy 之后终止并且在strcat 上执行strcat 是未定义的行为。
  • @kiran。不,newfilname 以零结尾。因为calloc()strncpy()之前的所有字节都将为零,我们复制最大i字节,而缓冲区保存i+1字节。
  • 糟糕,我没有看到那部分。UV 用于单挑。
  • @kiran,我上面的评论是错误的(很久没有写答案了)。但声明表明,当使用calloc (i + strlen("(decrypted).txt") + 1, 1) 时,'newfilename` 为零终止。然后缓冲区将足够大以容纳strncpy()strcat() 和零终止。 i+1的buffersize是为了指出原代码的bug。
猜你喜欢
  • 2021-12-18
  • 2021-10-15
  • 2021-09-23
  • 2016-05-29
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多