aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorPaul Eggert2011-07-27 17:48:01 -0700
committerPaul Eggert2011-07-27 17:48:01 -0700
commit044c22e545acef592ed95e4e3bb9f8aeff67291a (patch)
tree167a4c706b62b12ea979bdf6ad47e70b66bb0394 /src/gmalloc.c
parentdbf38e02c9ade4979418f24a99962cfef170b957 (diff)
parent8265d3bb30544e58683fc16e23f9908f3d5d0abc (diff)
downloademacs-044c22e545acef592ed95e4e3bb9f8aeff67291a.tar.gz
emacs-044c22e545acef592ed95e4e3bb9f8aeff67291a.zip
Merge: Integer signedness and overflow and related fixes.
Fixes: debbugs:9079
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c158
1 files changed, 4 insertions, 154 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index a023d2d78e5..fa4aa1fdf6a 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -41,37 +41,13 @@ Fifth Floor, Boston, MA 02110-1301, USA.
41#define USE_PTHREAD 41#define USE_PTHREAD
42#endif 42#endif
43 43
44#if ((defined __cplusplus || (defined (__STDC__) && __STDC__) \
45 || defined STDC_HEADERS || defined PROTOTYPES))
46#undef PP 44#undef PP
47#define PP(args) args 45#define PP(args) args
48#undef __ptr_t 46#undef __ptr_t
49#define __ptr_t void * 47#define __ptr_t void *
50#else /* Not C++ or ANSI C. */
51#undef PP
52#define PP(args) ()
53#undef __ptr_t
54#define __ptr_t char *
55#endif /* C++ or ANSI C. */
56 48
57#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
58#include <string.h> 49#include <string.h>
59#else
60#ifndef memset
61#define memset(s, zero, n) bzero ((s), (n))
62#endif
63#ifndef memcpy
64#define memcpy(d, s, n) bcopy ((s), (d), (n))
65#endif
66#endif
67
68#ifdef HAVE_LIMITS_H
69#include <limits.h> 50#include <limits.h>
70#endif
71#ifndef CHAR_BIT
72#define CHAR_BIT 8
73#endif
74
75#include <unistd.h> 51#include <unistd.h>
76 52
77#ifdef USE_PTHREAD 53#ifdef USE_PTHREAD
@@ -86,26 +62,9 @@ extern "C"
86{ 62{
87#endif 63#endif
88 64
89#ifdef STDC_HEADERS
90#include <stddef.h> 65#include <stddef.h>
91#define __malloc_size_t size_t 66#define __malloc_size_t size_t
92#define __malloc_ptrdiff_t ptrdiff_t 67#define __malloc_ptrdiff_t ptrdiff_t
93#else
94#ifdef __GNUC__
95#include <stddef.h>
96#ifdef __SIZE_TYPE__
97#define __malloc_size_t __SIZE_TYPE__
98#endif
99#endif
100#ifndef __malloc_size_t
101#define __malloc_size_t unsigned int
102#endif
103#define __malloc_ptrdiff_t int
104#endif
105
106#ifndef NULL
107#define NULL 0
108#endif
109 68
110 69
111/* Allocate SIZE bytes of memory. */ 70/* Allocate SIZE bytes of memory. */
@@ -1069,20 +1028,6 @@ Fifth Floor, Boston, MA 02110-1301, USA.
1069#endif 1028#endif
1070 1029
1071 1030
1072/* Cope with systems lacking `memmove'. */
1073#ifndef memmove
1074#if (!defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
1075#ifdef emacs
1076#undef __malloc_safe_bcopy
1077#define __malloc_safe_bcopy safe_bcopy
1078#endif
1079/* This function is defined in realloc.c. */
1080extern void __malloc_safe_bcopy PP ((__ptr_t, __ptr_t, __malloc_size_t));
1081#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
1082#endif
1083#endif
1084
1085
1086/* Debugging hook for free. */ 1031/* Debugging hook for free. */
1087void (*__free_hook) PP ((__ptr_t __ptr)); 1032void (*__free_hook) PP ((__ptr_t __ptr));
1088 1033
@@ -1402,85 +1347,6 @@ Fifth Floor, Boston, MA 02110-1301, USA.
1402#endif 1347#endif
1403 1348
1404 1349
1405
1406/* Cope with systems lacking `memmove'. */
1407#if (!defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
1408
1409#ifdef emacs
1410#undef __malloc_safe_bcopy
1411#define __malloc_safe_bcopy safe_bcopy
1412#else
1413
1414/* Snarfed directly from Emacs src/dispnew.c:
1415 XXX Should use system bcopy if it handles overlap. */
1416
1417/* Like bcopy except never gets confused by overlap. */
1418
1419void
1420__malloc_safe_bcopy (afrom, ato, size)
1421 __ptr_t afrom;
1422 __ptr_t ato;
1423 __malloc_size_t size;
1424{
1425 char *from = afrom, *to = ato;
1426
1427 if (size <= 0 || from == to)
1428 return;
1429
1430 /* If the source and destination don't overlap, then bcopy can
1431 handle it. If they do overlap, but the destination is lower in
1432 memory than the source, we'll assume bcopy can handle that. */
1433 if (to < from || from + size <= to)
1434 bcopy (from, to, size);
1435
1436 /* Otherwise, we'll copy from the end. */
1437 else
1438 {
1439 register char *endf = from + size;
1440 register char *endt = to + size;
1441
1442 /* If TO - FROM is large, then we should break the copy into
1443 nonoverlapping chunks of TO - FROM bytes each. However, if
1444 TO - FROM is small, then the bcopy function call overhead
1445 makes this not worth it. The crossover point could be about
1446 anywhere. Since I don't think the obvious copy loop is too
1447 bad, I'm trying to err in its favor. */
1448 if (to - from < 64)
1449 {
1450 do
1451 *--endt = *--endf;
1452 while (endf != from);
1453 }
1454 else
1455 {
1456 for (;;)
1457 {
1458 endt -= (to - from);
1459 endf -= (to - from);
1460
1461 if (endt < to)
1462 break;
1463
1464 bcopy (endf, endt, to - from);
1465 }
1466
1467 /* If SIZE wasn't a multiple of TO - FROM, there will be a
1468 little left over. The amount left over is
1469 (endt + (to - from)) - to, which is endt - from. */
1470 bcopy (from, to, endt - from);
1471 }
1472 }
1473}
1474#endif /* emacs */
1475
1476#ifndef memmove
1477extern void __malloc_safe_bcopy PP ((__ptr_t, __ptr_t, __malloc_size_t));
1478#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
1479#endif
1480
1481#endif
1482
1483
1484#define min(A, B) ((A) < (B) ? (A) : (B)) 1350#define min(A, B) ((A) < (B) ? (A) : (B))
1485 1351
1486/* Debugging hook for realloc. */ 1352/* Debugging hook for realloc. */
@@ -1983,22 +1849,6 @@ struct hdr
1983 unsigned long int magic; /* Magic number to check header integrity. */ 1849 unsigned long int magic; /* Magic number to check header integrity. */
1984 }; 1850 };
1985 1851
1986#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
1987#define flood memset
1988#else
1989static void flood (__ptr_t, int, __malloc_size_t);
1990static void
1991flood (ptr, val, size)
1992 __ptr_t ptr;
1993 int val;
1994 __malloc_size_t size;
1995{
1996 char *cp = ptr;
1997 while (size--)
1998 *cp++ = val;
1999}
2000#endif
2001
2002static enum mcheck_status checkhdr (const struct hdr *); 1852static enum mcheck_status checkhdr (const struct hdr *);
2003static enum mcheck_status 1853static enum mcheck_status
2004checkhdr (hdr) 1854checkhdr (hdr)
@@ -2037,7 +1887,7 @@ freehook (ptr)
2037 hdr = ((struct hdr *) ptr) - 1; 1887 hdr = ((struct hdr *) ptr) - 1;
2038 checkhdr (hdr); 1888 checkhdr (hdr);
2039 hdr->magic = MAGICFREE; 1889 hdr->magic = MAGICFREE;
2040 flood (ptr, FREEFLOOD, hdr->size); 1890 memset (ptr, FREEFLOOD, hdr->size);
2041 } 1891 }
2042 else 1892 else
2043 hdr = NULL; 1893 hdr = NULL;
@@ -2063,7 +1913,7 @@ mallochook (size)
2063 hdr->size = size; 1913 hdr->size = size;
2064 hdr->magic = MAGICWORD; 1914 hdr->magic = MAGICWORD;
2065 ((char *) &hdr[1])[size] = MAGICBYTE; 1915 ((char *) &hdr[1])[size] = MAGICBYTE;
2066 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size); 1916 memset ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
2067 return (__ptr_t) (hdr + 1); 1917 return (__ptr_t) (hdr + 1);
2068} 1918}
2069 1919
@@ -2083,7 +1933,7 @@ reallochook (ptr, size)
2083 1933
2084 checkhdr (hdr); 1934 checkhdr (hdr);
2085 if (size < osize) 1935 if (size < osize)
2086 flood ((char *) ptr + size, FREEFLOOD, osize - size); 1936 memset ((char *) ptr + size, FREEFLOOD, osize - size);
2087 } 1937 }
2088 1938
2089 __free_hook = old_free_hook; 1939 __free_hook = old_free_hook;
@@ -2100,7 +1950,7 @@ reallochook (ptr, size)
2100 hdr->magic = MAGICWORD; 1950 hdr->magic = MAGICWORD;
2101 ((char *) &hdr[1])[size] = MAGICBYTE; 1951 ((char *) &hdr[1])[size] = MAGICBYTE;
2102 if (size > osize) 1952 if (size > osize)
2103 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize); 1953 memset ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
2104 return (__ptr_t) (hdr + 1); 1954 return (__ptr_t) (hdr + 1);
2105} 1955}
2106 1956