From: Alexander Kuleshov Date: Tue, 8 Dec 2015 18:38:12 +0000 (+0600) Subject: boot0cfg: check result of malloc in read_mbr() X-Git-Tag: v4.6.0rc~1126 X-Git-Url: https://gitweb.dragonflybsd.org/~tuxillo/dragonfly.git/commitdiff_plain/5f115e3b8fa054dfb6e59af424b0946871ca6cc0 boot0cfg: check result of malloc in read_mbr() We allocating buffer for MBR in the read_mbr() function. The malloc() may return NULL, so this patch checks the result of the malloc and exit with the error if we can't allocate memory. Besides this we close file descriptor. --- diff --git a/usr.sbin/boot0cfg/boot0cfg.c b/usr.sbin/boot0cfg/boot0cfg.c index 53e7e95ea5..4694f17f03 100644 --- a/usr.sbin/boot0cfg/boot0cfg.c +++ b/usr.sbin/boot0cfg/boot0cfg.c @@ -24,7 +24,6 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD: src/usr.sbin/boot0cfg/boot0cfg.c,v 1.7.2.4 2002/03/16 01:06:51 mikeh Exp $ - * $DragonFly: src/usr.sbin/boot0cfg/boot0cfg.c,v 1.6 2008/03/24 23:04:19 swildner Exp $ */ #include @@ -238,11 +237,14 @@ read_mbr(const char *disk, u_int8_t **mbr, int check_version) err(1, "%s", disk); if (n != mbr_size) errx(1, "%s: short read", disk); + close(fd); return (mbr_size); } - *mbr = malloc(sizeof(buf)); - memcpy(*mbr, buf, sizeof(buf)); + if ((*mbr = malloc(sizeof(buf))) == NULL) + errx(1, "%s: unable to allocate mbr buffer", disk); + memcpy(*mbr, buf, sizeof(buf)); + close(fd); return sizeof(buf); }