Skip to content

Commit a90358d

Browse files
committed
Check calls to malloc() everywhere.
Try to let programs continue running.
1 parent 052644c commit a90358d

File tree

22 files changed

+95
-22
lines changed

22 files changed

+95
-22
lines changed

doc/doc-txt/ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ LC/01 Prefer the use of size_t for variables representing sizes. Even if most
5151
LC/02 Some values representing maximum path size were hard coded.
5252
They are now replaced with the PATH_MAX macro.
5353

54+
LC/03 As everybody knows, malloc() can fails by returning 0. The return values
55+
weren’t checked everywhere.
56+
The values are checked manually in order handle the situation in way that
57+
let the program continue running. Otherwise, replace direct calls to
58+
malloc() with store_malloc() from the project standard memory management
59+
facilities in order to stop the program.
60+
Except if it isn’t possible to call store_malloc() or that some ressources
61+
cleanup need to done.
62+
5463

5564
Exim version 4.87
5665
-----------------

src/OS/Makefile-Base

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ exim_tidydb: $(OBJ_TIDYDB)
408408

409409
# The utility for building dbm files
410410

411-
exim_dbmbuild: exim_dbmbuild.o
411+
exim_dbmbuild: util-store.o exim_dbmbuild.o
412412
@echo "$(LNCC) -o exim_dbmbuild"
413-
$(FE)$(LNCC) $(CFLAGS) $(INCLUDE) -o exim_dbmbuild $(LFLAGS) exim_dbmbuild.o \
413+
$(FE)$(LNCC) $(CFLAGS) $(INCLUDE) -o exim_dbmbuild $(LFLAGS) exim_dbmbuild.o util-store.o \
414414
$(LIBS) $(EXTRALIBS) $(DBMLIB)
415415
@if [ x"$(STRIP_COMMAND)" != x"" ]; then \
416416
echo $(STRIP_COMMAND) exim_dbmbuild; \
@@ -421,11 +421,11 @@ exim_dbmbuild: exim_dbmbuild.o
421421

422422
# The utility for locking a mailbox while messing around with it
423423

424-
exim_lock: exim_lock.c os.h
424+
exim_lock: util-store.o exim_lock.c os.h
425425
@echo "$(CC) exim_lock.c"
426426
$(FE)$(CC) -c $(CFLAGS) $(INCLUDE) exim_lock.c
427427
@echo "$(LNCC) -o exim_lock"
428-
$(FE)$(LNCC) -o exim_lock $(LFLAGS) exim_lock.o \
428+
$(FE)$(LNCC) -o exim_lock $(LFLAGS) exim_lock.o util-store.o \
429429
$(LIBS) $(EXTRALIBS)
430430
@if [ x"$(STRIP_COMMAND)" != x"" ]; then \
431431
echo $(STRIP_COMMAND) exim_lock; \

src/exim_monitor/em_version.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "mytypes.h"
99
#include "macros.h"
10+
#include "store.h"
1011
#include <string.h>
1112
#include <stdlib.h>
1213

@@ -25,7 +26,7 @@ Ustrcpy(today, __DATE__);
2526
if (today[4] == ' ') i = 1;
2627
today[3] = today[6] = '-';
2728

28-
version_date = (uschar *)malloc(32);
29+
version_date = (uschar *)store_malloc(32);
2930
version_date[0] = 0;
3031
Ustrncat(version_date, today+4+i, 3-i);
3132
Ustrncat(version_date, today, 4);

src/exim_monitor/em_xs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void xs_SetValues(Widget w, Cardinal num_args, ...)
3030
{
3131
int i;
3232
va_list ap;
33-
Arg *aa = (num_args > 15)? (Arg *)malloc(num_args*sizeof(Arg)) : xs_temparg;
33+
Arg *aa = (num_args > 15)? (Arg *)store_malloc(num_args*sizeof(Arg)) : xs_temparg;
3434
va_start(ap, num_args);
3535
for (i = 0; i < num_args; i++)
3636
{

src/src/buildconfig.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ else if (isgroup)
688688
while (*p != 0) if (*p++ == ':') count++;
689689

690690
vector = malloc((count+1) * sizeof(uid_t));
691+
if (!vector) {
692+
printf("memory allocation falied");
693+
return 1;
694+
}
691695
vector[0] = (uid_t)count;
692696

693697
for (i = 1, j = 0; i <= count; list++, i++)

src/src/dbfn.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ spool_directory = argv[1];
465465
debug_selector = D_all - D_memory;
466466
debug_file = stderr;
467467
big_buffer = malloc(big_buffer_size);
468+
if (!big_buffer)
469+
{
470+
printf("Memory allocation failed!\n");
471+
return 1;
472+
}
468473

469474
for (i = 0; i < max_db; i++) dbblock[i].dbptr = NULL;
470475

src/src/dbstuff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ utilities as well as the main Exim binary. */
2222
/* ************************* tdb interface ************************ */
2323

2424
#include <tdb.h>
25+
#include "store.h"
2526

2627
/* Basic DB type */
2728
#define EXIM_DB TDB_CONTEXT
@@ -64,7 +65,7 @@ tdb_traverse to be called) */
6465

6566
/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
6667
#define EXIM_DBCREATE_CURSOR(db, cursor) { \
67-
*(cursor) = malloc(sizeof(TDB_DATA)); (*(cursor))->dptr = NULL; }
68+
*(cursor) = store_malloc(sizeof(TDB_DATA)); (*(cursor))->dptr = NULL; }
6869

6970
/* EXIM_DBSCAN - This is complicated because we have to free the last datum
7071
free() must not die when passed NULL */

src/src/dmarc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static dmarc_exim_p dmarc_policy_description[] = {
5757
static error_block *
5858
add_to_eblock(error_block *eblock, uschar *t1, uschar *t2)
5959
{
60-
error_block *eb = malloc(sizeof(error_block));
60+
error_block *eb = store_malloc(sizeof(error_block));
6161
if (eblock == NULL)
6262
eblock = eb;
6363
else

src/src/exim.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,7 +3973,7 @@ EXIM_TMPDIR by the build scripts.
39733973
if (Ustrncmp(*p, "TMPDIR=", 7) == 0 &&
39743974
Ustrcmp(*p+7, EXIM_TMPDIR) != 0)
39753975
{
3976-
uschar *newp = malloc(Ustrlen(EXIM_TMPDIR) + 8);
3976+
uschar *newp = store_malloc(Ustrlen(EXIM_TMPDIR) + 8);
39773977
sprintf(CS newp, "TMPDIR=%s", EXIM_TMPDIR);
39783978
*p = newp;
39793979
DEBUG(D_any) debug_printf("reset TMPDIR=%s in environment\n", EXIM_TMPDIR);
@@ -4010,15 +4010,15 @@ else
40104010
int count = 0;
40114011
if (environ) while (*p++ != NULL) count++;
40124012
if (envtz == NULL) count++;
4013-
newp = new = malloc(sizeof(uschar *) * (count + 1));
4013+
newp = new = store_malloc(sizeof(uschar *) * (count + 1));
40144014
if (environ) for (p = USS environ; *p != NULL; p++)
40154015
{
40164016
if (Ustrncmp(*p, "TZ=", 3) == 0) continue;
40174017
*newp++ = *p;
40184018
}
40194019
if (timezone_string != NULL)
40204020
{
4021-
*newp = malloc(Ustrlen(timezone_string) + 4);
4021+
*newp = store_malloc(Ustrlen(timezone_string) + 4);
40224022
sprintf(CS *newp++, "TZ=%s", timezone_string);
40234023
}
40244024
*newp = NULL;

src/src/exim_dbmbuild.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ uschar *bptr;
151151
uschar keybuffer[256];
152152
uschar temp_dbmname[512];
153153
uschar real_dbmname[512];
154-
uschar *buffer = malloc(max_outsize);
155-
uschar *line = malloc(max_insize);
154+
uschar *buffer = store_malloc(max_outsize);
155+
uschar *line = store_malloc(max_insize);
156156

157157
while (argc > 1)
158158
{

0 commit comments

Comments
 (0)