这里的所有专家......
我在Linux上使用c ++中的自写readline方法时遇到了很多麻烦。它会不时抛出异常,SEGMATATION FAULT。我知道似乎readline写入了不允许的内存,它发生在来自tcp流的行被破坏时。有人可以帮助我纠正读取线以使其更安全,但它不应该慢得多,我需要读取从流中分割的\ r \ n行中的性能。另一种可能性是纠正读取线不抛出SEG FAULT或者能够在不杀死应用程序的情况下捕获SEG FAULT(如在Windows中尝试catch)。
以下是Readline的代码:
int readLineChatPri(std::string* line, int sock)
{
static std::string buffer1;
std::string::iterator pos;
int n = 0;
try
{
char buf[MAXRECV];
while ((pos = find(buffer1.begin(), buffer1.end(), '\n')) == buffer1.end())
{
n = read(sock, buf, MAXRECV - 1);
if (n == 0)
return n;
if (n == -1)
{
*line = buffer1;
buffer1 = "";
return n; // Error Handling
}
buf [n] = 0;
buffer1 += buf;
}
*line = string(buffer1.begin(), pos); // pos + 1 with \r\n pos with \n pos -1 without \r\n
buffer1 = string(pos + 1, buffer1.end());
return n;
}
catch (exception& e)
{
logErr.WriteLogEntry("Error in <readLineChatPri 1000> : " + boost::lexical_cast<string>(e.what()));
return -1;
}
}
如果您需要进一步提供信息,希望有人可以帮助我。
Thanx Sascha
以下是此类崩溃后的gdb输出:
*** glibc detected *** /root/Projects/EdsXInquiry/bin/Debug/EdsXInquiry: double free or corruption (!prev): 0x00007fffbc001d80 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3ffaa760e6]
/lib64/libc.so.6[0x3ffaa78c13] /usr/lib64/libstdc++.so.6(_ZNSs6assignERKSs+0x85) 0x3006a9d565]
/root/Projects/EdsXInquiry/bin/Debug/EdsXInquiry[0x433dbc] /root/Projects/EdsXInquiry/bin/Debug/EdsXInquiry[0x486187] /root/Projects/EdsXInquiry/bin/Debug/EdsXInquiry[0x4f6791]
/usr/lib64/libboost_thread-mt.so.5(thread_proxy+0x77)[0x7ffff7dd8d47] /lib64/libpthread.so.0[0x3ffb207851]
/lib64/libc.so.6(clone+0x6d)[0x3ffaae890d]
======= Memory map: ======== 00400000-0058c000 r-xp 00000000 08:03 7734460 /root/Projects/EdsXInquiry/bin/Debug/EdsXInquiry 0078c000-0078f000 rw-p 0018c000 08:03 7734460
/root/Projects/EdsXInquiry/bin/Debug/EdsXInquiry 0078f000-00e34000 rw-p 00000000 00:00 0 [heap] 3003200000-3003216000 r-xp 00000000 08:03 1573324 /lib64/libgcc_s-4.4.7-20120601.so.1 3003216000-3003415000 ---p 00016000 08:03 1573324 /lib64/libgcc_s-4.4.7-20120601.so.1 3003415000-3003416000 rw-p 00015000 08:03 1573324 /lib64/libgcc_s-4.4.7-20120601.so.1 3004a00000-3004a07000 r-xp 00000000 08:03 1573095 /lib64/libcrypt-2.12.so 3004a07000-3004c07000 ---p 00007000 08:03 1573095 /lib64/libcrypt-2.12.so 3004c07000-3004c08000 r--p 00007000 08:03 1573095 /lib64/libcrypt-2.12.so 3004c08000-3004c09000 rw-p 00008000 08:03 1573095 /lib64/libcrypt-2.12.so 3004c09000-3004c37000 rw-p 00000000 00:00 0 3005200000-300525d000 r-xp 00000000 08:03 1573093
/lib64/libfreebl3.so 300525d000-300545c000 ---p 0005d000 08:03 1573093
/lib64/libfreebl3.so 300545c000-300545d000 r--p 0005c000 08:03 1573093
/lib64/libfreebl3.so 300545d000-300545e000 rw-p 0005d000 08:03 1573093
/lib64/libfreebl3.so 300545e000-3005462000 rw-p 00000000 00:00 0 3006600000-3006603000 r-xp 00000000 08:03 1573299
/lib64/libcom_err.so.2.1 3006603000-3006802000 ---p 00003000 08:03 1573299 /lib64/libcom_err.so.2.1 3006802000-3006803000 r--p 00002000 08:03 1573299 /lib64/libcom_err.so.2.1 3006803000-3006804000 rw-p 00003000 08:03 1573299 /lib64/libcom_err.so.2.1 3006a00000-3006ae8000 r-xp 00000000 08:03 3045411 /usr/lib64/libstdc++.so.6.0.13 3006ae8000-3006ce8000 ---p 000e8000 08:03 30454[New Thread 0x7fffad7fb700 (LWP 6046)] 11
/usr/lib64/libstdc++.so.6.0.13 3006ce8000-3006cef000 r--p 000e8000 08:03 3045411 /usr/lib64/libstdc++.so.6.0.13 3006cef000-3006cf1000 rw-p 000ef000 08:03 3045411 /usr/lib64/libstdc++.so.6.0.13 3006cf1000-3006d06000 rw-p 00000000 00:00 0 3006e00000-3006e02000 r-xp 00000000 08:03 1573294
/lib64/libkeyutils.so.1.3 3006e02000-3007001000 ---p 00002000 08:03 1573294 /lib64/libkeyutils.so.1.3 3007001000-3007002000 r--p 00001000 08:03 1573294 /lib64/libkeyutils.so.1.3 3007002000-3007003000 rw-p 00002000 08:03 1573294 /lib64/libkeyutils.so.1.3 3007200000-3007374000 r-xp 00000000 08:03 3045375 /usr/lib64/libcrypto.so.1.0.0 3007374000-3007573000 ---p 00174000 08:03 3045375 /usr/lib64/libcrypto.so.1.0.0 3007573000-300758c000 r--p 00173000 08:03 3045375 /usr/lib64/libcrypto.so.1.0.0 300758c000-3007596000 rw-p 0018c000 08:03 3045375 /usr/lib64/libcrypto.so.1.0.0 3007596000-300759a000 rw-p 00000000 00:00 0 3007600000-300760a000 r-xp 00000000 08:03 1573297
/lib64/libkrb5support.so.0.1 300760a000-3007809000 ---p 0000a000 08:03 1573297 /lib64/libkrb5support.so.0.1 3007809000-300780a000 r--p 00009000 08:03 1573297 /lib64/libkrb5support.so.0.1 300780a000-300780b000 rw-p 0000a000 08:03 1573297 /lib64/libkrb5support.so.0.1 3007a00000-3007adb000 r-xp 00000000 08:03 1573300 /lib64/libkrb5.so.3.3 3007adb000-3007cda000 ---p 000db000 08:03 1573300 /lib64/libkrb5.so.3.3 3007cda000-3007ce4000 r--p 000da000 08:03 1573300 /lib64/libkrb5.so.3.3 3007ce4000-3007ce6000 rw-p 000e4000 08:03 1573300 /lib64/libkrb5.so.3.3 3007e00000-3007e29000 r-xp 00000000 08:03 1573298 /lib64/libk5crypto.so.3.1 3007e29000-3008029000 ---p 00029000 08:03 1573298 /lib64/libk5crypto.so.3.1 3008029000-300802a000 r--p 00029000 08:03 1573298 /lib64/libk5crypto.so.3.1 300802a000-300802b000 rw-p 0002a000 08:03 1573298 /lib64/libk5crypto.so.3.1 300802b000-300802c000 rw-p 00000000 00:00 0 3008600000-3008641000 r-xp 00000000 08:03 1573313
/lib64/libgssapi_krb5.so.2.2 3008641000-3008841000 ---p 00041000 08:03 1573313 /lib64/libgssapi_krb5.so.2.2 3008841000-3008842000 r--p 00041000 08:03 1573313 /lib64/libgssapi_krb5.so.2.2 3008842000-3008844000 rw-p 00042000 08:03 1573313 /lib64/libgssapi_krb5.so.2.2 3008e00000-3008e55000 r-xp 00000000 08:03 3045376 /usr/lib64/libssl.so.1.0.0 3008e55000-3009055000 ---p 00055000 08:03 3045376 /usr/lib64/libssl.so.1.0.0 3009055000-3009058000 r--p 00055000 08:03 3045376 /usr/lib64/libssl.so.1.0.0 3009058000-300905d000 rw-p 00058000 08:03 3045376 /usr/lib64/libssl.so.1.0.0 3009600000-3009609000 r-xp 00000000 08:03 3017438 /usr/lib64/libltdl.so.7.2.1 3009609000-3009808000 ---p 00009000 08:03 3017438 /usr/lib64/libltdl.so.7.2.1 3009808000-3009809000 rw-p 00008000 08:03 3017438 /usr/lib64/libltdl.so.7.2.1 300b200000-300b216000 r-xp 00000000 08:03 1573098 /lib64/libnsl-2.12.so 300b216000-300b415000 ---p 00016000 08:03 1573098 /lib64/libnsl-2.12.so 300b415000-300b416000 r--p 00015000 08:03 1573098 /lib64/libnsl-2.12.so 300b416000-300b417000 rw-p 00016000 08:03 1573098 /lib64/libnsl-2.12.so 300b417000-300b419000 rw-p 00000000 00:00 0 300c600000-300c614000 r-xp 00000000 08:03 3017764
/usr/lib64/libboost_filesystem-mt.so.5 300c614000-300c814000 ---p 00014000 08:03 3017764 /usr/lib64/libboost_filesystem-mt.so.5 300c814000-300c815000 rw-p 00014000 08:03 3017764 /usr/lib64/libboost_filesystem-mt.so.5 300ca00000-300ca03000 r-xp 00000000 08:03 3025260 /usr/lib64/libboost_system-mt.so.5 300ca03000-300cc02000 ---p 00003000 08:03 3025260 /usr/lib64/libboost_system-mt.so.5 300cc02000-300cc03000 rw-p 00002000 08:03 3025260 /usr/lib64/libboost_system-mt.so.5 3ffa200000-3ffa220000 r-xp 00000000 08:03 1572952 /lib64/ld-2.12.so 3ffa41f000-3ffa420000 r--p 0001f000 08:03 1572952 /lib64/ld-2.12.so 3ffa420000-3ffa421000 rw-p 00020000 08:03 1572952
/lib64/ld-2.12.so 3ffa421000-3ffa422000 rw-p 00000000 00:00 0 3ffa600000-3ffa615000 r-xp 00000000 08:03 1573311
/lib64/libz.so.1.2.3 3ffa615000-3ffa814000 ---p 00015000 08:03 1573311
/lib64/libz.so.1.2.3 3ffa814000-3ffa815000 r--p 00014000 08:03 1573311
/lib64/libz.so.1.2.3 3ffa815000-3ffa816000 rw-p 00015000 08:03 1573311
/lib64/libz.so.1.2.3 3ffaa00000-3ffab8a000 r-xp 00000000 08:03 1572972
/lib64/libc-2.12.so 3ffab8a000-3ffad89000 ---p 0018a000 08:03 1572972
/lib64/libc-2.12.so 3ffad89000-3ffad8d000 r--p 00189000 08:03 1572972
/lib64/libc-2.12.so 3ffad8d000-3ffad8e000 rw-p 0018d000 08:03 1572972
/lib64/libc-2.12.so 3ffad8e000-3ffad93000 rw-p 00000000 00:00 0 3ffae00000-3ffae02000 r-xp 00000000 08:03 1572980
/lib64/libdl-2.12.so 3ffae02000-3ffb002000 ---p 00002000 08:03 1572980
/lib64/libdl-2.12.so 3ffb002000-3ffb003000 r--p 00002000 08:03 1572980
/lib64/libdl-2.12.so 3ffb003000-3ffb004000 rw-p 00003000 08:03 1572980
/lib64/libdl-2.12.so 3ffb200000-3ffb217000 r-xp 00000000 08:03 1572976
/lib64/libpthread-2.12.so 3ffb217000-3ffb417000 ---p 00017000 08:03 1572976 /lib64/libpthread-2.12.so 3ffb417000-3ffb418000 r--p 00017000 08:03 1572976 /lib64/libpthread-2.12.so 3ffb418000-3ffb419000 rw-p 00018000 08:03 1572976 /lib64/libpthread-2.12.so Program received signal SIGABRT, Aborted. [Switching to Thread 0x7fffae1fc700 (LWP 4602)] 0x0000003ffaa328a5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 64
return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
Missing separate debuginfos, use: debuginfo-install libtool-ltdl-2.2.6-15.5.el6.x86_64 (gdb) #backtrace
(gdb) bacjtrace Undefined command: "bacjtrace". Try "help".
(gdb) backtrace
`#0 0x0000003ffaa328a5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64`
`#1 0x0000003ffaa34085 in abort () at abort.c:92`
`#2 0x0000003ffaa707b7 in __libc_message (do_abort=2, fmt=0x3ffab57f80 "*** glibc detected *** %s: %s: 0x%s ***\n")
at ../sysdeps/unix/sysv/linux/libc_fatal.c:198`
`#3 0x0000003ffaa760e6 in malloc_printerr (action=3, str=0x3ffab582e0 "double free or corruption (!prev)", ptr=<value optimized out>) at malloc.c:6311`
`#4 0x0000003ffaa78c13 in _int_free (av=0x7fffbc000020, p=0x7fffbc001d70, have_lock=0) at malloc.c:4811`
`#5 0x0000003006a9d565 in _M_dispose (this=0x78fc50, __str=<value optimized out>)
at /usr/src/debug/gcc-4.4.7-20120601/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:236`
`#6 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign (this=0x78fc50, __str=<value optimized out>)
at /usr/src/debug/gcc-4.4.7-20120601/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:251 `
`#7 0x0000000000433dbc in readLineChatSec (line=0x7fffae1fb7b0, sock=244) at /root/Projects/EdsXInquiry/main.cpp:510 `
`#8 0x0000000000486187 in secondaryChatListenerWorker () at /root/Projects/EdsXInquiry/main.cpp:4870 `
`#9 0x00000000004f6791 in boost::detail::thread_data<void (*)()>::run (this=0x7fffb0000ba0) at /usr/include/boost/thread/detail/thread.hpp:56 `
`#10 0x00007ffff7dd8d47 in boost::(anonymous namespace)::thread_proxy (param=<value optimized out>)
at /usr/src/debug/boost-1.41.0.cmake0/libs/thread/src/pthread/thread.cpp:120 `
`#11 0x0000003ffb207851 in start_thread (arg=0x7fffae1fc700) at pthread_create.c:301 `
`#12 0x0000003ffaae890d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:115 `
(gdb) up 3
`#3 0x0000003ffaa760e6 in malloc_printerr (action=3, str=0x3ffab582e0 "double free or corruption (!prev)", ptr=<value optimized out>) at malloc.c:6311 6311 __libc_message (action & 2, `
(gdb) up 1
`#4 0x0000003ffaa78c13 in _int_free (av=0x7fffbc000020, p=0x7fffbc001d70, have_lock=0) at malloc.c:4811 4811 malloc_printerr (check_action, errstr, chunk2mem(p)); `
(gdb) up 1
`#5 0x0000003006a9d565 in _M_dispose (this=0x78fc50, __str=<value optimized out>)
at /usr/src/debug/gcc-4.4.7-20120601/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:236 236 _M_destroy(__a); `
(gdb) up 1
`#6 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign (this=0x78fc50, __str=<value optimized out>)
at /usr/src/debug/gcc-4.4.7-20120601/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:251 251 _M_rep()->_M_dispose(__a); `
(gdb) up 1
`#7 0x0000000000433dbc in readLineChatSec (line=0x7fffae1fb7b0, sock=244) at /root/Projects/EdsXInquiry/main.cpp:510 510 buffers = string(pos + 1, buffers.end()); `
(gdb) info locals
buf = "SACPS_06,SAC_N,,,SCAN_DATA,159 23.37.38.935.2013.05.23 \001\000\000\000\000\000\000\000\020\246\037\256\377\177\000\000\327gB\000\000\000\000\000\003\000\000\000\377\177", '\000' <repeats 18 times>, "P", '\000' <repeats 31 times>, "\001\000\000\000\000\000\000\000`\246\037\256\377\177\000\000\327gB\000\000\000\000\000\220\246\037\256\377\177\000\000\307]B\000\000\000\000\000\350B\000\250\377\177\000\000\240\247\037\256\377\177\000\000\200\247\037\256\377\177\000\000\240\247\037\256\377\177\000\000\260\246\037\256\377\177\000\000C\236B\000\000\000\000\000\200\247\037\256\377\177\000\000\240\247\037\256\377\177\000\000\340\246\037\256\377\177\000\000\017\237B\000\000\000\000\000\260\v\000\250\377\177\000\000\200\247\037\256\377\177\000\000\240\247\037\256\377\177\000\000\334\v\000\250\377\177\000\000\340\247\037\256\377\177"...
buffers = "" pos = 32 ' ' n = 0
(gdb) up 1
`#8 0x0000000000486187 in secondaryChatListenerWorker () at /root/Projects/EdsXInquiry/main.cpp:4870 4870 readBytes = readLineChatSec(&msg, sieChatSec.SockFD); `
(gdb) info locals
msgSeqUsed = false ret = 1 timeout = {tv_sec = 10, tv_usec = 889581} readFd = {fds_bits = {0, 0, 0, 4503599627370496, 0 <repeats 12 times>}} readBytes = 0 msg =
"SACPS_08,SAC_N,,,SCAN_DATA,159 23.37.38.828.2013.05.23 NSCAN-2838INNI1Z8W31850495877050", ' ' <repeats 24 times>, "348262114", ' ' <repeats 36 times> (gdb) down 1
`#7 0x0000000000433dbc in readLineChatSec (line=0x7fffae1fb7b0, sock=244) at /root/Projects/EdsXInquiry/main.cpp:510 510 buffers = string(pos + 1, buffers.end()); `
(gdb) print buffers $1 = ""
(gdb) print pos $2 = 32 ' '
(gdb) print pos+1 Attempt to take address of value not located in memory.
(gdb) print pos-1 Attempt to take address of value not located in memory.
(gdb) print buffers.end()
In Connecting : CHATPRIMARY
In Connecting : CHATSECONDARY
In Connecting : SSGSECONDARY
$3 = 0 '\000'
(gdb) info locals
buf = "SACPS_06,SAC_N,,,SCAN_DATA,159 23.37.38.935.2013.05.23 \001\000\000\000\000\000\000\000\020\246\037\256\377\177\000\000\327gB\000\000\000\000\000\003\000\000\000\377\177", '\000' <repeats 18 times>, "P", '\000' <repeats 31 times>, "\001\000\000\000\000\000\000\000`\246\037\256\377\177\000\000\327gB\000\000\000\000\000\220\246\037\256\377\177\000\000\307]B\000\000\000\000\000\350B\000\250\377\177\000\000\240\247\037\256\377\177\000\000\200\247\037\256\377\177\000\000\240\247\037\256\377\177\000\000\260\246\037\256\377\177\000\000C\236B\000\000\000\000\000\200\247\037\256\377\177\000\000\240\247\037\256\377\177\000\000\340\246\037\256\377\177\000\000\017\237B\000\000\000\000\000\260\v\000\250\377\177\000\000\200\247\037\256\377\177\000\000\240\247\037\256\377\177\000\000\334\v\000\250\377\177\000\000\340\247\037\256\377\177"...
buffers =
"SACPS_08,SAC_N,,,DIV_CONF,94 23.37.37.629.2013.05.23 1Z81W88E0491949523ND NROUT-4802 \r\nSACPS_08,SAC_N,,,DIV_CONF,94
23.37.37.657.2013.05.23 H8610626898 ND NBRGE-2804 \r\nSACPS_08,SAC"...
pos = 32 ' ' n = 0
(gdb) print buffers
$4 =
"SACPS_08,SAC_N,,,DIV_CONF,94 23.37.37.629.2013.05.23 1Z81W88E0491949523ND NROUT-4802 \r\nSACPS_08,SAC_N,,,DIV_CONF,94
23.37.37.657.2013.05.23 H8610626898 ND NBRGE-2804 \r\nSACPS_08,SAC"...
(gdb)