From patchwork Fri Jul 15 09:21:45 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurence Withers X-Patchwork-Id: 104775 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id C17E0B6F65 for ; Fri, 15 Jul 2011 19:22:00 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7F891280ED; Fri, 15 Jul 2011 11:21:59 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id iESGTjLULK5G; Fri, 15 Jul 2011 11:21:59 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6C74E280E2; Fri, 15 Jul 2011 11:21:57 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 88C9A280E9 for ; Fri, 15 Jul 2011 11:21:55 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id daPAv18jp0FO for ; Fri, 15 Jul 2011 11:21:54 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from chrysocolla.lwithers.me.uk (chrysocolla.lwithers.me.uk [80.68.94.184]) by theia.denx.de (Postfix) with ESMTPS id DF5CE280E2 for ; Fri, 15 Jul 2011 11:21:54 +0200 (CEST) Received: from 4.b.0.2.4.9.e.f.f.f.5.6.f.6.e.1.0.0.0.0.5.a.1.0.0.b.8.0.1.0.0.2.ip6.arpa ([2001:8b0:1a5:0:1e6f:65ff:fe94:20b4] helo=rhodium.platinum.guralp.com) by chrysocolla.lwithers.me.uk with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1Qheao-0007bR-BR; Fri, 15 Jul 2011 09:21:54 +0000 Received: from lwithers by rhodium.platinum.guralp.com with local (Exim 4.72) (envelope-from ) id 1Qheao-0002PZ-0n; Fri, 15 Jul 2011 09:21:54 +0000 From: Laurence Withers To: u-boot@lists.denx.de Date: Fri, 15 Jul 2011 09:21:45 +0000 Message-Id: <1310721705-9240-1-git-send-email-lwithers@guralp.com> X-Mailer: git-send-email 1.7.2.5 Cc: Andy Fleming Subject: [U-Boot] [PATCH] miiphy: use strncpy() not sprintf() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de In miiphy_register() the new device's name was initialised by passing a string parameter as the format string to sprintf(). As this would cause problems if it ever contained a '%' symbol, switch to using strncpy() instead. Signed-off-by: Laurence Withers Cc: Andy Fleming --- Changes for v2: - Use strncpy() rather than plain strcpy() for extra safety. Changes for v3: - Use BUG_ON() as an additional safety measure to ensure the name never exceeds the buffer size MDIO_NAME_LEN, simplifying the previous test. --- common/miiphyutil.c | 14 ++++---------- 1 files changed, 4 insertions(+), 10 deletions(-) diff --git a/common/miiphyutil.c b/common/miiphyutil.c index bcab74e..35ad357 100644 --- a/common/miiphyutil.c +++ b/common/miiphyutil.c @@ -111,7 +111,8 @@ void miiphy_register(const char *name, { struct mii_dev *new_dev; struct legacy_mii_dev *ldev; - unsigned int name_len; + + BUG_ON(strlen(name) >= MDIO_NAME_LEN); /* check if we have unique name */ new_dev = miiphy_get_dev_by_name(name); @@ -121,14 +122,6 @@ void miiphy_register(const char *name, } /* allocate memory */ - name_len = strlen(name); - if (name_len > MDIO_NAME_LEN - 1) { - /* Hopefully this won't happen, but if it does, we'll know */ - printf("miiphy_register: MDIO name was longer than %d\n", - MDIO_NAME_LEN); - return; - } - new_dev = mdio_alloc(); ldev = malloc(sizeof(*ldev)); @@ -141,7 +134,8 @@ void miiphy_register(const char *name, /* initalize mii_dev struct fields */ new_dev->read = legacy_miiphy_read; new_dev->write = legacy_miiphy_write; - sprintf(new_dev->name, name); + strncpy(new_dev->name, name, MDIO_NAME_LEN); + new_dev->name[MDIO_NAME_LEN - 1] = 0; ldev->read = read; ldev->write = write; new_dev->priv = ldev;