g++ segmentation fault

Florian Lohoff flo at rfc822.org
Wed Apr 26 14:41:33 CEST 2000


On Wed, Apr 26, 2000 at 12:32:02PM +0200, Thomas Balls-Thies wrote:
> void main(void)
> {  char *a = "text";
>    a[2] = 's';
> }
> 
> funktionieren? Der Kursleiter behauptet zumindest, mit einem
> Borland-Compiler liefe das.

Ich bin mal ein wenig ueberfragt - Interessant ist das dieses geht:

void main(void ) {
        char    *a;
        char    b[5] = "text";
        a=&b;
        a[2] = 's';
}

Guckt man sich dieses als disass an

0x80484c2 <main+50>:    movl   0xfffffffc(%ebp),%eax
0x80484c5 <main+53>:    addl   $0x2,%eax
0x80484c8 <main+56>:    movb   $0x73,(%eax)

und das original

0x804846d <main+13>:    movl   0xfffffffc(%ebp),%eax
0x8048470 <main+16>:    addl   $0x2,%eax
0x8048473 <main+19>:    movb   $0x73,(%eax)

Gibts keinen unterschied.

Aaaaber ... Vermutlich wird das text in eine "read only" text
segment gelegt.


(gdb) r
Starting program: /tmp/j 

Program received signal SIGSEGV, Segmentation fault.
0x8048473 in main () at j.c:6
6               a[2] = 's';



Was ein objdump bestaetigt:


(flo at paradigm)/tmp# objdump --headers j
[...]
  9 .text         000000e8  080483d0  080483d0  000003d0  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
[...]

Also - Du  versucht einen "string" zu modifizieren der als
"read-only" im memory steht.

Wenn man mein beispiel genauer debugged - Dann:

(flo at paradigm)/tmp# gdb j
GNU gdb 4.17.m68k.objc.threads.hwwp.fpu.gnat
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i486-pc-linux-gnu"...
(gdb) break j.c:7 
Breakpoint 1 at 0x8048477: file j.c, line 7.
(gdb) r
Starting program: /tmp/j 

Breakpoint 1, main () at j.c:7
7               a=&b;
(gdb) print a
$1 = 0x0
(gdb) step
8               a[2] = 's';
(gdb) print a
$2 = 0xbffffb6c "text"
(gdb) info all-registers
[...]
     esp: 0xbffffb6c -1073742996
[...]
(gdb) 

D.h. in meinem beispiel legt der das "text" auf
dem stack ab - Der natuerlich per definition
read-write ist.

Flo
-- 
Florian Lohoff		flo at rfc822.org		      	+49-subject-2-change
"Technology is a constant battle between manufacturers producing bigger and
more idiot-proof systems and nature producing bigger and better idiots."


-
Hinweise zur Benutzung dieser (und anderer Mailing-Listen) bitte beachten:
--> http://lug-owl.de/mailinglist_hints.html <--



More information about the Linux mailing list