Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DC9DCC433F5 for ; Thu, 31 Mar 2022 12:41:57 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 56AB0841F2; Thu, 31 Mar 2022 14:41:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 8E6F9841DF; Thu, 31 Mar 2022 14:41:52 +0200 (CEST) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 793B5841DF for ; Thu, 31 Mar 2022 14:41:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=andy.shevchenko@gmail.com X-IronPort-AV: E=McAfee;i="6200,9189,10302"; a="259536018" X-IronPort-AV: E=Sophos;i="5.90,225,1643702400"; d="scan'208";a="259536018" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2022 05:41:41 -0700 X-IronPort-AV: E=Sophos;i="5.90,225,1643702400"; d="scan'208";a="555068515" Received: from smile.fi.intel.com ([10.237.72.59]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2022 05:41:40 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.95) (envelope-from ) id 1nZu6t-009zBZ-Sx; Thu, 31 Mar 2022 15:41:07 +0300 Date: Thu, 31 Mar 2022 15:41:07 +0300 From: Andy Shevchenko To: Sean Anderson Cc: U-Boot Mailing List , Simon Glass Subject: Re: [PATCH] IOMUX: Fix access past end of console_devices Message-ID: References: <20220330164902.431732-1-seanga2@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean On Wed, Mar 30, 2022 at 01:25:59PM -0400, Sean Anderson wrote: > On 3/30/22 1:13 PM, Andy Shevchenko wrote: > > On Wed, Mar 30, 2022 at 7:49 PM Sean Anderson wrote: > > > > Also I don't like to have workarounds for the broken tools. > > But if you still want to have something, what about rather this > > > > > #define for_each_console_dev(i, file, dev) \ > > > - for (i = 0, dev = console_devices[file][i]; \ > > > - i < cd_count[file]; \ > > > - i++, dev = console_devices[file][i]) > > > + for (i = 0; i < cd_count[file] && \ > > > + (dev = console_devices[file][i]); i++) > > > > for (i = 0, dev = console_devices[file][0]; \ > > i < cd_count[file]; \ > > i++, dev = console_devices[file][i]) > > > > ? > > > > Or if it's still complains > > > > for (i = 0, dev = cd_count[file] ? console_devices[file][0] : NULL; \ > > i < cd_count[file]; \ > > i++, dev = console_devices[file][i]) > > > > ? > > The problem is not the first assignment but the last. Consider the case when cd_count[file] = 1 Following that logic the first one is also problematic when cd_count[file] == 0. > i = 0, dev = console_devices[file][0]; // OK > i < cd_count[file] // 0 < 1 > // loop body > i++, dev = console_devices[file][1] // Oops, past the end > i < cd_count[file] // 1 < 1, loop exit I don't see good solution. :-( Maybe moving to dev as a loop variable and having NULL terminated arrays should fix that. For now probably your solution is a good compromise. But, please rewrite it to have all three more visible in a for-loop: for (i = 0; \ i < cd_count[file] && (dev = console_devices[file][i]); \ i++) -- With Best Regards, Andy Shevchenko