Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e28e5e

Browse files
committed
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
argument to malloc() (size_t or unsigned int) * listobject.c: check for overflow of the size of the object, so things like range(0x7fffffff) will raise MemoryError instead of calling malloc() with -4 (and then crashing -- malloc's fault)
1 parent b001f7a commit 1e28e5e

4 files changed

Lines changed: 83 additions & 3 deletions

File tree

Include/allobjects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5050
#include "fileobject.h"
5151

5252
#include "errors.h"
53-
#include "malloc.h"
53+
#include "mymalloc.h"
5454

5555
extern char *strdup PROTO((const char *));
5656
extern void fatal PROTO((char *));

Include/mymalloc.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/***********************************************************
2+
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3+
Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
11+
supporting documentation, and that the names of Stichting Mathematisch
12+
Centrum or CWI not be used in advertising or publicity pertaining to
13+
distribution of the software without specific, written prior permission.
14+
15+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22+
23+
******************************************************************/
24+
25+
/* Lowest-level memory allocation interface */
26+
27+
#ifdef macintosh
28+
#define ANY void
29+
#ifndef THINK_C_3_0
30+
#define HAVE_STDLIB
31+
#endif
32+
#endif
33+
34+
#ifdef sun
35+
/* Maybe not for very old versions of SunOS ? */
36+
#define HAVE_STDLIB
37+
#endif
38+
39+
#ifdef sgi
40+
#define HAVE_STDLIB
41+
#endif
42+
43+
#ifdef __STDC__
44+
#define ANY void
45+
#define HAVE_STDLIB
46+
#endif
47+
48+
#ifndef ANY
49+
#define ANY char
50+
#endif
51+
52+
#ifndef NULL
53+
#define NULL 0
54+
#endif
55+
56+
#define NEW(type, n) ( (type *) malloc((n) * sizeof(type)) )
57+
#define RESIZE(p, type, n) \
58+
if ((p) == NULL) \
59+
(p) = (type *) malloc((n) * sizeof(type)); \
60+
else \
61+
(p) = (type *) realloc((ANY *)(p), (n) * sizeof(type))
62+
#define DEL(p) free((ANY *)p)
63+
#define XDEL(p) if ((p) == NULL) ; else DEL(p)
64+
65+
#ifdef HAVE_STDLIB
66+
#include <stdlib.h>
67+
#define MALLARG size_t
68+
#else
69+
#define MALLARG size_t
70+
extern ANY *malloc PROTO((MALLARG));
71+
extern ANY *calloc PROTO((MALLARG, MALLARG));
72+
extern ANY *realloc PROTO((ANY *, MALLARG));
73+
extern void free PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
74+
#endif

Include/pgenheaders.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4949
#endif
5050

5151
#include "PROTO.h"
52-
#include "malloc.h"
52+
#include "mymalloc.h"
5353

5454
extern void fatal PROTO((char *));

Objects/listobject.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ newlistobject(size)
3434
{
3535
int i;
3636
listobject *op;
37+
MALLARG nbytes;
3738
if (size < 0) {
3839
err_badcall();
3940
return NULL;
4041
}
42+
nbytes = size * sizeof(object *);
43+
/* Check for overflow */
44+
if (nbytes / sizeof(object *) != size) {
45+
return err_nomem();
46+
}
4147
op = (listobject *) malloc(sizeof(listobject));
4248
if (op == NULL) {
4349
return err_nomem();
@@ -46,7 +52,7 @@ newlistobject(size)
4652
op->ob_item = NULL;
4753
}
4854
else {
49-
op->ob_item = (object **) malloc(size * sizeof(object *));
55+
op->ob_item = (object **) malloc(nbytes);
5056
if (op->ob_item == NULL) {
5157
free((ANY *)op);
5258
return err_nomem();

0 commit comments

Comments
 (0)