Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Causing a Seg Fault on Purpose (http://www.programmingforums.org/showthread.php?t=15390)

SnakeByte Mar 11th, 2008 1:17 PM

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

:

  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)
$

Narue Mar 11th, 2008 2:06 PM

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.

mbd Mar 11th, 2008 3:21 PM

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.

Ancient Dragon Mar 12th, 2008 1:00 AM

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;

Jimbo Mar 12th, 2008 2:08 AM

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...

L7Sqr Mar 12th, 2008 7:26 AM

Re: Causing a Seg Fault on Purpose
 
:

#include <signal.h>

/* ... */

raise (SIGSEGV);


Jessehk Mar 12th, 2008 8:50 AM

Re: Causing a Seg Fault on Purpose
 
Quote:

Originally Posted by L7Sqr (Post 142391)
:

#include <signal.h>

/* ... */

raise (SIGSEGV);


That's cheating. ;)

SnakeByte Mar 12th, 2008 8:55 AM

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.

Ancient Dragon Mar 12th, 2008 10:06 AM

Re: Causing a Seg Fault on Purpose
 
This sounds like a good idea for a game -- 100 ways to create a seg fault :)

mbd Mar 12th, 2008 12:24 PM

Re: Causing a Seg Fault on Purpose
 
Quote:

Originally Posted by Ancient Dragon (Post 142380)
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.


All times are GMT -5. The time now is 4:09 AM.

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