Skip to content

Commit 417647a

Browse files
committed
Check return values of calls to malloc() everywhere. Try to let programs continue running.
1 parent 2707823 commit 417647a

30 files changed

+136
-59
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: 2 additions & 2 deletions
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
{
@@ -39,7 +39,7 @@ for (i = 0; i < num_args; i++)
3939
}
4040
va_end(ap);
4141
XtSetValues(w, aa, num_args);
42-
if (num_args > 15) free(aa);
42+
if (num_args > 15) store_free(aa);
4343
}
4444

4545
/* End of em_xs.c */

src/src/auths/auth-spa.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extern int DEBUGLEVEL;
159159

160160
#include <sys/types.h> /* For size_t */
161161
#include "auth-spa.h"
162+
#include "../store.h"
162163
#include <assert.h>
163164
#include <ctype.h>
164165
#include <stdio.h>
@@ -1401,7 +1402,7 @@ spa_build_auth_request (SPAAuthRequest * request, char *user, char *domain)
14011402
SIVAL (&request->flags, 0, 0x0000b207); /* have to figure out what these mean */
14021403
spa_string_add (request, user, u);
14031404
spa_string_add (request, domain, domain);
1404-
free (u);
1405+
store_free (u);
14051406
}
14061407

14071408

@@ -1483,8 +1484,8 @@ spa_build_auth_response (SPAAuthChallenge * challenge,
14831484

14841485
response->flags = challenge->flags;
14851486

1486-
free (d);
1487-
free (u);
1487+
store_free (d);
1488+
store_free (u);
14881489
}
14891490
#endif
14901491

@@ -1537,6 +1538,6 @@ spa_build_auth_response (SPAAuthChallenge * challenge,
15371538
spa_string_add (response, sessionKey, NULL);
15381539
response->flags = challenge->flags;
15391540

1540-
if (d != NULL) free (d);
1541-
free (u);
1541+
if (d != NULL) store_free (d);
1542+
store_free (u);
15421543
}

src/src/auths/call_pam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ for (i = 0; i < num_msg; i++)
100100
break;
101101

102102
default: /* Must be an error of some sort... */
103-
free (reply);
103+
store_free (reply);
104104
pam_conv_had_error = TRUE;
105105
return PAM_CONV_ERR;
106106
}

src/src/auths/gsasl_exim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ auth_gsasl_server(auth_instance *ablock, uschar *initial_data)
358358
auth_get_no64_data((uschar **)&received, (uschar *)to_send);
359359

360360
if (to_send) {
361-
free(to_send);
361+
store_free(to_send);
362362
to_send = NULL;
363363
}
364364

src/src/auths/heimdal_gssapi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ auth_heimdal_gssapi_init(auth_instance *ablock)
160160
principal ? principal : "??",
161161
entry.vno,
162162
enctype_s ? enctype_s : "??");
163-
free(principal);
164-
free(enctype_s);
163+
store_free(principal);
164+
store_free(enctype_s);
165165
krb5_kt_free_entry(context, &entry);
166166
}
167167
krc = krb5_kt_end_seq_get(context, keytab, &cursor);

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

0 commit comments

Comments
 (0)