summaryrefslogtreecommitdiffstats
path: root/usr.sbin/installboot/util.c
diff options
context:
space:
mode:
authorjsg <jsg@openbsd.org>2015-11-04 02:12:49 +0000
committerjsg <jsg@openbsd.org>2015-11-04 02:12:49 +0000
commit43aa4f76dd09736cfa883290d7f924a3430dc749 (patch)
treed1cad5b985d8465775c998db2127fe42fe176734 /usr.sbin/installboot/util.c
parentuse mq_purge to free a queue of mbufs. (diff)
downloadwireguard-openbsd-43aa4f76dd09736cfa883290d7f924a3430dc749.tar.xz
wireguard-openbsd-43aa4f76dd09736cfa883290d7f924a3430dc749.zip
fix a memory leak in multiple error paths
ok krw@
Diffstat (limited to 'usr.sbin/installboot/util.c')
-rw-r--r--usr.sbin/installboot/util.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/usr.sbin/installboot/util.c b/usr.sbin/installboot/util.c
index a31eeac73e4..ddb890a85c6 100644
--- a/usr.sbin/installboot/util.c
+++ b/usr.sbin/installboot/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.10 2015/10/19 19:05:24 krw Exp $ */
+/* $OpenBSD: util.c,v 1.11 2015/11/04 02:12:49 jsg Exp $ */
/*
* Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
@@ -40,11 +40,6 @@ filecopy(const char *srcfile, const char *dstfile)
int sfd, dfd;
char *buf;
- if ((buf = malloc(BUFSIZE)) == NULL) {
- warn("malloc");
- return (-1);
- }
-
sfd = open(srcfile, O_RDONLY);
if (sfd == -1) {
warn("open %s", srcfile);
@@ -71,15 +66,22 @@ filecopy(const char *srcfile, const char *dstfile)
return (-1);
}
+ if ((buf = malloc(BUFSIZE)) == NULL) {
+ warn("malloc");
+ return (-1);
+ }
+
while (sz > 0) {
n = MINIMUM(sz, BUFSIZE);
if ((n = read(sfd, buf, n)) == -1) {
warn("read");
+ free(buf);
return (-1);
}
sz -= n;
if (write(dfd, buf, n) != n) {
warn("write");
+ free(buf);
return (-1);
}
}