Back to index

glibc  2.9
tst-mcheck.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 
00024 static int errors = 0;
00025 
00026 static void
00027 merror (const char *msg)
00028 {
00029   ++errors;
00030   printf ("Error: %s\n", msg);
00031 }
00032 
00033 int
00034 main (void)
00035 {
00036   void *p, *q;
00037 
00038   errno = 0;
00039 
00040   p = malloc (-1);
00041 
00042   if (p != NULL)
00043     merror ("malloc (-1) succeeded.");
00044   else if (errno != ENOMEM)
00045     merror ("errno is not set correctly.");
00046 
00047   p = malloc (10);
00048   if (p == NULL)
00049     merror ("malloc (10) failed.");
00050 
00051   p = realloc (p, 0);
00052   if (p != NULL)
00053     merror ("realloc (p, 0) failed.");
00054 
00055   p = malloc (0);
00056   if (p == NULL)
00057     merror ("malloc (0) failed.");
00058 
00059   p = realloc (p, 0);
00060   if (p != NULL)
00061     merror ("realloc (p, 0) failed.");
00062 
00063   q = malloc (256);
00064   if (q == NULL)
00065     merror ("malloc (256) failed.");
00066 
00067   p = malloc (512);
00068   if (p == NULL)
00069     merror ("malloc (512) failed.");
00070 
00071   if (realloc (p, -256) != NULL)
00072     merror ("realloc (p, -256) succeeded.");
00073   else if (errno != ENOMEM)
00074     merror ("errno is not set correctly.");
00075 
00076   free (p);
00077 
00078   p = malloc (512);
00079   if (p == NULL)
00080     merror ("malloc (512) failed.");
00081 
00082   if (realloc (p, -1) != NULL)
00083     merror ("realloc (p, -1) succeeded.");
00084   else if (errno != ENOMEM)
00085     merror ("errno is not set correctly.");
00086 
00087   free (p);
00088   free (q);
00089 
00090   return errors != 0;
00091 }