【问题标题】:Why would linking against the FastCGI library cause a segfault?为什么链接 FastCGI 库会导致段错误?
【发布时间】:2013-11-26 02:19:48
【问题描述】:

我有一个程序正在使用一个名为“wjelement”的库,每当我尝试将此库与 FastCGI 一起使用时,都会出现段错误。我在下面做了一个简化的测试用例。如果我在没有 fcgi_stdio.h 的情况下编译代码并且不链接到库,则代码可以正常工作,如果我添加 fastcgi 标头并链接到它,我会得到一个段错误,即使我不使用任何快速 cgi 调用。

在我的 FastCGI 代码中,情况正好相反,如果我删除 WJelement 代码,程序的其余部分就可以正常工作。

我不确定是否需要责怪我的程序、FastCGI 库或 WJElement 库...

#include <stdio.h>
#include <fcgi_stdio.h>
#include <wjreader.h>

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

    FILE *my_schema_file;
    my_schema_file = fopen("test_schema.json", "rb");

    if (my_schema_file == NULL) {
        printf("Failed to open test schema file\n");
        return 1;
    } else {
        printf("Opened test schema file\n");
    }

    WJReader my_schema_reader;
    my_schema_reader = WJROpenFILEDocument(my_schema_file, NULL, 0);

    if (my_schema_reader  == NULL) {
        printf("Failed to open test schema reader\n");
        return 1;
    } else {
        printf("Opened test schema reader\n");
    }

    return 0;
}

GDB 回溯:

Program received signal SIGSEGV, Segmentation fault.
0x0000003e19e6c85f in __GI__IO_fread (buf=0x6023c4, size=1, count=2731, fp=0x602250) at iofread.c:41
41    _IO_acquire_lock (fp);
(gdb) backtrace
#0  0x0000003e19e6c85f in __GI__IO_fread (buf=0x6023c4, size=1, count=2731, fp=0x602250) at iofread.c:41
#1  0x00007ffff7dde5d9 in WJRFileCallback () from /lib/libwjreader.so.0
#2  0x00007ffff7dde037 in WJRFillBuffer () from /lib/libwjreader.so.0
#3  0x00007ffff7dde4e9 in _WJROpenDocument () from /lib/libwjreader.so.0
#4  0x000000000040081f in main (argc=1, argv=0x7fffffffdeb8) at test.c:20

【问题讨论】:

  • 可能涉及的头文件之一重新定义了FILE?

标签: c fastcgi


【解决方案1】:

在这里找到答案:http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm

如果您的应用程序将 FILE * 传递给在您没有源代码的库中实现的函数,那么您需要在包含 fcgi_stdio.h 之前包含这些库的标头

然后我必须使用 FCGI_ToFILE(FCGI_FILE *); 将 FCGI_FILE * 转换为 FILE *;

#include <stdio.h>
#include <wjreader.h>
#include <fcgi_stdio.h>

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

    FILE *my_schema_file;
    my_schema_file = fopen("test_schema.json", "rb");

    if (my_schema_file == NULL) {
        printf("Failed to open test schema file\n");
        return 1;
    } else {
        printf("Opened test schema file\n");
    }

    WJReader my_schema_reader;
    my_schema_reader = WJROpenFILEDocument(FCGI_ToFILE(my_schema_file), NULL, 0);

    if (my_schema_reader  == NULL) {
        printf("Failed to open test schema reader\n");
        return 1;
    } else {
        printf("Opened test schema reader\n");
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2013-03-01
    • 1970-01-01
    • 2021-04-22
    • 2021-07-18
    • 2020-04-15
    相关资源
    最近更新 更多