From patchwork Mon Aug 6 18:03:42 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1280641 Return-Path: X-Original-To: patchwork-ceph-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 1CDCADF288 for ; Mon, 6 Aug 2012 18:03:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756837Ab2HFSDp (ORCPT ); Mon, 6 Aug 2012 14:03:45 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:39424 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753685Ab2HFSDo (ORCPT ); Mon, 6 Aug 2012 14:03:44 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr13so2904653pbb.19 for ; Mon, 06 Aug 2012 11:03:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=4C3Vvw9a0NEFu8iRbE3YoM7QbVHxpQ3Qu0QK/bpLVh0=; b=gshJTOIobicyYSBSCJfhPA4c/23/Tig5+mjFS5FwqVih/ZYuaPNFovaPT0QT8wF5e/ 4PqpWidNJ703O8OiuzMRX++v5elHgu99AMXa/oRILqQlIIRPog3f63/Fj1rOHMBMm8fv rUVapZNO6G8KM8+4PC9qirAMrgF8Nd3LzaX17yyJ1GCuxjGMDENRY3aYDV5e/KRUX5tw IJsOSj08DytLOEL0/cJUCHA3n7y6FhXdkKCbimJct26fJugdTlMihA/6AX76GEdNoHSM HezAqFd6GAtT8AbNYAttwPQQG0ByQKsZm2QXTWi7zqwKsnv7spul7MXcxZUDPe1q9/Xg wILQ== Received: by 10.68.234.98 with SMTP id ud2mr7529459pbc.165.1344276224342; Mon, 06 Aug 2012 11:03:44 -0700 (PDT) Received: from ?IPv6:2607:f298:a:607:3c9c:52cb:843e:71a9? ([2607:f298:a:607:3c9c:52cb:843e:71a9]) by mx.google.com with ESMTPS id gh9sm9382269pbc.20.2012.08.06.11.03.43 (version=SSLv3 cipher=OTHER); Mon, 06 Aug 2012 11:03:43 -0700 (PDT) Message-ID: <502006FE.1080406@inktank.com> Date: Mon, 06 Aug 2012 11:03:42 -0700 From: Alex Elder User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: "ceph-devel@vger.kernel.org" Subject: [PATCH 3/3] rbd: use sizeof (object) instead of sizeof (type) References: <50200691.3040006@inktank.com> In-Reply-To: <50200691.3040006@inktank.com> X-Gm-Message-State: ALoCoQnVcgP7E+tZqtYnvJTw7+tnK0VtWxtbVZM8gnAKJ2LXt4QXm4cbk9AMc/5O1aU06ynytW5e Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org Fix a few spots in rbd_header_from_disk() to use sizeof (object) rather than sizeof (type). Use a local variable to record sizes to shorten some lines and improve readability. Signed-off-by: Alex Elder --- drivers/block/rbd.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: b/drivers/block/rbd.c =================================================================== --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -495,17 +495,21 @@ static int rbd_header_from_disk(struct r u32 allocated_snaps) { u32 snap_count; + size_t size; if (!rbd_dev_ondisk_valid(ondisk)) return -ENXIO; snap_count = le32_to_cpu(ondisk->snap_count); - if (snap_count > (SIZE_MAX - sizeof(struct ceph_snap_context)) - / sizeof (u64)) + + /* Make sure we don't overflow below */ + size = SIZE_MAX - sizeof (struct ceph_snap_context); + if (snap_count > size / sizeof (header->snapc->snaps[0])) return -EINVAL; - header->snapc = kmalloc(sizeof(struct ceph_snap_context) + - snap_count * sizeof(u64), - GFP_KERNEL); + + size = sizeof (struct ceph_snap_context); + size += snap_count * sizeof (header->snapc->snaps[0]); + header->snapc = kmalloc(size, GFP_KERNEL); if (!header->snapc) return -ENOMEM; @@ -516,8 +520,8 @@ static int rbd_header_from_disk(struct r GFP_KERNEL); if (!header->snap_names) goto err_snapc; - header->snap_sizes = kmalloc(snap_count * sizeof(u64), - GFP_KERNEL); + size = snap_count * sizeof (*header->snap_sizes); + header->snap_sizes = kmalloc(size, GFP_KERNEL); if (!header->snap_sizes) goto err_names; } else { @@ -527,14 +531,12 @@ static int rbd_header_from_disk(struct r header->snap_sizes = NULL; } - header->object_prefix = kmalloc(sizeof (ondisk->block_name) + 1, - GFP_KERNEL); + size = sizeof (ondisk->block_name) + 1; + header->object_prefix = kmalloc(size, GFP_KERNEL); if (!header->object_prefix) goto err_sizes; - - memcpy(header->object_prefix, ondisk->block_name, - sizeof(ondisk->block_name)); - header->object_prefix[sizeof (ondisk->block_name)] = '\0'; + memcpy(header->object_prefix, ondisk->block_name, size - 1); + header->object_prefix[size - 1] = '\0'; header->image_size = le64_to_cpu(ondisk->image_size); header->obj_order = ondisk->options.order;