由mkfifo()在C中生成的分段错误

时间:2015-12-26 17:46:29

标签: c segmentation-fault mkfifo

我一直在尝试调试这几个小时,我仍然被卡住了......

我在这段代码中遇到了一个“mkfifo”调用的分段错误(它只是我整个代码的一部分,因为我认为其余部分与此无关):

#include "marketfunc.h"
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define PIPE_PATH "./pipe.fifo"

struct myStruct
{
    int x;      
    int y;
    int z;  
};


struct myStruct *s;

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

    s = malloc(sizeof(struct myStruct));

    // 'int marketfunc_init(int x)' is defined in a perfectly working extern library
    if(marketfunc_init(1) == -1) error("failed to initialize marketfunc library", 5);

    printf("test1\n");

    // Segmentation fault
    if(mkfifo(PIPE_PATH, 0666) == -1) error("failed to create pipe", 1);

    printf("test2\n");

    //...

}

产生此输出(executableFile是我文件的名称):

test1
bin/executableFile: Segmentation fault (core dumped)

gdb backtrace产生了这个:

#0  strchrnul () at ../sysdeps/x86_64/strchr.S:32
#1  0x00007ffff7a5ed82 in __find_specmb (format=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>)
    at printf-parse.h:108
#2  _IO_vfprintf_internal (s=0x7fffffffb5a0, format=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, 
    ap=0x7fffffffdd58) at vfprintf.c:1332
#3  0x00007ffff7a63f31 in buffered_vfprintf (s=s@entry=0x7ffff7dd41c0 <_IO_2_1_stderr_>, 
    format=format@entry=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, args=args@entry=0x7fffffffdd58)
    at vfprintf.c:2356
#4  0x00007ffff7a5eeae in _IO_vfprintf_internal (s=0x7ffff7dd41c0 <_IO_2_1_stderr_>, 
    format=format@entry=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, ap=ap@entry=0x7fffffffdd58)
    at vfprintf.c:1313
#5  0x00007ffff7b0c595 in error_tail (status=status@entry=4199947, errnum=errnum@entry=1, 
    message=message@entry=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>, args=args@entry=0x7fffffffdd58)
    at error.c:201
#6  0x00007ffff7b0c6ed in __error (status=4199947, errnum=1, 
    message=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>) at error.c:251
#7  0x0000000000400b78 in main (argc=1, argv=0x7fffffffdf38) at src/executableFile.c:75

虽然创建了“pipe.fifo”文件... 在此先感谢您的帮助!

编辑:

错误在error.c中简单定义,并且在error.h中是签名:

#include "error.h"
#include <stdlib.h>
#include <stdio.h>

void error(char *msg, int ret)
{
    perror(msg);
    exit(ret);
}

1 个答案:

答案 0 :(得分:0)

如果查看堆栈跟踪,您会看到对error()的调用显示为:

0x00007ffff7b0c6ed in __error (status=4199947, errnum=1, message=0xffffffffffffff60 <error: Cannot access memory at address 0xffffffffffffff60>) at error.c:251

这是 NOT 您定义的error()函数。相反,它是error()中定义的error.h函数,具有以下签名:

void error(int status, int errnum, const char *format, ...);

请参阅error.h

正如您所看到的,此函数需要char* format作为最后一个参数,它会从堆栈0xffffffffffffff60中获取一些垃圾,因为您根本不传递第三个参数。似乎链接器正在解析对error()的错误函数的调用。

作为快速修复,我会按如下方式重命名:

rename your file `error.h` to `error_my.h`

your definition of `error()` to, say, `error_my()`

replace the calls to `error()` with `error_my()`.

您的代码如下:

#include "marketfunc.h"
#include "error_my.h" // <=======================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define PIPE_PATH "./pipe.fifo"

struct myStruct
{
    int x;      
    int y;
    int z;  
};


struct myStruct *s;

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

    s = malloc(sizeof(struct myStruct));

    // 'int marketfunc_init(int x)' is defined in a perfectly working extern library
    if(marketfunc_init(1) == -1) error_my("failed to initialize marketfunc library", 5); // <=======================

    printf("test1\n");

    // Segmentation fault
    if(mkfifo(PIPE_PATH, 0666) == -1) error_my("failed to create pipe", 1); // <=======================

    printf("test2\n");

    //...

}

errnomkfifo()未能找到失败的原因时打印<%= form_tag img_upload_create_path, method: "POST", html: { multipart: true }