Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 11th, 2008, 12:17 PM   #1
SnakeByte
Newbie
 
SnakeByte's Avatar
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 SnakeByte is on a distinguished road
Question Causing a Seg Fault on Purpose

Hi, I'm trying to learn C coding and I happen to be at a chapter in a book I'm reading which deals with segmentation faults. What I'm trying to do is get a program, that accepts command-line arguments, to crash (with a seg fault) when it isn't given any arguments. The code from the book is as follows:

convert2.c

C Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. void usage(char *program_name) {
  4. printf("Usage: %s <message> <# of times to repeat\n", program_name);
  5. exit(1);
  6. }
  7.  
  8. int main(int argc, char *argv[]) {
  9. int i, count;
  10.  
  11. //if(argc < 3) // If fewer than 3 arguements are used,
  12. // usage(argv[0]); // Display usage messages and exit.
  13.  
  14. count = atoi(argv[2]); //Convert the 2nd arg into an integer.
  15. printf("Repeating %d times...\n", count);
  16.  
  17. for(i=0; i < count; i++)
  18. printf("%3d - %s\n", i, argv[1]); // Print the 1st arg.
  19. }

When I compile this code in gcc (v4.1.3) on Ubuntu 7.10 I get this message:
$ gcc -g -o convert2 convert2.c
convert2.c: In function ‘usage’:
convert2.c:5: warning: incompatible implicit declaration of built-in function ‘exit’

It still compiles, but when I run it, it does this:
$ ./convert2
Repeating 0 times...

Since I'm trying to break the program, this isn't the result I want. Does anyone know how to "break" this program so that it will cause a seg fault?

BTW, the desired result would be this:
$ ./convert2
Segmentation fault (core dumped)
$
__________________
Boy, it sure would be nice if we had some grenades, don't you think? - Jayne Cobb

Last edited by SnakeByte; Mar 11th, 2008 at 12:33 PM.
SnakeByte is offline   Reply With Quote
Old Mar 11th, 2008, 1:06 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
Re: Causing a Seg Fault on Purpose

>What I'm trying to do is get a program, that accepts command-line
>arguments, to crash (with a seg fault) when it isn't given any arguments.
What you're seeing is undefined behavior. It might crash, it might not. In this case, accessing argv[2] doesn't touch memory outside of your address space, and when atoi fails, it returns 0. Both of these are undefined behavior, but you lucked out in that it doesn't cause the error you were expecting.

The only way to guarantee a seg fault is to raise it yourself.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Mar 11th, 2008, 2:21 PM   #3
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Causing a Seg Fault on Purpose

#include <stdlib.h>

will get rid of the warning about exit being implicitly defined

to get a seg fault you should just try writing to a specific memory address. something along the lines of:

int *i;
i = 0;
*i = 0;

i think that would generate a seg fault.
mbd is offline   Reply With Quote
Old Mar 12th, 2008, 12:00 AM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Causing a Seg Fault on Purpose

There are lots of things that will generate seg faults -- division by 0 if another one of them
int x = 123 / 0;
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 12th, 2008, 1:08 AM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Re: Causing a Seg Fault on Purpose

Out of curiosity, any particular reason you want to cause a seg fault? It is an odd request...
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Mar 12th, 2008, 6:26 AM   #6
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 124
Rep Power: 0 L7Sqr is an unknown quantity at this point
Re: Causing a Seg Fault on Purpose

#include <signal.h>

/* ... */

raise (SIGSEGV);
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Mar 12th, 2008, 7:50 AM   #7
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
Re: Causing a Seg Fault on Purpose

Quote:
Originally Posted by L7Sqr View Post
#include <signal.h>

/* ... */

raise (SIGSEGV);
That's cheating.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 12th, 2008, 7:55 AM   #8
SnakeByte
Newbie
 
SnakeByte's Avatar
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0 SnakeByte is on a distinguished road
Re: Causing a Seg Fault on Purpose

Quote:
Out of curiosity, any particular reason you want to cause a seg fault? It is an odd request...
Jimbo: I'm trying to cause a seg fault because the particular book I'm using to learn C attempts to explain the reason seg faults occur through debugging (gdb) and looking at memory addresses. In order to understand why/how they're caused, the book gives you a program that should cause a segmentation fault.
__________________
Boy, it sure would be nice if we had some grenades, don't you think? - Jayne Cobb
SnakeByte is offline   Reply With Quote
Old Mar 12th, 2008, 9:06 AM   #9
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Causing a Seg Fault on Purpose

This sounds like a good idea for a game -- 100 ways to create a seg fault
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 12th, 2008, 11:24 AM   #10
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Causing a Seg Fault on Purpose

Quote:
Originally Posted by Ancient Dragon View Post
There are lots of things that will generate seg faults -- division by 0 if another one of them
int x = 123 / 0;
a segmentation fault is only caused by accessing memory in a way which is not allowed. division by zero is a different type of exception.
mbd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list seg fault sackarias C++ 2 Apr 30th, 2007 3:25 PM
Segmentation Fault jazz C 5 Aug 21st, 2006 3:16 AM
Segmentation Fault Cipher C++ 2 Feb 21st, 2006 2:10 PM
Network Fault Injector Mad_guy Project Ideas 0 Oct 6th, 2005 4:53 PM
fprintf segmentation fault Michael Edgar C++ 0 Mar 21st, 2005 12:21 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC