简体   繁体   English

Pipe 的写入覆盖了分配的空间 memory

[英]Pipe's write overwrites an allocated space of memory

My program it's pretty big, so I'll highlight the main problem and add some details about it.我的程序很大,所以我会突出显示主要问题并添加一些相关细节。

First part of my code:我的代码的第一部分:

int myPipe[2]; //A global variable, so I don't have to pass it to future functions

int main(int argc, char *args[]) 
{
    mode_t pUmask = umask(0000); //Obsolete variable to my problem
    errno = 0; //Obsolete variable to my problem
    char pattern[2500] = "Test1"; //Obsolete variable to my problem
    int p = 0;              //DEFAULT NUMBER OF PROCESSES
    int deep = 0; //Obsolete variable to my problem
    int n = 1; //Obsolete variable to my problem

    if(pipe(myPipe))
    {
        perror("Pipe Error: ");
        exit(-1);
    }
    
    if( (write(myPipe[1], &p, (sizeof(int)*3))) == -1) //First write works
    {
        perror("write: ");
        exit(-1);
    }
    //Then a bunch of code releated to file reading
}

Second part:第二部分:

{
    //in another function
    //The part where I create fileName
    char* fileName = calloc(strlen(fileData->d_name)+4, sizeof(char));
    strcpy(fileName, fileData->d_name);
}

Third part:第三方:

//in another another function
if(S_ISREG(data.st_mode))
{
    printf("\tfileName: %s\n", fileName); //Regular print of the right fileName
    printf("\t\tOh boy! It's a regular.\n");
    printf("\tfileName: %s\n", fileName); //Regular print of the right fileName

    if((read(myPipe[0], &p, (sizeof(int)*3))) == -1) //First time I read
    {
        perror("\t\t read: ");
        exit(-1);
    } 
    printf("fileName: %s", fileName); //SEGMENTATION FAULT

There is a bunch of code in between, but it doesn't affect the fileName at all (in fact, up until the "read", fileName was printed flawlessly), and after it a SEGMENTATION FAULT happens.中间有一堆代码,但它根本不影响文件名(事实上,直到“读取”,文件名被完美地打印出来),然后发生分段错误。

At one point by changing the printfs locations I was able to get the fileName AFTER the read, which was basically the fileName value("File1") followed by the p integer value(0), which created the new corrupted fileName("File10").有一次通过更改 printfs 位置我能够在读取后获得文件名,这基本上是文件名值(“File1”)后跟 p integer 值(0),它创建了新的损坏文件名(“File10” ).

So what's happening?那么发生了什么事? I reserved the space for fileName, I passed the fileName pointer to the following functions up to that read, and supposedly the fd should have it's own adress space as well.我为 fileName 保留空间,我将 fileName 指针传递给以下函数直到读取,并且 fd 应该也有它自己的地址空间。 HELP.帮助。

Ps if you need more info, I'm willing to give it to you, even the full code, but it's REALLY complicated, and I think I gave you enough proof that fileName doesn't get corrupted at all until the read part, THANK YOU. Ps 如果你需要更多信息,我愿意给你,甚至是完整的代码,但它真的很复杂,我想我给了你足够的证据证明文件名在读取部分之前根本不会损坏,谢谢你。

Pps I never close either of the "MyPipe" extremes, since I have to use them multiple times, I wanted to close them at the end of the program. Pps 我从不关闭任何一个“MyPipe”极端,因为我必须多次使用它们,所以我想在程序结束时关闭它们。

The statements that write and read the pipe are causing undefined behavior.写入和读取 pipe 的语句导致未定义的行为。 p is declared: p声明:

int p;

But when you write and read it through the pipe, you use sizeof(int)*3 , so you're accessing outside the object.但是当你通过 pipe 写入和读取它时,你使用sizeof(int)*3 ,所以你在 object 之外访问。

Change those statements to use just sizeof p .将这些语句更改为仅使用sizeof p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM