diff -Nur --exclude=CVS dietlibc-orig/dietfeatures.h dietlibc-cp0.3/dietfeatures.h
--- dietlibc-orig/dietfeatures.h	2004-01-31 14:48:49.000000000 +0100
+++ dietlibc-cp0.3/dietfeatures.h	2004-07-07 10:56:15.000000000 +0200
@@ -92,6 +92,16 @@
 /* #define WANT_MALLOC_ZERO */
 
 
+/* do you want ContraPolice support? ContraPolice is a mechanism to protect
+ * buggy programs from buffer overflows on the heap being exploited.  To
+ * enforce it after every library call, you still have to enable CP_FORCE_CHECK,
+ * otherwise you have to do manual calls to cp_check()/cp_check_all().
+ */
+
+#define WANT_CONTRAPOLICE
+
+#define CP_FORCE_CHECK
+
 /* stop uncommenting here ;-) */
 #ifndef WANT_FASTER_STRING_ROUTINES
 #define WANT_SMALL_STRING_ROUTINES
diff -Nur --exclude=CVS dietlibc-orig/include/stdlib.h dietlibc-cp0.3/include/stdlib.h
--- dietlibc-orig/include/stdlib.h	2004-04-14 22:38:52.000000000 +0200
+++ dietlibc-cp0.3/include/stdlib.h	2004-07-07 10:56:34.000000000 +0200
@@ -14,6 +14,10 @@
 void free(void *ptr) __THROW;
 void *realloc(void *ptr, size_t size) __THROW __attribute_malloc__;
 
+/* ContraPolice check functions */
+void cp_check(void *);
+void cp_check_all(void);
+
 char *getenv(const char *name) __THROW __pure;
 int putenv(const char *string) __THROW;
 int setenv(const char *name, const char *value, int overwrite) __THROW;
diff -Nur --exclude=CVS dietlibc-orig/lib/alloc.c dietlibc-cp0.3/lib/alloc.c
--- dietlibc-orig/lib/alloc.c	2004-06-12 12:10:38.000000000 +0200
+++ dietlibc-cp0.3/lib/alloc.c	2004-07-07 11:01:34.000000000 +0200
@@ -1,6 +1,8 @@
 /*
  * malloc/free by O.Dreesen
  *
+ * ContraPolice support by Andreas Krennmair
+ *
  * first TRY:
  *   lists w/magics
  * and now the second TRY
@@ -18,6 +20,12 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef WANT_CONTRAPOLICE
+#include <stdio.h> /* XXX for printf ... */
+#include <time.h>
+#include <fcntl.h>
+#endif
+
 #include <sys/shm.h>	/* for PAGE_SIZE */
 
 
@@ -31,14 +39,39 @@
 #define NULL ((void*)0)
 #endif
 
-typedef struct {
+typedef struct __alloc {
   void*  next;
   size_t size;
+#ifdef WANT_CONTRAPOLICE
+  struct __alloc * cp_next;
+  size_t alloc_size;
+  uint32_t decoy;
+#endif
 } __alloc_t;
 
+#ifdef WANT_CONTRAPOLICE
+
+static __alloc_t * cp_first = NULL;
+
+/* the decoy is placed right after the allocated memory 
+ * cp_f(__alloc_t.decoy) == __decoy_t.decoy must be true
+ * to make an allocated memory block pass as not overflowed
+ */
+
+typedef struct {
+  uint32_t decoy;
+} __decoy_t;
+
+#endif
+
+
 #define BLOCK_START(b)	(((void*)(b))-sizeof(__alloc_t))
 #define BLOCK_RET(b)	(((void*)(b))+sizeof(__alloc_t))
 
+#ifdef WANT_CONTRAPOLICE
+#define BLOCK_DECOY(b)        (__decoy_t *)(((char *)BLOCK_RET(b))+(((__alloc_t *)(b))->alloc_size))
+#endif
+
 #define MEM_BLOCK_SIZE	PAGE_SIZE
 #define PAGE_ALIGN(s)	(((s)+MEM_BLOCK_SIZE-1)&(unsigned long)(~(MEM_BLOCK_SIZE-1)))
 
@@ -53,6 +86,106 @@
   return mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, (size_t)0);
 }
 
+#ifdef WANT_CONTRAPOLICE
+
+/* -- ContraPolice -------------------------------------- */
+
+static int cp_srand(int x, int * fd) {
+  int myfd;
+  srand(x);
+  myfd = open ("/dev/urandom", O_RDONLY);
+  if (myfd != -1) {
+    *fd = myfd;
+    return 1;
+  }
+  return 0;
+}
+
+static uint32_t cp_rand(void) {
+  static int initialized;
+  static int urand;
+  static int fd;
+  uint32_t x;
+  if (!initialized) {
+    urand = cp_srand(time(NULL) ^ getpid() ^ getppid(),&fd);
+    initialized = 1;
+  }
+  if (urand) {
+    ssize_t l = read(fd,&x,sizeof(x));
+    if (l!=sizeof(x)) { x = rand(); }
+  } else {
+    x = rand();
+  }
+  return x;
+}
+
+static inline uint32_t cp_f(uint32_t x) {
+  return x; /* should we improve this? */
+}
+
+static inline void cp_add_memblock(__alloc_t * x) {
+  /* simply add memory block to begin of list */
+  if (x) {
+    x->cp_next = cp_first;
+    cp_first = x;
+  }
+}
+
+static void cp_remove_memblock(__alloc_t * x) {
+  if (cp_first && x) {
+    __alloc_t * t;
+    if (cp_first == x) {
+      cp_first = x->next;
+    } else {
+      for (t=cp_first;t->next!=NULL;t=t->next) {
+        if (t->next == x) { t->next = x->next; break; }
+      }
+    }
+  }
+}
+
+static void cp_abort(void * x) {
+  
+  printf("ContraPolice: detected heap buffer overflow while checking address %p.\n",x); /* XXX this means bloat! */
+  abort();
+}
+
+void cp_check(void * x) {
+  if (cp_first && x) {
+    __alloc_t * t;
+    for (t=cp_first;t!=NULL;t=t->next) {
+      size_t alloc_size; 
+      uint32_t decoy;
+      alloc_size = t->alloc_size;
+      decoy = t->decoy;
+
+      /* the range check is done so that a heap overflow check
+       * can be done even when we only have an address to points
+       * to somewhere in the middle of a dynamically allocated buffer
+       */
+      if (((char *)x)>=((char *)BLOCK_RET(t)) && ((char *)x)<(((char *)BLOCK_RET(t))+alloc_size)) {
+        if ( (BLOCK_DECOY(t))->decoy != cp_f(decoy)) {
+          cp_abort(x);
+        }
+      }
+    }
+  }
+}
+
+void cp_check_all(void) {
+  if (cp_first) {
+    __alloc_t * t;
+    for (t=cp_first;t!=NULL;t=t->next) {
+      uint32_t decoy = ((__alloc_t *)t)->decoy;
+      if ((BLOCK_DECOY(t))->decoy != cp_f(decoy)) {
+        cp_abort(BLOCK_RET(t));
+      }
+    }
+  }
+}
+
+#endif
+
 /* -- SMALL MEM ----------------------------------------------------------- */
 
 static __alloc_t* __small_mem[8];
@@ -128,6 +261,12 @@
 static void _alloc_libc_free(void *ptr) {
   register size_t size;
   if (ptr) {
+ #ifdef WANT_CONTRAPOLICE
+ #ifdef CP_FORCECHECK
+    cp_check(BLOCK_START(ptr));
+ #endif
+    cp_remove_memblock(BLOCK_START(ptr));
+ #endif
     size=((__alloc_t*)BLOCK_START(ptr))->size;
     if (size) {
       if (size<=__MAX_SMALL_SIZE)
@@ -148,13 +287,19 @@
 static void* _alloc_libc_malloc(size_t size) {
   __alloc_t* ptr;
   size_t need;
+  size_t orig_size = size;
 #ifdef WANT_MALLOC_ZERO
   if (!size) return BLOCK_RET(zeromem);
 #else
   if (!size) goto err_out;
 #endif
+#ifdef WANT_CONTRAPOLICE
+  size+=sizeof(__alloc_t)+sizeof(__decoy_t);
+  if (size<(sizeof(__alloc_t)+sizeof(__decoy_t))) goto err_out;
+#else
   size+=sizeof(__alloc_t);
   if (size<sizeof(__alloc_t)) goto err_out;
+#endif
   if (size<=__MAX_SMALL_SIZE) {
     need=GET_SIZE(size);
     ptr=__small_malloc(need);
@@ -165,6 +310,11 @@
   }
   if (ptr==MAP_FAILED) goto err_out;
   ptr->size=need;
+#ifdef WANT_CONTRAPOLICE
+  ptr->alloc_size = orig_size;
+  ((__decoy_t *)BLOCK_DECOY(ptr))->decoy = cp_f(ptr->decoy = cp_rand()); /* XXX set random value */
+  cp_add_memblock(ptr);
+#endif
   return BLOCK_RET(ptr);
 err_out:
   (*__errno_location())=ENOMEM;
@@ -187,10 +337,18 @@
 void* __libc_realloc(void* ptr, size_t _size) {
   register size_t size=_size;
   if (ptr) {
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+    cp_check(BLOCK_START(ptr));
+#endif
     if (size) {
       __alloc_t* tmp=BLOCK_START(ptr);
+#ifdef WANT_CONTRAPOLICE
+      size+=(sizeof(__alloc_t)+sizeof(__decoy_t));
+      if (size<(sizeof(__alloc_t)+sizeof(__decoy_t))) goto retzero;
+#else
       size+=sizeof(__alloc_t);
       if (size<sizeof(__alloc_t)) goto retzero;
+#endif
       size=(size<=__MAX_SMALL_SIZE)?GET_SIZE(size):PAGE_ALIGN(size);
       if (tmp->size!=size) {
 	if ((tmp->size<=__MAX_SMALL_SIZE)) {
@@ -199,7 +357,11 @@
 	    register __alloc_t* foo=BLOCK_START(new);
 	    size=foo->size;
 	    if (size>tmp->size) size=tmp->size;
+#ifdef WANT_CONTRAPOLICE
+            if (size) memcpy(new,ptr,size-(sizeof(__alloc_t)+sizeof(__decoy_t)));
+#else
 	    if (size) memcpy(new,ptr,size-sizeof(__alloc_t));
+#endif
 	    _alloc_libc_free(ptr);
 	  }
 	  ptr=new;
@@ -207,6 +369,9 @@
 	else {
 	  register __alloc_t* foo;
 	  size=PAGE_ALIGN(size);
+#ifdef WANT_CONTRAPOLICE
+          cp_remove_memblock(tmp);
+#endif
 	  foo=mremap(tmp,tmp->size,size,MREMAP_MAYMOVE);
 	  if (foo==MAP_FAILED) {
 retzero:
@@ -215,6 +380,11 @@
 	  }
 	  else {
 	    foo->size=size;
+#ifdef WANT_CONTRAPOLICE
+            cp_add_memblock(foo);
+            foo->alloc_size = size-(sizeof(__alloc_t)+sizeof(__decoy_t));
+            ((__decoy_t *)BLOCK_DECOY(foo))->decoy = cp_f(foo->decoy = cp_rand());
+#endif
 	    ptr=BLOCK_RET(foo);
 	  }
 	}
diff -Nur --exclude=CVS dietlibc-orig/lib/memccpy.c dietlibc-cp0.3/lib/memccpy.c
--- dietlibc-orig/lib/memccpy.c	2003-04-01 18:00:31.000000000 +0200
+++ dietlibc-cp0.3/lib/memccpy.c	2004-07-11 20:14:10.000000000 +0200
@@ -1,5 +1,6 @@
 #define _POSIX_SOURCE
 #define _XOPEN_SOURCE
+#include "dietfeatures.h"
 #include <sys/types.h>
 #include <string.h>
 
@@ -16,5 +17,8 @@
     }
     b++;
   }
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(dst);
+#endif
   return 0;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/memcpy.c dietlibc-cp0.3/lib/memcpy.c
--- dietlibc-orig/lib/memcpy.c	2004-01-31 14:48:50.000000000 +0100
+++ dietlibc-cp0.3/lib/memcpy.c	2004-07-07 11:03:37.000000000 +0200
@@ -44,6 +44,10 @@
 	    *c1++ = *c2++;
     }
 
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+    cp_check(res);
+#endif
+
     return (res);
 #endif
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/snprintf.c dietlibc-cp0.3/lib/snprintf.c
--- dietlibc-orig/lib/snprintf.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/snprintf.c	2004-07-07 10:56:34.000000000 +0200
@@ -1,3 +1,4 @@
+#include "dietfeatures.h"
 #include <stdarg.h>
 #include <sys/types.h>
 #include <stdio.h>
@@ -9,5 +10,8 @@
   va_start(arg_ptr, format);
   n=vsnprintf(str,size,format,arg_ptr);
   va_end (arg_ptr);
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(str);
+#endif
   return n;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/sprintf.c dietlibc-cp0.3/lib/sprintf.c
--- dietlibc-orig/lib/sprintf.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/sprintf.c	2004-07-07 10:56:34.000000000 +0200
@@ -1,3 +1,4 @@
+#include "dietfeatures.h"
 #include <stdarg.h>
 #include <sys/types.h>
 #include <stdlib.h>
@@ -10,5 +11,8 @@
   va_start(arg_ptr, format);
   n=vsprintf(dest,format,arg_ptr);
   va_end (arg_ptr);
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(dest);
+#endif
   return n;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strcat.c dietlibc-cp0.3/lib/strcat.c
--- dietlibc-orig/lib/strcat.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/strcat.c	2004-07-07 10:56:34.000000000 +0200
@@ -13,6 +13,9 @@
     if (!(*s = *t)) break; ++s; ++t;
 #endif
   }
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(dest);
+#endif
   return dest;
 }
 
diff -Nur --exclude=CVS dietlibc-orig/lib/strcpy.c dietlibc-cp0.3/lib/strcpy.c
--- dietlibc-orig/lib/strcpy.c	2004-01-31 14:48:51.000000000 +0100
+++ dietlibc-cp0.3/lib/strcpy.c	2004-07-07 11:06:14.000000000 +0200
@@ -9,30 +9,43 @@
     char           *res = s1;
 #ifdef WANT_SMALL_STRING_ROUTINES
     while ((*s1++ = *s2++));
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+    cp_check(res);
+#endif
     return (res);
 #else
     int             tmp;
     unsigned long   l;
 
     if (UNALIGNED(s1, s2)) {
-	while ((*s1++ = *s2++));
-	return (res);
+        while ((*s1++ = *s2++));
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+        cp_check(res);
+#endif
+        return (res);
     }
     if ((tmp = STRALIGN(s1))) {
-	while (tmp-- && (*s1++ = *s2++));
-	if (tmp != -1) return (res);
+    while (tmp-- && (*s1++ = *s2++));
+    if (tmp != -1) {
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+        cp_check(res);
+#endif
+        return (res);
     }
 
     while (1) {
-	l = *(const unsigned long *) s2;
-	if (((l - MKW(0x1)) & ~l) & MKW(0x80)) {
-	    unsigned char c;
-	    while ((*s1++ = (l & 0xff))) l>>=8;
-	    return (res);
-	}
-	*(unsigned long *) s1 = l;
-	s2 += sizeof(unsigned long);
-	s1 += sizeof(unsigned long);
+    l = *(const unsigned long *) s2;
+    if (((l - MKW(0x1)) & ~l) & MKW(0x80)) {
+        unsigned char c;
+        while ((*s1++ = (l & 0xff))) l>>=8;
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+        cp_check(res);
+#endif
+        return (res);
+    }
+    *(unsigned long *) s1 = l;
+    s2 += sizeof(unsigned long);
+    s1 += sizeof(unsigned long);
     }
 #endif
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strdup.c dietlibc-cp0.3/lib/strdup.c
--- dietlibc-orig/lib/strdup.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/strdup.c	2004-07-07 10:56:34.000000000 +0200
@@ -1,3 +1,4 @@
+#include "dietfeatures.h"
 #include <string.h>
 #include <stdlib.h>
 
@@ -5,5 +6,8 @@
   char *tmp=(char *)malloc(strlen(s)+1);
   if (!tmp) return 0;
   strcpy(tmp,s);
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(tmp);
+#endif
   return tmp;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strlcat.c dietlibc-cp0.3/lib/strlcat.c
--- dietlibc-orig/lib/strlcat.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/strlcat.c	2004-07-07 10:56:34.000000000 +0200
@@ -31,6 +31,7 @@
 static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $";
 #endif /* LIBC_SCCS and not lint */
 
+#include "dietfeatures.h"
 #include <sys/types.h>
 #include <string.h>
 
@@ -67,6 +68,8 @@
 		s++;
 	}
 	*d = '\0';
-
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+	cp_check(dst);
+#endif
 	return(dlen + (s - src));	/* count does not include NUL */
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strlcpy.c dietlibc-cp0.3/lib/strlcpy.c
--- dietlibc-orig/lib/strlcpy.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/strlcpy.c	2004-07-07 10:56:34.000000000 +0200
@@ -31,6 +31,7 @@
 static char *rcsid = "$OpenBSD: strlcpy.c,v 1.3 1999/04/24 01:17:37 millert Exp $";
 #endif /* LIBC_SCCS and not lint */
 
+#include "dietfeatures.h"
 #include <sys/types.h>
 #include <string.h>
 
@@ -63,6 +64,8 @@
 		while (*s++)
 			;
 	}
-
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+	cp_check(dst);
+#endif
 	return(s - src - 1);	/* count does not include NUL */
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strncat.c dietlibc-cp0.3/lib/strncat.c
--- dietlibc-orig/lib/strncat.c	2003-08-23 11:15:38.000000000 +0200
+++ dietlibc-cp0.3/lib/strncat.c	2004-07-07 10:56:34.000000000 +0200
@@ -30,5 +30,8 @@
   }
   *s=0;
 fini:
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(dest);
+#endif
   return dest;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strncpy.c dietlibc-cp0.3/lib/strncpy.c
--- dietlibc-orig/lib/strncpy.c	2003-04-01 18:00:32.000000000 +0200
+++ dietlibc-cp0.3/lib/strncpy.c	2004-07-07 10:56:34.000000000 +0200
@@ -11,5 +11,8 @@
   memset(dest,0,n);
 #endif
   memccpy(dest,src,0,n);
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(dest);
+#endif
   return dest;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/strtok_r.c dietlibc-cp0.3/lib/strtok_r.c
--- dietlibc-orig/lib/strtok_r.c	2003-10-21 21:35:17.000000000 +0200
+++ dietlibc-cp0.3/lib/strtok_r.c	2004-07-07 10:56:34.000000000 +0200
@@ -1,3 +1,4 @@
+#include "dietfeatures.h"
 #include <string.h>
 
 char*strtok_r(char*s,const char*delim,char**ptrptr) {
@@ -11,5 +12,8 @@
     if (__likely(*s)) *s++=0;	/* not the end ? => terminate it */
   }
   *ptrptr=s;
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(*ptrptr);
+#endif
   return tmp;
 }
diff -Nur --exclude=CVS dietlibc-orig/lib/vsnprintf.c dietlibc-cp0.3/lib/vsnprintf.c
--- dietlibc-orig/lib/vsnprintf.c	2004-04-04 01:06:48.000000000 +0200
+++ dietlibc-cp0.3/lib/vsnprintf.c	2004-07-07 10:56:34.000000000 +0200
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "dietstdio.h"
+#include "dietfeatures.h"
 
 struct str_data {
   unsigned char* str;
@@ -34,5 +35,8 @@
     if (size!=(size_t)-1 && ((size_t)n>=size)) str[size-1]=0;
     else str[n]=0;
   }
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(str);
+#endif
   return n;
 }
diff -Nur --exclude=CVS dietlibc-orig/libshell/realpath.c dietlibc-cp0.3/libshell/realpath.c
--- dietlibc-orig/libshell/realpath.c	2004-05-16 12:29:31.000000000 +0200
+++ dietlibc-cp0.3/libshell/realpath.c	2004-07-11 20:11:30.000000000 +0200
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
+#include "dietfeatures.h"
 
 static char* myrealpath(const char* file, char* dest, int count) {
 /* assume dest has PATH_MAX space */
@@ -23,18 +24,35 @@
     if (c-file>PATH_MAX) return 0;
     memcpy(buf,file,c-file);
     buf[c-file]=0;
-    if (chdir(buf)==-1) return 0;
+    if (chdir(buf)==-1) {
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+      cp_check(dest);
+#endif
+      return 0;
+    }
     file=c+1;
   }
-  if (readlink(file,buf,PATH_MAX)==0)
+  if (readlink(file,buf,PATH_MAX)==0) {
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+    cp_check(dest);
+#endif
     return myrealpath(buf,dest,count-1);
-  if (getcwd(dest,PATH_MAX)==0) return 0;
+  }
+  if (getcwd(dest,PATH_MAX)==0) {
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+    cp_check(dest);
+#endif
+    return 0;
+  }
   i=strlen(dest); dest[i]='/'; ++i;
   for (; i<PATH_MAX-1; ++i) {
     if (!(dest[i]=*file)) break;
     ++file;
   }
   dest[i]=0;
+#if defined(WANT_CONTRAPOLICE) && defined(CP_FORCE_CHECK)
+  cp_check(dest);
+#endif
   return dest;
 }
 
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test1.c dietlibc-cp0.3/test/cp-test1.c
--- dietlibc-orig/test/cp-test1.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test1.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  assert(x);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test10.c dietlibc-cp0.3/test/cp-test10.c
--- dietlibc-orig/test/cp-test10.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test10.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  assert(x);
+  memset(x,'x',101);
+  free(x);
+  cp_check_all();
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test11.c dietlibc-cp0.3/test/cp-test11.c
--- dietlibc-orig/test/cp-test11.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test11.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  assert(x);
+  memset(x,'x',101);
+  x = realloc(x,50);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test12.c dietlibc-cp0.3/test/cp-test12.c
--- dietlibc-orig/test/cp-test12.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test12.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(5000);
+  assert(x);
+  memset(x,'x',5001);
+  x = realloc(x,4096);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test13.c dietlibc-cp0.3/test/cp-test13.c
--- dietlibc-orig/test/cp-test13.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test13.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(5000);
+  assert(x);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test14.c dietlibc-cp0.3/test/cp-test14.c
--- dietlibc-orig/test/cp-test14.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test14.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(5000);
+  assert(x);
+  memset(x,'x',5000);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test15.c dietlibc-cp0.3/test/cp-test15.c
--- dietlibc-orig/test/cp-test15.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test15.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(5000);
+  assert(x);
+  memset(x,'x',5001); /* buffer overflow */
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test2.c dietlibc-cp0.3/test/cp-test2.c
--- dietlibc-orig/test/cp-test2.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test2.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  assert(x);
+  memset(x,'x',100);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test3.c dietlibc-cp0.3/test/cp-test3.c
--- dietlibc-orig/test/cp-test3.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test3.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  assert(x);
+  memset(x,'x',101); /* buffer overflow */
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test4.c dietlibc-cp0.3/test/cp-test4.c
--- dietlibc-orig/test/cp-test4.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test4.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  void * y;
+  assert(x);
+  memset(x,'x',101); /* buffer overflow */
+  y = malloc(30);
+  memset(y,'y',31);
+  cp_check_all();
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test5.c dietlibc-cp0.3/test/cp-test5.c
--- dietlibc-orig/test/cp-test5.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test5.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  void * y;
+  assert(x);
+  memset(x,'x',100); /* buffer overflow */
+  y = malloc(30);
+  memset(y,'y',31);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test6.c dietlibc-cp0.3/test/cp-test6.c
--- dietlibc-orig/test/cp-test6.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test6.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  void * y;
+  assert(x);
+  memset(x,'x',101); /* buffer overflow */
+  y = malloc(30);
+  memset(y,'y',31);
+  cp_check(y);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test7.c dietlibc-cp0.3/test/cp-test7.c
--- dietlibc-orig/test/cp-test7.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test7.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  void * y;
+  assert(x);
+  memset(x,'x',100);
+  y = malloc(30);
+  memset(y,'y',31); /* buffer overflow */
+  cp_check(y);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test8.c dietlibc-cp0.3/test/cp-test8.c
--- dietlibc-orig/test/cp-test8.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test8.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  void * y;
+  assert(x);
+  memset(x,'x',100);
+  y = malloc(30);
+  memset(y,'y',30);
+  cp_check(y);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/cp-test9.c dietlibc-cp0.3/test/cp-test9.c
--- dietlibc-orig/test/cp-test9.c	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/cp-test9.c	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,11 @@
+#include <stdlib.h>
+#include <assert.h>
+
+int main(void) {
+  void * x = malloc(100);
+  assert(x);
+  memset(x,'x',101);
+  free(x);
+  cp_check(x);
+  return 0;
+}
diff -Nur --exclude=CVS dietlibc-orig/test/run-cptest.sh dietlibc-cp0.3/test/run-cptest.sh
--- dietlibc-orig/test/run-cptest.sh	1970-01-01 01:00:00.000000000 +0100
+++ dietlibc-cp0.3/test/run-cptest.sh	2004-07-07 10:56:34.000000000 +0200
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+FILES="cp-test1.c cp-test2.c cp-test3.c cp-test4.c cp-test5.c cp-test6.c cp-test7.c cp-test8.c cp-test9.c cp-test10.c cp-test11.c cp-test12.c cp-test13.c cp-test14.c cp-test15.c"
+
+compile() {
+  diet -Os gcc $1 -o $2 2> /dev/null
+}
+
+run() {
+  echo -n "running $1 ..."
+  if ./$1 ; then
+    echo "OK"
+  else
+    echo "FAILED"
+  fi
+}
+
+echo "****************************************************************"
+echo "* ContraPolice test suite (kinda)                              *"
+echo "* tests that fail: 3, 4, 6, 7, 11, 12 and 15                   *"
+echo "****************************************************************"
+
+
+for f in $FILES ; do
+  f2=`basename $f .c`
+  compile $f $f2
+  run $f2
+  rm -f $f2
+done
+
