From patchwork Sat Jun 4 08:19:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergei Trofimovich X-Patchwork-Id: 849242 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p549U5CR002398 for ; Sat, 4 Jun 2011 09:30:11 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753769Ab1FDIRV (ORCPT ); Sat, 4 Jun 2011 04:17:21 -0400 Received: from smtp.gentoo.org ([140.211.166.183]:49503 "EHLO smtp.gentoo.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753539Ab1FDIRU (ORCPT ); Sat, 4 Jun 2011 04:17:20 -0400 Received: from gentoo.org (unknown [178.125.218.26]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: slyfox) by smtp.gentoo.org (Postfix) with ESMTPSA id B26FE1B402F; Sat, 4 Jun 2011 08:17:17 +0000 (UTC) Received: by gentoo.org (sSMTP sendmail emulation); Sat, 04 Jun 2011 11:20:13 +0300 From: Sergei Trofimovich To: Chris Mason Cc: linux-btrfs@vger.kernel.org, Sergei Trofimovich Subject: [PATCH v2 5/9] mkfs.btrfs: fix symlink names writing Date: Sat, 4 Jun 2011 11:19:20 +0300 Message-Id: <1307175564-25355-6-git-send-email-slyfox@gentoo.org> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: <1307175564-25355-1-git-send-email-slyfox@gentoo.org> References: <1307175564-25355-1-git-send-email-slyfox@gentoo.org> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Sat, 04 Jun 2011 09:30:11 +0000 (UTC) Found by valgrind: ==8968== Use of uninitialised value of size 8 ==8968== at 0x41CE7D: crc32c_le (crc32c.c:98) ==8968== by 0x40A1D0: csum_tree_block_size (disk-io.c:82) ==8968== by 0x40A2D4: csum_tree_block (disk-io.c:105) ==8968== by 0x40A7D6: write_tree_block (disk-io.c:241) ==8968== by 0x40ACEE: __commit_transaction (disk-io.c:354) ==8968== by 0x40AE9E: btrfs_commit_transaction (disk-io.c:385) ==8968== by 0x42CF66: make_image (mkfs.c:1061) ==8968== by 0x42DE63: main (mkfs.c:1410) ==8968== Uninitialised value was created by a stack allocation ==8968== at 0x42B5FB: add_inode_items (mkfs.c:493) readlink(2) does not write '\0' for us, so make it manually. Signed-off-by: Sergei Trofimovich --- mkfs.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-) diff --git a/mkfs.c b/mkfs.c index 9d7b792..8ff2b1e 100644 --- a/mkfs.c +++ b/mkfs.c @@ -692,45 +692,47 @@ static int record_file_extent(struct btrfs_trans_handle *trans, root->root_key.objectid, objectid, 0); fail: btrfs_release_path(root, &path); return ret; } static int add_symbolic_link(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 objectid, const char *path_name) { int ret; u64 sectorsize = root->sectorsize; char *buf = malloc(sectorsize); ret = readlink(path_name, buf, sectorsize); if (ret <= 0) { fprintf(stderr, "readlink failed for %s\n", path_name); goto fail; } - if (ret > sectorsize) { + if (ret >= sectorsize) { fprintf(stderr, "symlink too long for %s", path_name); ret = -1; goto fail; } + + buf[ret] = '\0'; /* readlink does not do it for us */ ret = btrfs_insert_inline_extent(trans, root, objectid, 0, buf, ret + 1); fail: free(buf); return ret; } static int add_file_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_inode_item *btrfs_inode, u64 objectid, ino_t parent_inum, struct stat *st, const char *path_name, int out_fd) { int ret; ssize_t ret_read; u64 bytes_read = 0; char *buffer = NULL; struct btrfs_key key; int blocks; u32 sectorsize = root->sectorsize;