Article View: cn.comp.security
Article #1extern与指针的指针变量
From: NewBlue
Date: Tue, 14 Oct 2003 01:37
Date: Tue, 14 Oct 2003 01:37
127 lines
7248 bytes
7248 bytes
[tips] extern��ָ���ָ�����
alert7 �����ڣ�2003-09-08 13:02
extern��ָ���ָ�����
by alert7
gcc��һ��С����,�������Ѿ�֪�����ȽϷ�dz�����Dz�֪���Ŀ��ܻ����ƺü���Сʱ��
һֱ���������Ƕ���Ϊchar *argv[]��char **argv�ǵȼ۵ġ���������
Ҳһֱû�г��?���Խ��ҵ���Ϊchar *argv[]��char **argv�ǵȼ۵ģ�ԭ��
��һ��������ͨ�õġ�
���ǣ������һ��BUG������һ�죬���ƾ���֮�������Ƕ���ָ���ָ�����
�ǵĻ�������ԭ�ͣ�Ӧ�ö����ָ�����顣���������ְѾ���д�ɱ��ģ���ʹ������
��һЩ��·��
���µ��������ҳ�������ģ�table�������ָ�����飬��ʵ����ʵ��o.c�����ǵ���
���ṩ�������û��Դ���룬����ܲݲݵİ�table����Ϊextern void ** ;
[root@redhat73 root]# cat o.c
#include <stdio.h>
void * table[]={"0","1","2","3",NULL};
int test(void )
{
return 0;
}
[root@redhat73 root]# cat t.c
#include <stdio.h>
int main(int argc, char *argv[])
{
extern void **table;
int t;
printf("table %x\n",&table);
printf("&table[1] %x\n",&table[1]);
printf("table[1] %s\n",table[1]);
return 0;
}
[root@redhat73 root]# gcc -c t.c
[root@redhat73 root]# gcc -o t t.o o.o
[root@redhat73 root]# ./t
table 8049518
&table[1] 8048507 <-----������Ȼ����������Ҫ��
table[1] (null) <-----������dz�����
���������»�����
08048400 <main>:
8048400: 55 push %ebp
8048401: 89 e5 mov %esp,%ebp
8048403: 83 ec 08 sub $0x8,%esp
8048406: 83 ec 08 sub $0x8,%esp
8048409: 68 18 95 04 08 push $0x8049518 <------table��ַ
804840e: 68 d8 84 04 08 push $0x80484d8
8048413: e8 d8 fe ff ff call 80482f0 <_init+0x58>
8048418: 83 c4 10 add $0x10,%esp
804841b: 83 ec 08 sub $0x8,%esp
804841e: a1 18 95 04 08 mov 0x8049518,%eax <-----��0x8049518��ַ�е�ֵ�ŵ�eax��
8048423: 83 c0 04 add $0x4,%eax <-----�ٰ����ֵ��4������Ȼ�����ǵı����г���
8048426: 50 push %eax
8048427: 68 e2 84 04 08 push $0x80484e2
804842c: e8 bf fe ff ff call 80482f0 <_init+0x58>
8048431: 83 c4 10 add $0x10,%esp
8048434: 83 ec 08 sub $0x8,%esp
8048437: a1 18 95 04 08 mov 0x8049518,%eax
804843c: 83 c0 04 add $0x4,%eax
804843f: ff 30 pushl (%eax)
8048441: 68 f0 84 04 08 push $0x80484f0
8048446: e8 a5 fe ff ff call 80482f0 <_init+0x58>
804844b: 83 c4 10 add $0x10,%esp
804844e: b8 00 00 00 00 mov $0x0,%eax
8048453: c9 leave
8048454: c3 ret
�������°���ԭ�Ͷ����extern void *table[];
#include <stdio.h>
int main(int argc, char *argv[])
{
extern void *table[];
int t;
printf("table %x\n",&table);
printf("&table[1] %x\n",&table[1]);
printf("table[1] %s\n",table[1]);
return 0;
}
[root@redhat73 root]# gcc -c t.c
[root@redhat73 root]# gcc -o t t.o o.o
[root@redhat73 root]# ./t
table 8049508
&table[1] 804950c
table[1] 1
08048400 <main>:
8048400: 55 push %ebp
8048401: 89 e5 mov %esp,%ebp
8048403: 83 ec 08 sub $0x8,%esp
8048406: 83 ec 08 sub $0x8,%esp
8048409: 68 08 95 04 08 push $0x8049508
804840e: 68 c8 84 04 08 push $0x80484c8
8048413: e8 d8 fe ff ff call 80482f0 <_init+0x58>
8048418: 83 c4 10 add $0x10,%esp
804841b: 83 ec 08 sub $0x8,%esp
804841e: 68 0c 95 04 08 push $0x804950c <--------����Ͷ���
8048423: 68 d2 84 04 08 push $0x80484d2
8048428: e8 c3 fe ff ff call 80482f0 <_init+0x58>
804842d: 83 c4 10 add $0x10,%esp
8048430: 83 ec 08 sub $0x8,%esp
8048433: ff 35 0c 95 04 08 pushl 0x804950c
8048439: 68 e0 84 04 08 push $0x80484e0
804843e: e8 ad fe ff ff call 80482f0 <_init+0x58>
8048443: 83 c4 10 add $0x10,%esp
8048446: b8 00 00 00 00 mov $0x0,%eax
804844b: c9 leave
804844c: c3 ret
���ԣ����ǻ���Ҫ�ϸ��պ����ԭ�������塣
����һЩʵ�飬ָ���ָ���������extern����������Ҫע���ˣ�
��ʱ��ָ���ָ�������ָ������Ͳ���ͨ���ˡ�
--
NewBlue
�Դ����뿪���Ժ���һֱ�ȴ��������
Technical Details
Message-ID:
<uad84zjij.fsf@yaako.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news-hog.berkeley.edu!ucberkeley!192.50.8.47.MISMATCH!newsfeed.media.kyoto-u.ac.jp!newsgate.cuhk.edu.hk!news.cn99.com!news.yaako.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB X-Google-Thread: 1009e6,a15f179b39aafe37,start X-Google-Attributes: gid1009e6,public X-Google-Thread: 11047e,a15f179b39aafe37,start X-Google-Attributes: gid11047e,public X-Google-Thread: 1098a9,a15f179b39aafe37,start X-Google-Attributes: gid1098a9,public X-Google-Thread: f5fb9,a15f179b39aafe37,start X-Google-Attributes: gidf5fb9,public X-Google-Thread: 113009,a15f179b39aafe37,start X-Google-Attributes: gid113009,public X-Google-Thread: 10b277,a15f179b39aafe37,start X-Google-Attributes: gid10b277,public X-Google-ArrivalTime: 2003-10-13 20:21:57 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news-hog.berkeley.edu!ucberkeley!192.50.8.47.MISMATCH!newsfeed.media.kyoto-u.ac.jp!newsgate.cuhk.edu.hk!news.cn99.com!news.yaako.com!not-for-mail From: NewBlue <NewBlue@yaako.com> Newsgroups: cn.comp.security,cn.comp.programm,cn.comp.lang.c,cn.comp.os.linux,cn.comp.os.freebsd,cn.comp.hacker Subject: extern与指针的指针变量 Date: Tue, 14 Oct 2003 09:37:56 +0800 Organization: 功夫茶 Lines: 127 Message-ID: <uad84zjij.fsf@yaako.com> NNTP-Posting-Host: 61.146.252.3 Mime-Version: 1.0 Content-Type: text/plain; charset=gb2312 Content-Transfer-Encoding: 8bit X-Trace: news.yaako.com 1066100559 11604 61.146.252.3 (14 Oct 2003 03:02:39 GMT) X-Complaints-To: usenet@news.yaako.com NNTP-Posting-Date: Tue, 14 Oct 2003 03:02:39 +0000 (UTC) User-Agent: Emacs+gnus Cancel-Lock: sha1:Ub+nyXawy3RuoLcsmX3A38Do3gE=