Skip to content

Commit a90358d

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

22 files changed

+95
-22
lines changed

doc/doc-txt/ChangeLog

+9
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

+4-4
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

+2-1
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

+1-1
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

+4
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

+5
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

+2-1
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

+1-1
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

+3-3
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

+2-2
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
{

src/src/exim_lock.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Copyright (c) The Exim Maintainers 2016
1414
*/
1515

1616
#include "os.h"
17+
#include "store.h"
1718

1819
#include <stdio.h>
1920
#include <stdlib.h>
@@ -299,9 +300,9 @@ if (use_lockfile)
299300
primary_hostname = s.nodename;
300301

301302
len = (int)strlen(filename);
302-
lockname = malloc(len + 8);
303+
lockname = store_malloc(len + 8);
303304
sprintf(lockname, "%s.lock", filename);
304-
hitchname = malloc(len + 32 + (int)strlen(primary_hostname));
305+
hitchname = store_malloc(len + 32 + (int)strlen(primary_hostname));
305306

306307
/* Presumably, this must match appendfile.c */
307308
sprintf(hitchname, "%s.%s.%08x.%08x", lockname, primary_hostname,

src/src/expand.c

+6
Original file line numberDiff line numberDiff line change
@@ -7746,6 +7746,12 @@ debug_selector = D_v;
77467746
debug_file = stderr;
77477747
debug_fd = fileno(debug_file);
77487748
big_buffer = malloc(big_buffer_size);
7749+
if (!big_buffer)
7750+
{
7751+
printf("** error Memory allocation failed!\n");
7752+
exit(EXIT_FAILURE);
7753+
}
7754+
77497755

77507756
for (i = 1; i < argc; i++)
77517757
{

src/src/hash.c

+6
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,16 @@ for (i = 0; i < sizeof(tests)/sizeof(uschar *); i ++)
787787
/* 1 000 000 repetitions of "a" */
788788

789789
ctest = malloc(1000000);
790+
if(!ctest)
791+
{
792+
printf("Memory allocation failed!\n*** No match ***\n");
793+
exit(EXIT_FAILURE);
794+
}
790795
memset(ctest, 'a', 1000000);
791796

792797
printf("1 000 000 repetitions of 'a'\n");
793798
printf("Should be: %s\n", atest);
799+
free(ctest);
794800
native_sha1_start(&base);
795801
native_sha1_end(&base, ctest, 1000000, digest);
796802
for (j = 0; j < 20; j++) sprintf(s+2*j, "%02X", digest[j]);

src/src/mime.c

+2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ FILE *f = NULL;
195195
uschar *filename;
196196

197197
filename = (uschar *)malloc(PATH_MAX);
198+
if (!filename)
199+
return NULL;
198200

199201
if (pname && fname)
200202
{

src/src/smtp_in.c

+2-7
Original file line numberDiff line numberDiff line change
@@ -1890,10 +1890,7 @@ acl_var_c = NULL;
18901890

18911891
/* Allow for trailing 0 in the command and data buffers. */
18921892

1893-
smtp_cmd_buffer = (uschar *)malloc(2*smtp_cmd_buffer_size + 2);
1894-
if (smtp_cmd_buffer == NULL)
1895-
log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1896-
"malloc() failed for SMTP command buffer");
1893+
smtp_cmd_buffer = (uschar *)store_malloc(2*smtp_cmd_buffer_size + 2);
18971894
smtp_cmd_buffer[0] = 0;
18981895
smtp_data_buffer = smtp_cmd_buffer + smtp_cmd_buffer_size + 1;
18991896

@@ -1915,9 +1912,7 @@ else
19151912
/* Set up the buffer for inputting using direct read() calls, and arrange to
19161913
call the local functions instead of the standard C ones. */
19171914

1918-
smtp_inbuffer = (uschar *)malloc(in_buffer_size);
1919-
if (smtp_inbuffer == NULL)
1920-
log_write(0, LOG_MAIN|LOG_PANIC_DIE, "malloc() failed for SMTP input buffer");
1915+
smtp_inbuffer = (uschar *)store_malloc(in_buffer_size);
19211916
receive_getc = smtp_getc;
19221917
receive_ungetc = smtp_ungetc;
19231918
receive_feof = smtp_feof;

src/src/store.c

+12
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,14 @@ if ((char *)ptr < bc || (char *)ptr > bc + b->length)
345345
if ((char *)ptr >= bc && (char *)ptr <= bc + b->length) break;
346346
}
347347
if (b == NULL)
348+
#ifndef COMPILE_UTILITY
348349
log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal error: store_reset(%p) "
349350
"failed: pool=%d %-14s %4d", ptr, store_pool, filename, linenumber);
351+
#else
352+
fprintf(stderr, "internal error: store_reset(%p) "
353+
"failed: pool=%d %-14s %4d\n", ptr, store_pool, filename, linenumber);
354+
exit(EXIT_FAILURE);
355+
#endif
350356
}
351357

352358
/* Back up, rounding to the alignment if necessary. When testing, flatten
@@ -500,8 +506,14 @@ if (size < 16) size = 16;
500506
yield = malloc(size);
501507

502508
if (yield == NULL)
509+
#ifndef COMPILE_UTILITY
503510
log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to malloc %zd bytes of memory: "
504511
"called from line %d of %s", size, linenumber, filename);
512+
#else
513+
fprintf(stderr, "failed to malloc %zd bytes of memory: "
514+
"called from line %d of %s\n", size, linenumber, filename);
515+
exit(EXIT_FAILURE);
516+
#endif
505517

506518
nonpool_malloc += size;
507519

src/src/store.h

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#ifndef STORE_H
1111
#define STORE_H
12+
#include <stddef.h>
1213

1314
/* Define symbols for identifying the store pools. */
1415

@@ -37,6 +38,9 @@ tracing information for debugging. */
3738
#define store_release(addr) store_release_3(addr, __FILE__, __LINE__)
3839
#define store_reset(addr) store_reset_3(addr, __FILE__, __LINE__)
3940

41+
#ifndef BOOL
42+
#include "mytypes.h"
43+
#endif
4044

4145
/* The real functions */
4246

src/src/string.c

+5
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,11 @@ while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
18471847
else
18481848
{
18491849
uschar *sss = malloc(s - ss + 1);
1850+
if(!sss)
1851+
{
1852+
printf("***ERROR\nMemory allocation failed!\n");
1853+
exit(EXIT_FAILURE);
1854+
}
18501855
Ustrncpy(sss, ss, s-ss);
18511856
args[n++] = sss;
18521857
}

src/src/transport.c

+7
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,13 @@ while (1)
17251725
/* create an array to read entire message queue into memory for processing */
17261726

17271727
msgq = (msgq_t*) malloc(sizeof(msgq_t) * host_record->count);
1728+
1729+
if(!msgq) {
1730+
dbfn_close(dbm_file);
1731+
DEBUG(D_transport) debug_printf("memory allocation for message queue failed\n");
1732+
return FALSE;
1733+
}
1734+
17281735
msgq_count = host_record->count;
17291736
msgq_actual = msgq_count;
17301737

test/src/cf.c

+5
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ bufbase_one = (char *)malloc(storesize);
680680
buftop_one = bufbase_one + storesize;
681681
bufbase_two = (char *)malloc(storesize);
682682
buftop_two = bufbase_two + storesize;
683+
if (!bufbase_one || !buftop_two)
684+
{
685+
fprintf(stderr, "Memory allocation failed!\n");
686+
exit(EXIT_FAILURE);
687+
}
683688

684689
/* Do the job */
685690

test/src/fakens.c

+5
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ va_start(ap, format);
182182
vsprintf(buffer, CS format, ap);
183183
va_end(ap);
184184
yield = (uschar *)malloc(Ustrlen(buffer) + 1);
185+
if (!yield)
186+
{
187+
fprintf(stderr, "Memory allocation failed!\n");
188+
exit(EXIT_FAILURE);
189+
}
185190
Ustrcpy(yield, buffer);
186191
return yield;
187192
}

test/src/server.c

+5
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
460460
buffer[n] = 0;
461461
if (strcmp(CS buffer, "++++") == 0) break;
462462
next = malloc(sizeof(line) + n);
463+
if(!next)
464+
{
465+
fprintf(stderr, "memory allocation failed\n");
466+
exit(1);
467+
}
463468
next->next = NULL;
464469
d = next->line;
465470
{

0 commit comments

Comments
 (0)