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 A8814C61DA4 for ; Sun, 5 Feb 2023 02:09:29 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3134985790; Sun, 5 Feb 2023 03:09:18 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="m1WTnKu6"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2A0CB858D3; Sun, 5 Feb 2023 03:09:14 +0100 (CET) Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 50D1D858CC for ; Sun, 5 Feb 2023 03:09:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd2b.google.com with SMTP id w24so3350583iow.13 for ; Sat, 04 Feb 2023 18:09:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4KpdCMCYpVZsQajb5WnfdMP4EJ63obbsw9XnbGRKDTE=; b=m1WTnKu62oYKvERNq3bTnEy0rtXgta8XowUz76uawyZL+6OD5cPko1UjUDTxJLDV0E 8reN15J71YvCev/wVC/YOEdj6gOQUuNNlYV877iajP+kCcEtaMSzcyjVWe2g/AFob3DH CIDODz9YqHDMCSTzkgaKcQfOKXx2GyhX5aTks= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4KpdCMCYpVZsQajb5WnfdMP4EJ63obbsw9XnbGRKDTE=; b=eVsBkLEIww2FWB9eGHrvJIZ1xi0/OdukDXbx3+C5Co1mGQ/14s2+0JCIbcFeNw9/46 8p8V6gphBuOSGT3LWfi5rI/q2A3uMNX0Q1M/ADYKoI6yQ9AU9/Z4Mg/vWk0yyd0dBLBA I3ZqjxzinupD3aVresJsZYVVO0I6xFg78qFMQHET5JWBU7LAT46w07GMdkrFTOuM8bi8 tCKlA1WTnCE9kIwJ4FxHr6rZnox5MkvNMfZmglpUJZSM3U/KIj2iDjEYU3QPbeJgBOQ4 MqIsv0w4X7VRIShNRiBWccTIRhJNK14gVGolRnBb9JDLUXPaMjr8Mr1EwAarx6CA7LTu 5Tqg== X-Gm-Message-State: AO0yUKUApjLxqJEh/UtgeO+596z6MkTlM5nLVbqEHppkPzyOlihdDrUW mxoPSiS+2O/FeTxYHquJ5QGz5gPbVq1SHihG X-Google-Smtp-Source: AK7set/oVh3WSOuKJ/DpZmoFgJQlaHHG01H05LX0mqINcZGZELeQrZ2AN92qceoDmQ0lxQjsaiQcLQ== X-Received: by 2002:a5d:9807:0:b0:71b:cd72:192d with SMTP id a7-20020a5d9807000000b0071bcd72192dmr9764053iol.20.1675562939596; Sat, 04 Feb 2023 18:08:59 -0800 (PST) Received: from sjg1.lan (c-73-14-173-85.hsd1.co.comcast.net. [73.14.173.85]) by smtp.gmail.com with ESMTPSA id s10-20020a02ad0a000000b003a2e9dda8e9sm2137917jan.132.2023.02.04.18.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 18:08:59 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Heinrich Schuchardt , Simon Glass , =?UTF-8?q?Pali=20Roh=C3=A1r?= , Sean Anderson , Stefan Roese Subject: [PATCH v2 2/2] cli: Correct handling of invalid escape sequences in cread_line() Date: Sat, 4 Feb 2023 19:08:55 -0700 Message-Id: <20230205020855.146348-2-sjg@chromium.org> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog In-Reply-To: <20230205020855.146348-1-sjg@chromium.org> References: <20230205020855.146348-1-sjg@chromium.org> MIME-Version: 1.0 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.6 at phobos.denx.de X-Virus-Status: Clean The second call to cli_ch_process() is in the wrong place, meaning that the one of the characters of an invalid escape sequence is swallowed instead of being returned. Fix the bug and add a test to cover this. Signed-off-by: Simon Glass Reported-by: Heinrich Schuchardt --- Changes in v2: - Fix a failling test common/cli_readline.c | 3 +-- include/cli.h | 4 ++-- test/common/cread.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/common/cli_readline.c b/common/cli_readline.c index 709e9c3d38b..e83743e90c9 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -284,10 +284,9 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len, } ichar = getcmd_getch(); + ichar = cli_ch_process(cch, ichar); } - ichar = cli_ch_process(cch, ichar); - /* ichar=0x0 when error occurs in U-Boot getc */ if (!ichar) continue; diff --git a/include/cli.h b/include/cli.h index c777c90313f..094a6602d70 100644 --- a/include/cli.h +++ b/include/cli.h @@ -98,8 +98,8 @@ int cli_readline(const char *const prompt); * * @prompt: Prompt to display * @buffer: Place to put the line that is entered - * @timeout: Timeout in milliseconds, 0 if none - * Return: command line length excluding terminator, or -ve on error: of the + * @timeout: Timeout in seconds, 0 if none + * Return: command line length excluding terminator, or -ve on error: if the * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout * parameter), then -2 is returned. If a break is detected (Ctrl-C) then * -1 is returned. diff --git a/test/common/cread.c b/test/common/cread.c index 3dce4bdb0ef..2fdd29a265f 100644 --- a/test/common/cread.c +++ b/test/common/cread.c @@ -46,3 +46,48 @@ static int cli_ch_test(struct unit_test_state *uts) return 0; } COMMON_TEST(cli_ch_test, 0); + +static int cread_test(struct unit_test_state *uts) +{ + int duration; + ulong start; + char buf[10]; + + /* + * useful for debugging + * + * gd->flags &= ~GD_FLG_RECORD; + * print_buffer(0, buf, 1, 7, 0); + */ + + console_record_reset_enable(); + + /* simple input */ + *buf = '\0'; + ut_asserteq(4, console_in_puts("abc\n")); + ut_asserteq(3, cli_readline_into_buffer("-> ", buf, 1)); + ut_asserteq_str("abc", buf); + + /* try an escape sequence (cursor left after the 'c') */ + *buf = '\0'; + ut_asserteq(8, console_in_puts("abc\e[Dx\n")); + ut_asserteq(4, cli_readline_into_buffer("-> ", buf, 1)); + ut_asserteq_str("abxc", buf); + + /* invalid escape sequence */ + *buf = '\0'; + ut_asserteq(8, console_in_puts("abc\e[Xx\n")); + ut_asserteq(7, cli_readline_into_buffer("-> ", buf, 1)); + ut_asserteq_str("abc\e[Xx", buf); + + /* check timeout, should be between 1000 and 1050ms */ + start = get_timer(0); + *buf = '\0'; + ut_asserteq(-2, cli_readline_into_buffer("-> ", buf, 1)); + duration = get_timer(start) - 1000; + ut_assert(duration >= 0); + ut_assert(duration < 50); + + return 0; +} +COMMON_TEST(cread_test, 0); -- 2.39.1.519.gcb327c4b5f-goog