From mboxrd@z Thu Jan 1 00:00:00 1970 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 942D5C433F5 for ; Fri, 18 Feb 2022 11:24:35 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2B3EB838AF; Fri, 18 Feb 2022 12:24:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="fDUXqj+W"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 451AD838A4; Fri, 18 Feb 2022 12:24:30 +0100 (CET) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (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 62E24838AF for ; Fri, 18 Feb 2022 12:24:26 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B154861EA8; Fri, 18 Feb 2022 11:24:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05FF8C340E9; Fri, 18 Feb 2022 11:24:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1645183464; bh=5qgJ6Ag6Vnli9or+WP9xc8ZWCGrIDSwHIkIeIb9vmlk=; h=From:To:Cc:Subject:Date:From; b=fDUXqj+WitSI5d/6Bc7lhArLAjsdlISE4jSltISEicWnxLQUmx3+R1h6YTBUDLFnY JxGq8CTQJnKr8cAVAMJR+pYreQyJnHpWTiQJ5Du8796GOCa60veEWjwpSCHDKQ/gkW ckd7UqB/ItmiNJEZb5yXGgNncs9h+RopUF9yB91ZqnrvVw+R9aldcEntAYHIi9yTbf P98qbQvQFqroxcBFtyx+Z1w7+VKlG62ZLB9AgosQ5kzOUd5ex/B3amZ1pOXWVnQeW0 5ebSihgGZq0Bk++vXUGF+xjtwfMu4BVXiqlgEQE6913BNOvGi0zZ/iFbETzNsMcDST /PRLjJm3Ra8jw== Received: by pali.im (Postfix) id F19942BAE; Fri, 18 Feb 2022 12:24:20 +0100 (CET) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: Stefan Roese , =?UTF-8?q?Marek=20Beh=C3=BAn?= Cc: u-boot@lists.denx.de Subject: [PATCH] tools: kwboot: Fix quitting terminal Date: Fri, 18 Feb 2022 12:24:13 +0100 Message-Id: <20220218112413.10009-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Sometimes kwboot after quitting terminal prints error message: terminal: Bad address This is caused by trying to call write() syscall with count of (size_t)-1 bytes. When quit sequence is split into more read() calls then number of input bytes (nin) at the end of cycle can underflow and be negative. Fix it. Fixes: de7514046ea5 ("tools: kwboot: Fix detection of quit esc sequence") Signed-off-by: Pali Rohár --- tools/kwboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/kwboot.c b/tools/kwboot.c index 68c0ef1f1b07..2d2d545d8258 100644 --- a/tools/kwboot.c +++ b/tools/kwboot.c @@ -1197,7 +1197,7 @@ kwboot_term_pipe(int in, int out, const char *quit, int *s) if (buf[i] == quit[*s]) { (*s)++; if (!quit[*s]) { - nin = i - *s; + nin = (i > *s) ? (i - *s) : 0; break; } } else { @@ -1208,7 +1208,7 @@ kwboot_term_pipe(int in, int out, const char *quit, int *s) } if (i == nin) - nin -= *s; + nin -= (nin > *s) ? *s : nin; } if (kwboot_write(out, buf, nin) < 0) -- 2.20.1