From patchwork Thu Feb 20 12:49:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "A. Sverdlin" X-Patchwork-Id: 866741 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1f85:b0:38f:210b:807b with SMTP id hj5csp179959wrb; Thu, 20 Feb 2025 04:50:07 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXVCrsCNSz8b4eeC3aohN07JiNEYEA611qG69gAa97OQ6fH8s9QxS/KOwqUbRyLtnquf53Uxg==@linaro.org X-Google-Smtp-Source: AGHT+IGH6Ou2JNdyROfEM8MCA71gXYVO13d9IiTfQvjEn135OAZmcEYdvwC1ghDeA9jh6DrasMXU X-Received: by 2002:a05:600c:4688:b0:439:89e9:4eff with SMTP id 5b1f17b1804b1-43989e951bbmr139322765e9.10.1740055807403; Thu, 20 Feb 2025 04:50:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740055807; cv=none; d=google.com; s=arc-20240605; b=iad7wDhjFpaebD0uciHOEvddGHsa43mUsqerKBOj32mcN7kmOSmW0p7aTE2OoWx/Mz ermo6B511ImjntDbWjYJRik8bdFrfaTn7Sm3Q5fIYRDc1JUYOKt1LrdCBc+CJbEpcbSS fs+yefZImfugsXR89XKyZxQC2DVgk7bydtgTUXsHC+dw8YhgAhsjwbgmh5nMNQeZWnrw 5Bvyj3E1U4bic6UVUbzSd13bTbg0rjkegwI8QmJVKNKPD8bmbx6aT20TSVFl1/ZaNXd6 3Uf/iWFAjRZdRNRTmwhD8lKg8LyM6vhDk0DNTAkX2XNpYZe9542lO9G8FONfwA/VFygb c/DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=0am+NklbSSCz+YJqS9na6fQp41gU5yM3iBENcwWDdi4=; fh=FQcp7Tjvh13/yujyHyov+U8dOBYyfdW8K55gvrfU/9w=; b=Mx7teUDAoD5wzb1MHsSJTNJymNm9/xYC3jwoUEYip6xpzC3yRlEiKr6UWifdxw7l2p k66RaZyMq9T9ZMic4wGYNuyrrQeC7pJfZyaO5isL/POboy/3BbiBquaTsLcpyX+wNc1z BHweBNkZ+WCLBQwzB+hZUkY+l6/qsPhq0z4iHMcV9bpUxJa/cXrFGkH7fqZ369s1j6jz 1JHLRJ66uX//Dzw4WjAly8EFwsCrAuzsBbo15tP7JzeXgokpHkldJYPSL5ez2M3gCfa5 y5X6AO8MI7iwRH/vwWXeyLceb6j9cOWSfNe3Wu5Sy/7gmIGn+a1hXaj2VufASu55O8gR n6PQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=dneUbI4v; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ffacd0b85a97d-38f25a283fdsi14330235f8f.709.2025.02.20.04.50.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 04:50:07 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=dneUbI4v; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0BF4380A46; Thu, 20 Feb 2025 13:50:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=siemens.com 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; secure) header.d=siemens.com header.i=alexander.sverdlin@siemens.com header.b="dneUbI4v"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 229E7801DE; Thu, 20 Feb 2025 13:50:05 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mta-64-227.siemens.flowmailer.net (mta-64-227.siemens.flowmailer.net [185.136.64.227]) (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 0318F8007D for ; Thu, 20 Feb 2025 13:50:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=siemens.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=alexander.sverdlin@siemens.com Received: by mta-64-227.siemens.flowmailer.net with ESMTPSA id 20250220125001e46aef54fc7c27d974 for ; Thu, 20 Feb 2025 13:50:01 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=alexander.sverdlin@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc; bh=0am+NklbSSCz+YJqS9na6fQp41gU5yM3iBENcwWDdi4=; b=dneUbI4vSlGL4y16UwCFSjxQrJE83YziMXm8hsHu6kYjTp4IFKu5p77L7dOQ+ER8QtOVr6 5rBcJQdu2ycvkIAVhXE9qMvIDa3BSa02n50q+uZFaadjOZ4yrMLYydb3cox3ZkQTkwqGCZha af1vsItYlB49LAWTu5VJk/jXChm63767+yQbaRH4hhTtI5ZQsLTRPOmCs0zp4jkv8uwD8CKd Ty69lH3QIxKb4SCVO7j5fLG3WUV46Znhq8giKM57e3K0Qq08T7PVDtDwV5uBUALxx59sANxu c8flgLWEMQdttpamP2VJrKW4r4H+LJD+4FkPaD7lGJBaONcKZ+GJJ+EQ==; From: "A. Sverdlin" To: Simon Glass , Stefan Roese , u-boot@lists.denx.de Cc: Alexander Sverdlin , Tom Rini , Chintan Vankar Subject: [PATCH] tiny-printf: emit \0 as %c Date: Thu, 20 Feb 2025 13:49:07 +0100 Message-ID: <20250220124908.3396843-1-alexander.sverdlin@siemens.com> MIME-Version: 1.0 X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-456497:519-21489:flowmailer 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.8 at phobos.denx.de X-Virus-Status: Clean From: Alexander Sverdlin The current code has a problematic corner case with formar "%c" and 0 as parameter. The proper zero byte is being emitted into digit buffer but the final copy into outstr expects null-terminated string and doesn't copy the required \0 byte. This has lead to malformed TFTP packets, refer to tftp_send() which relies on %c to generate multiple zero-terminated strings in one buffer. Introduce a variable to force the copy of one character in this case. The new behaviour is consistent with non-tiny implementation. Reported-by: Chintan Vankar Signed-off-by: Alexander Sverdlin --- lib/tiny-printf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c index 0503c17341f..faf55d7f327 100644 --- a/lib/tiny-printf.c +++ b/lib/tiny-printf.c @@ -211,6 +211,7 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va) bool lz = false; int width = 0; bool islong = false; + bool force_char = false; ch = *(fmt++); if (ch == '-') @@ -300,6 +301,8 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va) break; case 'c': out(info, (char)(va_arg(va, int))); + /* For the case when it's \0 char */ + force_char = true; break; case 's': p = va_arg(va, char*); @@ -317,8 +320,10 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va) while (width-- > 0) info->putc(info, lz ? '0' : ' '); if (p) { - while ((ch = *p++)) + while ((ch = *p++) || force_char) { info->putc(info, ch); + force_char = false; + } } } }