public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/15048] New: Dereferincing char pointer to char array yields segfault.
@ 2004-04-21 16:59 sh4d0wstr1f3 at yahoo dot com
2004-04-21 17:18 ` [Bug c/15048] " rearnsha at gcc dot gnu dot org
0 siblings, 1 reply; 2+ messages in thread
From: sh4d0wstr1f3 at yahoo dot com @ 2004-04-21 16:59 UTC (permalink / raw)
To: gcc-bugs
--- clip ---
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)
--- clip ---
Maybe related to 6857? I've got two source files, and a header. Header (H) has a
declaration for 'extern char *string'. Source (A) defines 'string' as 'char
string[20]' and has a routine which takes a char * and strncpy's it into
'string'. Source (B) has the main routine and includes the header. The main
routine invokes the function in sA to set 'string' to "foo"; then proceeds to
printf 'string'. This causes a segmentation fault.
I notice the following things:
1. The address of 'string' gets annihilated on the call to sA.
2. The assembler code generated for calling printf in sB has a 'pushl address',
not a 'push $address' for the address of 'string'. This causes it to derefence
the first characters of 'string'.
3. WORKAROUND: if I change the extern of 'string' to 'extern char string[]',
everything works as expected.
4. If I have sA include header H, I get "error: conflicting types for `string'".
(4) makes me think I've answered my own question, but if I did, I as yet have no
idea what the answer means to me. :) I mean, if I can say 'char *c =
&string[3]', shouldn't what I'm describing here work?
Thanks for any help you can provide!
--clip--
The invocation (from 'make'):
cc -I. -ggdb -c -o wg.o wg.c
cc -I. -ggdb -c -o swg.o swg.c
The preprocessed sources:
Source A:
# 1 "swg.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "swg.c"
char weak_char[20];
void set_wg_char(char *give)
{
printf("copy work???");
strncpy(weak_char, give, sizeof(weak_char));
printf("yes---%s--%p\n", weak_char, weak_char);
}
---clip---
Source B:
# 1 "wg.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "wg.c"
# 1 "wg.h" 1
extern char *weak_char;
void set_weak_char(char *);
# 2 "wg.c" 2
int main()
{
set_wg_char("it is a test");
printf("pointer:-%p-\n", weak_char);
printf("value:---%s---\n", weak_char);
}
--clip--
For case (4) where I include header H in sA:
swg.c:3: error: conflicting types for `weak_char'
wg.h:4: error: previous declaration of `weak_char'
--clip--
The interesting difference in assembly code generated when I change 'char *' to
'char []':
printf("value:---%s---\n", weak_char);
- 80483b1: 83 ec 08 sub $0x8,%esp
- 80483b4: 68 38 96 04 08 push $0x8049638
- 80483b9: 68 ff 84 04 08 push $0x80484ff
- 80483be: e8 e9 fe ff ff call 80482ac <_init+0x38>
- 80483c3: 83 c4 10 add $0x10,%esp
+ 80483b2: 83 ec 08 sub $0x8,%esp
+ 80483b5: ff 35 3c 96 04 08 pushl 0x804963c
+ 80483bb: 68 03 85 04 08 push $0x8048503
+ 80483c0: e8 e7 fe ff ff call 80482ac <_init+0x38>
+ 80483c5: 83 c4 10 add $0x10,%esp
--
Summary: Dereferincing char pointer to char array yields
segfault.
Product: gcc
Version: 3.3.2
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: sh4d0wstr1f3 at yahoo dot com
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15048
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug c/15048] Dereferincing char pointer to char array yields segfault.
2004-04-21 16:59 [Bug c/15048] New: Dereferincing char pointer to char array yields segfault sh4d0wstr1f3 at yahoo dot com
@ 2004-04-21 17:18 ` rearnsha at gcc dot gnu dot org
0 siblings, 0 replies; 2+ messages in thread
From: rearnsha at gcc dot gnu dot org @ 2004-04-21 17:18 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From rearnsha at gcc dot gnu dot org 2004-04-21 16:48 -------
Not a bug. Your comment shows the problem. extern char* is not the same as
extern char[]. One is a reference to a char array, the other a pointer to char.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15048
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-04-21 16:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-21 16:59 [Bug c/15048] New: Dereferincing char pointer to char array yields segfault sh4d0wstr1f3 at yahoo dot com
2004-04-21 17:18 ` [Bug c/15048] " rearnsha at gcc dot gnu dot org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).