From owner-svn-src-user@FreeBSD.ORG Thu May 15 14:48:26 2014 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 01406729; Thu, 15 May 2014 14:48:26 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E33012F78; Thu, 15 May 2014 14:48:25 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s4FEmPrZ030676; Thu, 15 May 2014 14:48:25 GMT (envelope-from marcel@svn.freebsd.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s4FEmPsk030675; Thu, 15 May 2014 14:48:25 GMT (envelope-from marcel@svn.freebsd.org) Message-Id: <201405151448.s4FEmPsk030675@svn.freebsd.org> From: Marcel Moolenaar Date: Thu, 15 May 2014 14:48:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r266132 - user/marcel/mkimg X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 May 2014 14:48:26 -0000 Author: marcel Date: Thu May 15 14:48:25 2014 New Revision: 266132 URL: http://svnweb.freebsd.org/changeset/base/266132 Log: Replace unchecked calls to write(2) and lseek(2) with calls to sparse_write() and make sure to check for errors. In particular, lseek(2) may not be possible (e.g. the output file is stdout) and sparse_write(2) will do what is optimal (i.e. use lseek(2) when possible). Modified: user/marcel/mkimg/vmdk.c Modified: user/marcel/mkimg/vmdk.c ============================================================================== --- user/marcel/mkimg/vmdk.c Thu May 15 14:44:16 2014 (r266131) +++ user/marcel/mkimg/vmdk.c Thu May 15 14:48:25 2014 (r266132) @@ -112,7 +112,8 @@ vmdk_write(int fd) { struct vmdk_header hdr; uint32_t *gt, *gd; - char *desc; + char *buf, *desc; + off_t cur, lim; uint64_t imagesz; size_t gdsz, gtsz; uint32_t sec; @@ -181,17 +182,37 @@ vmdk_write(int fd) for (n = 0; n < ngrains; n++) le32enc(gt + n, sec + n * grainsz); - write(fd, &hdr, VMDK_SECTOR_SIZE); - write(fd, desc, desc_len); - write(fd, gd, gdsz); - write(fd, gt, gtsz); - lseek(fd, sec * VMDK_SECTOR_SIZE, SEEK_SET); - error = image_copyout(fd); - + error = 0; + if (!error && sparse_write(fd, &hdr, VMDK_SECTOR_SIZE) < 0) + error = errno; + if (!error && sparse_write(fd, desc, desc_len) < 0) + error = errno; + if (!error && sparse_write(fd, gd, gdsz) < 0) + error = errno; + if (!error && sparse_write(fd, gt, gtsz) < 0) + error = errno; free(gt); free(gd); free(desc); + if (error) + return (error); + cur = VMDK_SECTOR_SIZE + desc_len + gdsz + gtsz; + lim = sec * VMDK_SECTOR_SIZE; + if (cur < lim) { + buf = calloc(VMDK_SECTOR_SIZE, 1); + if (buf == NULL) + error = ENOMEM; + while (!error && cur < lim) { + if (sparse_write(fd, buf, VMDK_SECTOR_SIZE) < 0) + error = errno; + cur += VMDK_SECTOR_SIZE; + } + if (buf != NULL) + free(buf); + } + if (!error) + error = image_copyout(fd); return (error); }