programs/C0-long.c
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 29 Oct 2013 12:09:44 +0000
changeset 124 382aad582d8b
parent 111 677179c76e35
permissions -rw-r--r--
added slides
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#include <string.h>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
#include <stdio.h>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
#include <stdlib.h>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
/*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  I used as environment the virtual machine provided here
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
    http://www.cis.upenn.edu/~cis551/box.tar
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  This is Debian/Etch with Linux 2.6.18 with gcc 4.1.2 from 2008.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  Some installation notes for this virtual machine under VMWare
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  are here
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    http://www.cis.upenn.edu/~cis551/project1.pdf
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  I run the virtial machine under MacOSX using the program 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  VirtualBox available for free from 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
    https://www.virtualbox.org
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  The C-program I compiled the program with 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
    gcc -ggdb -fno-stack-protector -mpreferred-stack-boundary=2
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
 */
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
void foo (char *bar)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
{
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  float my_float = 10.5;    // in hex: \x41\x28\x00\x00
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  char  buffer[28];        
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  printf("my float value = %f\n", my_float);
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  strcpy(buffer, bar);  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  printf("my float value = %f\n", my_float);
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
int main (int argc, char **argv)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
{
111
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 24
diff changeset
    43
  foo("my string is too long !!!!! ");                  // all is normal
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 24
diff changeset
    44
  foo("my string is too long !!!!! \x10\x10\xc0\x42");  // overwrites my_float
24
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  return 0;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47