Skip to content

Commit 84ddc4c

Browse files
committed
Merge pull request #155 from acv/malloc
Enhance safe_malloc to always zero memory
2 parents b2966e8 + eaedf3b commit 84ddc4c

File tree

5 files changed

+69
-68
lines changed

5 files changed

+69
-68
lines changed

src/client_list.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ client_get_new(void)
6161
{
6262
t_client *client;
6363
client = safe_malloc(sizeof(t_client));
64-
memset(client, 0, sizeof(t_client));
6564
return client;
6665
}
6766

src/conf.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ parse_auth_server(FILE * file, const char *filename, int *linenum)
359359
new = safe_malloc(sizeof(t_auth_serv));
360360

361361
/* Fill in struct */
362-
memset(new, 0, sizeof(t_auth_serv)); /*< Fill all with NULL */
363362
new->authserv_hostname = host;
364363
new->authserv_use_ssl = ssl_available;
365364
new->authserv_path = path;
@@ -556,7 +555,6 @@ _parse_firewall_rule(const char *ruleset, char *leftover)
556555
}
557556
/* Generate rule record */
558557
tmp = safe_malloc(sizeof(t_firewall_rule));
559-
memset((void *)tmp, 0, sizeof(t_firewall_rule));
560558
tmp->target = target;
561559
tmp->mask_is_ipset = mask_is_ipset;
562560
if (protocol != NULL)
@@ -573,7 +571,6 @@ _parse_firewall_rule(const char *ruleset, char *leftover)
573571
/* Append the rule record */
574572
if (config.rulesets == NULL) {
575573
config.rulesets = safe_malloc(sizeof(t_firewall_ruleset));
576-
memset(config.rulesets, 0, sizeof(t_firewall_ruleset));
577574
config.rulesets->name = safe_strdup(ruleset);
578575
tmpr = config.rulesets;
579576
} else {
@@ -585,7 +582,6 @@ _parse_firewall_rule(const char *ruleset, char *leftover)
585582
if (tmpr == NULL) {
586583
/* Rule did not exist */
587584
tmpr = safe_malloc(sizeof(t_firewall_ruleset));
588-
memset(tmpr, 0, sizeof(t_firewall_ruleset));
589585
tmpr->name = safe_strdup(ruleset);
590586
tmpr2->next = tmpr;
591587
}

src/pstring.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pstr_new(void)
4747
new->len = 0;
4848
new->size = MAX_BUF;
4949
new->buf = (char *)safe_malloc(MAX_BUF);
50-
memset(new->buf, 0, MAX_BUF);
5150

5251
return new;
5352
}

src/safe.c

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
/* Enable vasprintf */
3232
#define _GNU_SOURCE
3333

34-
3534
#include <stdarg.h>
3635
#include <stdio.h>
3736
#include <stdlib.h>
@@ -44,16 +43,19 @@
4443
#include <syslog.h>
4544

4645
/* From gateway.c */
47-
extern httpd * webserver;
48-
49-
void * safe_malloc (size_t size) {
50-
void * retval = NULL;
51-
retval = malloc(size);
52-
if (!retval) {
53-
debug(LOG_CRIT, "Failed to malloc %d bytes of memory: %s. Bailing out", size, strerror(errno));
54-
exit(1);
55-
}
56-
return (retval);
46+
extern httpd *webserver;
47+
48+
void *
49+
safe_malloc(size_t size)
50+
{
51+
void *retval = NULL;
52+
retval = malloc(size);
53+
if (!retval) {
54+
debug(LOG_CRIT, "Failed to malloc %d bytes of memory: %s. Bailing out", size, strerror(errno));
55+
exit(1);
56+
}
57+
memset(retval, 0, size);
58+
return (retval);
5759
}
5860

5961
void *
@@ -68,59 +70,65 @@ safe_realloc(void *ptr, size_t newsize)
6870
return retval;
6971
}
7072

71-
char * safe_strdup(const char *s) {
72-
char * retval = NULL;
73-
if (!s) {
74-
debug(LOG_CRIT, "safe_strdup called with NULL which would have crashed strdup. Bailing out");
75-
exit(1);
76-
}
77-
retval = strdup(s);
78-
if (!retval) {
79-
debug(LOG_CRIT, "Failed to duplicate a string: %s. Bailing out", strerror(errno));
80-
exit(1);
81-
}
82-
return (retval);
73+
char *
74+
safe_strdup(const char *s)
75+
{
76+
char *retval = NULL;
77+
if (!s) {
78+
debug(LOG_CRIT, "safe_strdup called with NULL which would have crashed strdup. Bailing out");
79+
exit(1);
80+
}
81+
retval = strdup(s);
82+
if (!retval) {
83+
debug(LOG_CRIT, "Failed to duplicate a string: %s. Bailing out", strerror(errno));
84+
exit(1);
85+
}
86+
return (retval);
8387
}
8488

85-
int safe_asprintf(char **strp, const char *fmt, ...) {
86-
va_list ap;
87-
int retval;
89+
int
90+
safe_asprintf(char **strp, const char *fmt, ...)
91+
{
92+
va_list ap;
93+
int retval;
8894

89-
va_start(ap, fmt);
90-
retval = safe_vasprintf(strp, fmt, ap);
91-
va_end(ap);
95+
va_start(ap, fmt);
96+
retval = safe_vasprintf(strp, fmt, ap);
97+
va_end(ap);
9298

93-
return (retval);
99+
return (retval);
94100
}
95101

96-
int safe_vasprintf(char **strp, const char *fmt, va_list ap) {
97-
int retval;
102+
int
103+
safe_vasprintf(char **strp, const char *fmt, va_list ap)
104+
{
105+
int retval;
98106

99-
retval = vasprintf(strp, fmt, ap);
107+
retval = vasprintf(strp, fmt, ap);
100108

101-
if (retval == -1) {
102-
debug(LOG_CRIT, "Failed to vasprintf: %s. Bailing out", strerror(errno));
103-
exit (1);
104-
}
105-
return (retval);
109+
if (retval == -1) {
110+
debug(LOG_CRIT, "Failed to vasprintf: %s. Bailing out", strerror(errno));
111+
exit(1);
112+
}
113+
return (retval);
106114
}
107115

108-
pid_t safe_fork(void) {
109-
pid_t result;
110-
result = fork();
111-
112-
if (result == -1) {
113-
debug(LOG_CRIT, "Failed to fork: %s. Bailing out", strerror(errno));
114-
exit (1);
115-
}
116-
else if (result == 0) {
117-
/* I'm the child - do some cleanup */
118-
if (webserver) {
119-
close(webserver->serverSock);
120-
webserver = NULL;
121-
}
122-
}
123-
124-
return result;
125-
}
116+
pid_t
117+
safe_fork(void)
118+
{
119+
pid_t result;
120+
result = fork();
126121

122+
if (result == -1) {
123+
debug(LOG_CRIT, "Failed to fork: %s. Bailing out", strerror(errno));
124+
exit(1);
125+
} else if (result == 0) {
126+
/* I'm the child - do some cleanup */
127+
if (webserver) {
128+
close(webserver->serverSock);
129+
webserver = NULL;
130+
}
131+
}
132+
133+
return result;
134+
}

src/safe.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#ifndef _SAFE_H_
2929
#define _SAFE_H_
3030

31-
#include <stdarg.h> /* For va_list */
32-
#include <sys/types.h> /* For fork */
33-
#include <unistd.h> /* For fork */
31+
#include <stdarg.h> /* For va_list */
32+
#include <sys/types.h> /* For fork */
33+
#include <unistd.h> /* For fork */
3434

3535
/** @brief Safe version of malloc
3636
*/
@@ -41,7 +41,7 @@ void *safe_realloc(void *, size_t);
4141

4242
/* @brief Safe version of strdup
4343
*/
44-
char * safe_strdup(const char *);
44+
char *safe_strdup(const char *);
4545

4646
/* @brief Safe version of asprintf
4747
*/
@@ -56,5 +56,4 @@ int safe_vasprintf(char **, const char *, va_list);
5656

5757
pid_t safe_fork(void);
5858

59-
#endif /* _SAFE_H_ */
60-
59+
#endif /* _SAFE_H_ */

0 commit comments

Comments
 (0)