Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Coder's Corner Lounge (http://www.programmingforums.org/forum11.html)
-   -   500 Ways to Cause An Error (http://www.programmingforums.org/showthread.php?t=15396)

titaniumdecoy Mar 12th, 2008 5:10 PM

500 Ways to Cause An Error
 
If you have been following this forum for a long time you will remember the "famous" 500 Ways to Program the Numbers 1 Through 10! thread started by Sane.

I found it quite fun and a good way to encourage new members to participate because it didn't require a great deal of programming knowledge to contribute.

So I propose that as a follow-up we all try to come up with as many creative or practical ways to cause an error in a program. It can be a compile-time or run-time error (or something else entirely), so long as it has the potential to wreak havoc. :twisted:

Please list the language and a description of the error. (It is not necessary to list the reason it occurs if you don't know or can't be bothered.)

I'll start:

1) C
Description: This program causes a segmentation fault by attempting to overwrite data outside its allocated page.

:

#include <stdio.h>

int main() {

    int array[] = {1,2,3,4,5,6,7,8,9,10};
       
    int i;
    for (i = 0; i < 5000; i++) {
        array[i] = i;
        printf("%d\n", i);
    }

    return 0;
}


Wizard1988 Mar 12th, 2008 5:38 PM

Re: 500 Ways to Cause An Error
 
How about a boot sector overwrite :)

Jessehk Mar 12th, 2008 5:39 PM

Re: 500 Ways to Cause An Error
 
2)

:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int *foo( int bar ) {
  5.     int *r = malloc( sizeof *r );
  6.  
  7.     *r = bar;
  8.     /* Notice the lack of a return */
  9. }
  10.  
  11. int main( void ) {
  12.     int *x = foo( 3 );
  13.  
  14.     printf( "%d\n", *x );
  15.  
  16.     free( x );   
  17.     return 0;
  18. }


You can catch this one if you enable all warnings in GCC. With a default compile though, there are (surprisingly) no compile-time errors.
As far as I can guess, x in main() ends up pointing to a random location in memory and a segfault results. I don't know enough about how GCC compiles code to know exactly what happens.

Benoit Mar 12th, 2008 5:47 PM

Re: 500 Ways to Cause An Error
 
Titaniumdecoy's program may or may not crash depending on stack size, it didn't crash on mine machine:(

language: C
Description: realloc could return NULL and overwrite the original pointer value, causing a seg fault later in the program

:

#include<stdio.h>
#include<stdlib.h>


int main(void)
{
    int *ptr;

    ptr=(int *)malloc(23*sizeof(int));

    ptr=(char *)realloc(ptr,53*sizeof(int));

    for(i=0; i<53; i++
    {
        ptr[i]=i;
        printf("%d\n",ptr[i]);
    }

    free(ptr);

    return 0;
)


glimmy Mar 12th, 2008 7:05 PM

Re: 500 Ways to Cause An Error
 
4)
Languge: Perl

It keeps on forking indefinitely. It's not really an error with perl, but it makes a hell of a fork bomb.
:

sub keep_on_forking {
        my $pid = fork();
        if ($pid == 0) {
                keep_on_forking();
        } else {
                keep_on_forking();
        }
}

keep_on_forking();


Jessehk Mar 12th, 2008 8:03 PM

Re: 500 Ways to Cause An Error
 
5) OCaml

:

(* Demonstrate the strong typing of OCaml. *)

(* Type inference automatically knows that "n" should be a float *)
let square n = n *. n

let main () =
    (* This call will work *)
    ignore (square 3.2);
   
    (* This one won't. int's will not be implicitly
    * converted to float's
    *)
    ignore (square 4)
   
let _ = main ()


compiling a byte-code version (native compiler also available):
:

$ ocamlc foo.ml -o foo
File "foo.ml", line 11, characters 19-20:
This expression has type int but is here used with type float


Jessehk Mar 12th, 2008 8:17 PM

Re: 500 Ways to Cause An Error
 
This will probably be my last one. I see it (and do it myself) so often that I thought it would be best. ;)

6) C
:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void allocate( int *dest ) {
  6.     if ( (dest = malloc( sizeof *dest )) == NULL ) {
  7.         perror( "test" );
  8.         exit( EXIT_FAILURE );
  9.     }
  10. }
  11.  
  12. int main( void ) {
  13.     int *x = NULL;
  14.  
  15.     allocate( x );
  16.     *x = 3;
  17.  
  18.     free( x );
  19.     return 0;
  20. }


What you'd expect to happen is that the parameter, x is set to point to the newly allocated memory. What actually happens is that a copy of the pointer is made, and the copy is what's assigned to. The x in main() remains set to NULL, and the code segfaults.

Jimbo Mar 12th, 2008 9:13 PM

Re: 500 Ways to Cause An Error
 
:

  1. class foo
  2. {
  3. } // end of file - this can get you 100+ errors if #included properly... >:)

</thread>? :icon_twisted:

Ancient Dragon Mar 12th, 2008 9:31 PM

Re: 500 Ways to Cause An Error
 
Here is one I saw just today
c++
:

char* p;
cin.getline(p, 1000);

Unallocated pointer p.

Benoit Mar 12th, 2008 9:41 PM

Re: 500 Ways to Cause An Error
 
9)

:

char string[32];

gets(string);



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

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