Home General Computer Multimedia Business Lounge
|
c quiz - find the bug of this c code
|
|
03-21-2012, 10:56 PM
(This post was last modified: 03-22-2012 04:35 PM by ev1lut10n.)
|
|||
|
|||
|
c quiz - find the bug of this c code
given sample gethostbyname c source file which contains a bug, try to find the bug :
buggy_code.c: ==== #include <stdio.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <string.h> int main(int argc,char *argv[]) { struct in_addr ipx; struct hostent *he; char buf[]=""; int argl=strlen(argv[1]); strncpy(buf,argv[1],argl); he=gethostbyname(buf); printf("\nhost :%s\n",he->h_name); bcopy(*he->h_addr_list++, (char *) &ipx, sizeof(ipx)); printf("address: %s\n", inet_ntoa(ipx)); return 0; } =========== |
|||
|
03-21-2012, 11:40 PM
|
|||
|
|||
|
RE: c quiz - find the bug of this c code
ketemu om
![]() Code: alessandra@hatimu:~$ gcc -g -fno-stack-protector -z execstack -o odon odon.cCode: Program received signal SIGSEGV, Segmentation fault. |
|||
Reputed by : ditatompel(+1) |
|
03-22-2012, 12:39 AM
|
|||
|
|||
RE: c quiz - find the bug of this c code
(03-21-2012 11:40 PM)alessandra Wrote: ketemu om Maksudnya gimana nih tante? ![]() Jelasin dong, ane jg pingin ..
|
|||
|
03-22-2012, 12:47 AM
|
|||
|
|||
|
RE: c quiz - find the bug of this c code
@ditatompel
itu bugnya dit jadi di Code: printf("address: %s\n", inet_ntoa(ipx));
|
|||
|
03-22-2012, 01:14 AM
|
|||
|
|||
| RE: c quiz - find the bug of this c code | |||
|
03-22-2012, 05:25 AM
|
|||
|
|||
|
RE: c quiz - find the bug of this c code
@alessandra:
mantap mbak sandra hampir ketemu penyebab bugnya dikit lagi btw boleh kenalan ga |
|||
|
03-22-2012, 09:44 AM
|
|||
|
|||
RE: c quiz - find the bug of this c code
(03-22-2012 05:25 AM)ev1lut10n Wrote: @alessandra: boleh bang trus bang kok dikit lagi.... ![]() ajarin dong ![]() (03-22-2012 01:14 AM)ditatompel Wrote:(03-22-2012 12:47 AM)alessandra Wrote: @ditatompel begitulah dit anyway gw juga belajar ama bang mywisdom
|
|||
|
03-22-2012, 11:51 AM
(This post was last modified: 03-22-2012 12:07 PM by ack_attack.)
|
|||
|
|||
|
RE: c quiz - find the bug of this c code
kakak.. aku coba jawab ya kak... dan kalau ada yang salah maafin aku ya kak.... ^_^
bug I : panjang array dari variabel "char buf[]" tidak diketahui, dan pada pendeklarasian variabel, variabel "char buf[]" telah diisi dengan nilai "" (string kosong). jadi kalau menurut aku tidak mungkin kalau kita mengkopi string dari "argv[1]" sepanjang "strlen(argv[1])" ke dalam variabel tersebut. solusi : - panjang array dari "char buf[]" harus diinisialisasi terlebih dahulu. misal "char buf[256]" - nilai string awal dari "char buf[256]" tidak boleh dideklarasikan langsung, melainkan dari ekspresi berikut --> "strncpy(buf, argv[1], strlen(argv[1]));" bug II : variabel "char host[]" pada awal pendeklarasian sudah berisikan "" (string kosong). jadi pada ekspresi berikut --> "he = gethostbyname(host);" akan menghasilkan segmentation fault apabila kita akan mengkopi nilai dari "*he->h_addr_list++" ke "(char *)&ipx" sebanyak "sizeof(ipx)". solusi : "char *host" atau "char host[]" tidak boleh berisi string kosong. # sekali lagi maafkan ya kak kalau ada yang salah.... ^_^ |
|||
|
03-22-2012, 11:55 AM
|
|||
|
|||
RE: c quiz - find the bug of this c code
(03-22-2012 11:51 AM)ack_attack Wrote: kakak.. aku coba jawab ya kak... dan kalau ada yang salah maafin aku ya kak.... ^_^ oh gitu ya makasih ack_attack lebih rinciaku cuma ngertinya karena buff kosong trus segmentation fault dan bisa menyebabkan buffer ![]() salam kenal bang
|
|||
|
03-22-2012, 11:56 AM
|
|||
|
|||
|
RE: c quiz - find the bug of this c code
salam kenal juga kakak alessandra.... ^_^
|
|||
|
« Next Oldest | Next Newest »
|
| Topic Tools | ||||||
| ||||||
| Possibly Related Threads... | |||||
| Thread: | Author | Replies: | Views: | Last Post | |
| [Ask] Need Help, inefficient c source code | cr0security | 0 | 84 |
10-26-2012 05:00 AM Last Post: cr0security |
|
| Shell Code Generator | wenkhairu | 15 | 4,036 |
08-11-2012 12:20 AM Last Post: pistol-air |
|
| "USE CASTING IN YOUR C CODE BITCH" | garfield | 2 | 239 |
05-11-2012 01:41 PM Last Post: wendyaja |
|
| (ASK) Cara menggunakan Code::Blocks IDE on UBUNTU | Black.exe | 3 | 704 |
03-11-2012 10:48 AM Last Post: DC_Zulfikar |
|
| [Tutor] Obfuscate source code C (bikin source code susah dibaca) | ketek | 14 | 923 |
02-04-2012 11:34 AM Last Post: ack_attack |
|
| Users Browsing |
| 1 Guest(s) |





![[Image: alessa2.png]](http://www.pa7.tv.br/alessa2.png)


..
trus bang kok dikit lagi.... 

anyway gw juga belajar ama bang mywisdom






