jois.de.vivre@gmail.com wrote:[color=blue]
> Hi,
>
> I have the following piece of code that is designed to help me add
> debug traces to my program (I wanted to use purely C++ code, but the
> only way I know how to do something like this is with macros, so please
> don't yell at me):[/color]
Perhaps you didn't notice this is comp.lang.c, not comp.lang.c++.
Anyways, look at your vspintf: vsprintf(display2, display, va);
display2 is char[256]. display is 548 (as you said in the main, i did
not count them). See anything wrong with that? You're overrunning the
buffer!
try vsnprintf.
[color=blue]
>
>
> #include <iostream>
> #include <cstdarg>
> #include <iomanip>
>
> #define MainDisplay(s, ...) Show( __FILE__, \
> __PRETTY_FUNCTION__,\
> __LINE__, \
> s, \
> ## __VA_ARGS__);
> using namespace std;
>
> void Show(const char* file, const char* fcn, int line, const char*
> display, ...)
> {
> //build display string
> va_list va;
> va_start(va, display);
> char display2[256];
> char linestr[10];
> vsprintf(display2, display, va);
> va_end(va);
> sprintf(linestr, "%d", line);
>
> string s = (file + string(" [") + linestr + string("]:"));
>
> //output to console
> cout << s;
> int ssize = s.size();
> if (ssize <= 45) cout << setw(50-ssize) << "" << display2;
> else cout << endl << setw(50) << "" << display2;
> }
>
>
> Now I want it to display a very long string message (this string has
> 548 characters in it):
>
> int main(int argc, char *argv[])
> {
>
> MainDisplay("0000000101010101010101010101010101010 10101010101010101010101010101010101010101010101010 10101010101111111111111111111111111111111111111100 01000000101100010001000001100111101000110001010011 10011010011100110100110110111111111111111111111111 11111111111111011010100110011100101100000000101010 10101010101010101010101010101010101010101010101010 10101010101010101010101010101010101111111111111111 11111111111111111111110001000000101100010001000001 10011110100011000101001110011010011100110100110110 11111111111111111111111111111111111111011010100110 01110010110");
>
> return EXIT_SUCCESS;
> }
>
>
>
> When I run this code I get a segmentation fault, and GDB spews the
> following:
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
> (gdb) backtrace
> #0 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
> #1 0x0000000000401775 in std::char_traits<char>::length
> (__s=0x3130313031303130 <Address 0x3130313031303130 out of bounds>) at
> char_traits.h:143
> #2 0x00000000004015ea in std::operator+<char, std::char_traits<char>,
> std::allocator<char> > (__lhs=0x3130313031303130 <Address
> 0x3130313031303130 out of bounds>, __rhs=@0x7fffffffe9b0) at
> basic_string.tcc:692
> #3 0x0000000000401232 in Show (file=0x3130313031303130 <Address
> 0x3130313031303130 out of bounds>, fcn=0x3130313031303130 <Address
> 0x3130313031303130 out of bounds>, line=825241904,
> display=0x3130313031303130 <Address 0x3130313031303130 out of bounds>)
> at /home/prashant/Development/test2/src/test2.cpp:20
> #4 0x3131303131313131 in ?? ()
> #5 0x3131303031303130 in ?? ()
> #6 0x3130303131313030 in ?? ()
> #7 0x00002a0030313130 in ?? ()
> #8 0x0000000000000000 in ?? ()
> #9 0x0000000000503028 in ?? ()
> #10 0x0000000000400e1b in _init ()
> #11 0x00007fffffffed78 in ?? ()
> #12 0x0000000100401801 in ?? ()
> #13 0
> Previous frame inner to this frame (corrupt stack?)
> x00002aaaaabc0c60 in ?? () from /lib64/ld-linux-x86-64.so.2
> #14 0x00002aaaaabc0c60 in ?? () from /lib64/ld-linux-x86-64.so.2
> #15 0x00000000004017e0 in __libc_csu_fini ()
> #16 0x00002aaaab00e1d8 in ?? () from /lib64/tls/libc.so.6
> #17 0x00007fffffffed78 in ?? ()
> #18 0x00000001ffffed88 in ?? ()
> #19 0x0000000000401490 in Show () at
> /home/prashant/Development/test2/src/test2.cpp:27
> (gdb) frame 0
> #0 0x00002aaaab06f6a0 in strlen () from /lib64/tls/libc.so.6
>
>
> If I shorten the string, I find that I get no segmentation fault for
> anything less than 313 characters. My assumption is that this is a
> 'new' or malloc memory limit. ulimit -a returns the following:
>
> core file size (blocks, -c) 0
> data seg size (kbytes, -d) unlimited
> file size (blocks, -f) unlimited
> max locked memory (kbytes, -l) 32
> max memory size (kbytes, -m) unlimited
> open files (-n) 1024
> pipe size (512 bytes, -p) 8
> stack size (kbytes, -s) unlimited
> cpu time (seconds, -t) unlimited
> max user processes (-u) 8191
> virtual memory (kbytes, -v) unlimited
>
>
> I'm running on Suse Linux 9.3 (but I've been able to reproduce this
> problem on 9.0) and I'm using gcc 3.4.1.
>
> Any Ideas?
>
> Prashant[/color]