From 0d018826fca6c96d4897c512c35b47cbd8edc690 Mon Sep 17 00:00:00 2001 From: Alexander Kuleshov Date: Wed, 9 Dec 2015 00:38:12 +0600 Subject: [PATCH] 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. --- usr.sbin/boot0cfg/boot0cfg.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/usr.sbin/boot0cfg/boot0cfg.c b/usr.sbin/boot0cfg/boot0cfg.c index 53e7e95..fd0b928 100644 --- a/usr.sbin/boot0cfg/boot0cfg.c +++ b/usr.sbin/boot0cfg/boot0cfg.c @@ -238,11 +238,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); } -- 2.5.0