来自根本未调用的函数的分段错误

来自根本未调用的函数的分段错误,第1张

概述好吧,这真的吓坏了我.我有一个以下函数,只读取输入并返回一个字符串 unsigned char* readFromIn() { unsigned char* text = malloc(1024); if (fgets(text, 1024, stdin) != NULL) { <--This is what's causing segmentation faul 好吧,这真的吓坏了我.我有一个以下函数,只读取输入并返回一个字符串
unsigned char* readFromIn() {    unsigned char* text = malloc(1024);    if (fgets(text,1024,stdin) != NulL) {          <--This is what's causing segmentation fault        int textLen = strlen(text);        if (textLen > 0 && text[textLen - 1] == '\n')            text[textLen - 1] = '
if (fgets(text,stdin) != NulL) {
'; // getting rID of newline character return text; } else { free(text); return NulL; }}

问题是,这个函数不会在任何地方被调用,只是为了确认,我将函数的名称更改为像9rawiohawr90awrhiokawrioawr这样的东西,并将printf语句放在函数的顶部.

我真的不确定为什么未调用的函数可能会导致分段错误错误.

我在ubuntu上使用gcc 4.6.3.

编辑:我知道那条线

#0 0xb7fa5ac2 in _IO_2_1_stdin_ () from /lib/i386-linux-gnu/libc.so.6#1 0xb7faf2fb in libwebsocket_create_context (info=0xbffff280) at libwebsockets.c:2125#2 0x0804a5bb in main()

是有问题的代码,因为只要我注释掉条件,就不会发生分段错误.

我知道函数没有被调用,因为我没有看到我输出的printf调试语句的输出.

Edit2:我尝试将类型从unsigned char更改为char.仍然分段错误.我会尝试获取gdb输出.

Edit3:gdb backtrace产生了以下内容

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <libwebsockets.h>unsigned char* readFromIn() {    unsigned char* text = malloc(1024);    if (fgets(text,stdin) != NulL) { <--SEGMENTATION FAulT HERE        int textLen = strlen(text);        if (textLen > 0 && text[textLen - 1] == '\n')            text[textLen - 1] = '
gcc -g testBUG.c -o test -lwebsockets
'; return text; } else { free(text); return NulL; }}int callback_http(struct libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,voID *user,voID *in,size_t len){ return 0;}static struct libwebsocket_protocols protocols[] = { /* first protocol must always be http handler */ { "http-only",// name callback_http,// callback 0 // per_session_data_size }};int main(voID) { printf("Initializing Web Server\n"); // server url will be http://localhost:8081 int port = 8081; const char *interface = NulL; struct libwebsocket_context *context; // we're not using ssl const char *cert_path = NulL; const char *key_path = NulL; // no special options int opts = 0; struct lws_context_creation_info info; memset(&info,sizeof info); info.port = port; info.iface = interface; info.protocols = protocols; info.extensions = libwebsocket_get_internal_extensions(); info.ssl_cert_filepath = NulL; info.ssl_private_key_filepath = NulL; info.gID = -1; info.uID = -1; info.options = opts; context = libwebsocket_create_context(&info); if (context == NulL) { fprintf(stderr,"libwebsocket init Failed\n"); return 0; } printf("starting server...\n"); while (1) { libwebsocket_service(context,50); } printf("Shutting server down...\n"); libwebsocket_context_destroy(context); return 0;}

做框架0,1,2特别没有输出任何有趣的东西.

编辑4:我已经尝试了评论中的所有建议,但无济于事,我仍然得到相同的分段错误.

所以我在虚拟 *** 作系统上安装了一个新的Ubuntu副本并​​重新编译了我的代码.仍然出现同样的问题.
在我看来,问题在于我的代码或库本身是否存在一些模糊不清.我创建了一个演示问题的最小例子:

(gdb) runStarting program: /home/l46kok/Desktop/websocketserver/test Initializing Web Server[1384002761:2270] NOTICE: Initial logging level 7[1384002761:2270] NOTICE: library version: 1.3 unkNown-build-hash[1384002761:2271] NOTICE:  Started with daemon pID 0[1384002761:2271] NOTICE:  static allocation: 4448 + (12 x 1024 fds) = 16736 bytes[1384002761:2271] NOTICE:  canonical_hostname = ubuntu[1384002761:2271] NOTICE:  Compiled with OpenSSL support[1384002761:2271] NOTICE:  Using non-SSL mode[1384002761:2271] NOTICE:  per-conn mem: 124 + 1360 headers + protocol rx buf[1384002761:2294] NOTICE:  Listening on port 8081Program received signal SIGSEGV,Segmentation fault.0xb7fb1ac0 in _IO_2_1_stdin_ () from /lib/i386-linux-gnu/libc.so.6(gdb) backtrace#0  0xb7fb1ac0 in _IO_2_1_stdin_ () from /lib/i386-linux-gnu/libc.so.6#1  0xb7fcc2c6 in libwebsocket_create_context () from /usr/local/lib/libwebsockets.so.4.0.0#2  0x080488c4 in main () at testBUG.c:483(gdb) frame 1#1  0xb7fcc2c6 in libwebsocket_create_context () from /usr/local/lib/libwebsockets.so.4.0.0(gdb) frame 2#2  0x080488c4 in main () at testBUG.c:483483         context = libwebsocket_create_context(&info);

这是我编译代码的方式

GNU gdb (GDB) 7.6.1 (Debian 7.6.1-1)copyright (C) 2013 Free Software Foundation,Inc.license GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.HTML>This is free software: you are free to change and redistribute it.There is NO WARRANTY,to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-linux-gnu".For BUG reporting instructions,please see:<http://www.gnu.org/software/gdb/BUGs/>...Reading symbols from /home/vili/x...done.(gdb) rStarting program: /home/vili/./xwarning: Could not load shared library symbols for linux-vdso.so.1.Do you need "set solib-search-path" or "set sysroot"?Initializing Web Server[1384020141:5692] NOTICE: Initial logging level 7[1384020141:5692] NOTICE: library version: 1.2[1384020141:5693] NOTICE:  Started with daemon pID 0[1384020141:5693] NOTICE:  static allocation: 5512 + (16 x 1024 fds) = 21896 bytes[1384020141:5693] NOTICE:  canonical_hostname = x220[1384020141:5693] NOTICE:  Compiled with OpenSSL support[1384020141:5693] NOTICE:  Using non-SSL mode[1384020141:5693] NOTICE:  per-conn mem: 248 + 1328 headers + protocol rx buf[1384020141:5713] NOTICE:  Listening on port 8081Program received signal SIGSEGV,Segmentation fault.0x00007ffff7bc2080 in _IO_2_1_stderr_ () from /lib/x86_64-linux-gnu/libc.so.6(gdb) bt#0  0x00007ffff7bc2080 in _IO_2_1_stderr_ () from /lib/x86_64-linux-gnu/libc.so.6#1  0x00007ffff7bcd83c in libwebsocket_create_context (info=0x7fffffffe580)    at libwebsockets.c:2093#2  0x0000000000400918 in main () at x.c:66(gdb) up#1  0x00007ffff7bcd83c in libwebsocket_create_context (info=0x7fffffffe580)    at libwebsockets.c:20932093                    info->protocols[context->count_protocols].callback(context,(gdb) p context->count_protocols = 1(gdb) p info->protocols[1] = {  name = 0x7ffff7bc2240 <_IO_2_1_stdin_> "0 5",<incomplete sequence 3>,callback = 0x7ffff7bc2080 <_IO_2_1_stderr_>,per_session_data_size = 140737349689696,rx_buffer_size = 0,owning_server = 0x602010,protocol_index = 1}(gdb)

这是我正在使用的库

http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/tag/?id=v1.23-chrome32-firefox24

您将看到我没有调用函数readFromIn(),一旦您尝试运行可执行文件就会发生分段错误.

我重新运行gdb,这次,回溯和帧告诉我更多的信息.

所以是啊..我想我已经提供了所有信息……但我真的不确定问题是什么.该程序在第483行导致分段错误,但当我注释掉未被调用的有问题的函数时问题就消失了.

解决方法 可能你在初始化libwebsockets时遗漏了一些东西.

实际上,使用调试重新编译libwebsockets表明:

您可能需要使用特殊条目(NulL)关闭libwebsocket_protocols数组,以便lib知道它通过info->协议获得了多少条目.

编辑:是的,检查文档:http://jsk.pp.ua/knowledge/libwebsocket.html

Array of structures Listing supported protocols and a protocol- specific callback for each one. The List is ended with an entry that has a NulL callback pointer.

总结

以上是内存溢出为你收集整理的来自根本未调用的函数的分段错误全部内容,希望文章能够帮你解决来自根本未调用的函数的分段错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1256770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存