From patchwork Mon Feb 13 06:54:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiang W X-Patchwork-Id: 1741368 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=fU00j08R; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=cgyrI5XR; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PFZpJ2rHDz23h0 for ; Mon, 13 Feb 2023 17:54:48 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=kldisK4EqCmtDAyWFpFWuek2wSYTfbwiyzmKOgYk3dg=; b=fU00j08RdoxqAe E2v42hY92sQeIdkDB/TiICNqJ6adFlsda1nrfawKDEgmwHHi2msuPUIvz8uAeWDQ7/zPWhBPnXDjQ bBylf1LB0ykA3nvFH3mDOo3RIpOwi3p9cBkB7BAXSBgE8z2iV+IbENnrqrWw+XA9Kq65PxDGUysTA KDXz6NmP659bjqZT6gOLUxQC71UhKFXa3iqTlnTsv/gzc6JwMjY/FMkbzF6MgMoeV1H7obZTilp+B zV5SN0STTnP7flWhlejJ5w96psU1K9X88gWs36qV6NaELwT4fUqz4Km0DR0WQENVz4vFoHeELvFTD EZorb3XKq+yY9B0CneTA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pRSjR-00DOMY-6Q; Mon, 13 Feb 2023 06:54:33 +0000 Received: from m126.mail.126.com ([123.126.96.241]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pRSjO-00DOLz-2H for opensbi@lists.infradead.org; Mon, 13 Feb 2023 06:54:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=DIAh4 5XmtP+qjoq+FS9U7m9uFZmd/GpnyqJ29o1mee8=; b=cgyrI5XR1M1RIb91zf38v VhQKLGuREzY2ceq8bdJyjhd4YDG7q/u53sqqcYp8g22DLGAylqWKLPqRdlXImn08 WPlCmJRP+X50Lal7BMvEqIM4DW1zd86HQ7VipDfDWA/s7eLrHKrn+4sEc2k8+BnH c7rWMhZG4eyyxvd55HKPGE= Received: from x390.lan (unknown [58.247.140.37]) by smtp12 (Coremail) with SMTP id fORpCgC3Ri+f3uljlzqDAw--.57681S2; Mon, 13 Feb 2023 14:54:23 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Cc: Xiang W Subject: [PATCH v3] lib: sbi: fix missing '\r' for console Date: Mon, 13 Feb 2023 14:54:11 +0800 Message-Id: <20230213065411.131378-1-wxjstz@126.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-CM-TRANSID: fORpCgC3Ri+f3uljlzqDAw--.57681S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxCF45ur45XFy7ZryrZrW8WFg_yoW5Jw48pr s3Ga13tr4Sqasaqr9aqr9rWF1rG3WkKw13trW7Awn7Za4DAaykZ3y3Xw1Yv3y3J34UZFyU AF9YqF95CF48XFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRY2NNUUUUU= X-Originating-IP: [58.247.140.37] X-CM-SenderInfo: pz0m23b26rjloofrz/1tbiFx8VOlpEFtyxpAAAsm X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230212_225430_497357_809E2632 X-CRM114-Status: GOOD ( 11.28 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: print is finally implemented by sbi_putc or console_dev->puts. sbi_putc will add a \r before the output \n. This patch adds missing \r when outputting characters via console_dev->puts. Signed-off-by: Xiang W Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [wxjstz[at]126.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org print is finally implemented by sbi_putc or console_dev->puts. sbi_putc will add a \r before the output \n. This patch adds missing \r when outputting characters via console_dev->puts. Signed-off-by: Xiang W Changes since v3: - Prevent string overflow when looking for \n Changes since v2: - Fix the bug reported by Samuel. Prevent p from accessing memory other than strings. --- lib/sbi/sbi_console.c | 47 +++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c index f3ac003..99436c3 100644 --- a/lib/sbi/sbi_console.c +++ b/lib/sbi/sbi_console.c @@ -46,27 +46,44 @@ void sbi_putc(char ch) } } +static unsigned long console_puts_all(const char *str, unsigned long len) +{ + const char *s = str; + const char *e = s + len; + while (s < e) + s += console_dev->console_puts(s, e - s); + return len; +} + +static unsigned long console_puts(const char *str, unsigned long len) +{ + const char *s, *p, *e; + s = str; + e = str + len; + while (s < e) { + p = sbi_strchr(s, '\n'); + if (p == NULL || p >= e) + p = e; + console_puts_all(s, p - s); + if (p < e && *p == '\n') + console_puts_all("\r\n", 2); + s = p + 1; + } + return len; +} + static unsigned long nputs(const char *str, unsigned long len) { - unsigned long i, ret; + unsigned long i; if (console_dev && console_dev->console_puts) { - ret = console_dev->console_puts(str, len); + console_puts(str, len); } else { for (i = 0; i < len; i++) sbi_putc(str[i]); - ret = len; } - return ret; -} - -static void nputs_all(const char *str, unsigned long len) -{ - unsigned long p = 0; - - while (p < len) - p += nputs(&str[p], len - p); + return len; } void sbi_puts(const char *str) @@ -74,7 +91,7 @@ void sbi_puts(const char *str) unsigned long len = sbi_strlen(str); spin_lock(&console_out_lock); - nputs_all(str, len); + nputs(str, len); spin_unlock(&console_out_lock); } @@ -255,7 +272,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) for (; *format != 0; ++format) { if (use_tbuf && !console_tbuf_len) { - nputs_all(console_tbuf, CONSOLE_TBUF_MAX); + nputs(console_tbuf, CONSOLE_TBUF_MAX); console_tbuf_len = CONSOLE_TBUF_MAX; tout = console_tbuf; } @@ -386,7 +403,7 @@ literal: } if (use_tbuf && console_tbuf_len < CONSOLE_TBUF_MAX) - nputs_all(console_tbuf, CONSOLE_TBUF_MAX - console_tbuf_len); + nputs(console_tbuf, CONSOLE_TBUF_MAX - console_tbuf_len); return pc; }