From nobody Sat Sep 6 14:41:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1757082118; cv=none; d=zohomail.com; s=zohoarc; b=Dq6HDlXbpibsGrWnxeTdOc/XVh/pgmg0C1TqVWYwVbDUdYDbA6/RhXROiVFHo07/Axxf0d1f5RHtZRgtWD/SXYUAerMcqIzTU4u12ZoA+iZERi6qeeG/5jrUFy+UDPi+q1RtRu03uKjQv6iAOR6mS/HfnOfnZeXer/d3YSY1ZSk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757082118; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=N+I1NJouYOnrrHASJrjD5TNHeMpicQO5xeT8Na9pxR0=; b=Fh1oc65Lxo6pzLXdVOPme3OJ4BLQpbUVZeL/5kyd/vWPCOD/RrWeyUx5AZKpSBM4uEUrkHeVS5li4rF3wq98b7EUSQ4/It8/t5bolOmlG/T62ApyEEJ4DS3MhVIADCt9+X5thxJD0dcmZCfern2uYBlDd+HCwV1KqDEAS80//K4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757082118095780.2477996876222; Fri, 5 Sep 2025 07:21:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uuXIk-0006kO-7G; Fri, 05 Sep 2025 10:20:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uuXIh-0006ja-B3 for qemu-devel@nongnu.org; Fri, 05 Sep 2025 10:20:28 -0400 Received: from forwardcorp1d.mail.yandex.net ([178.154.239.200]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uuXIU-0007wi-SC for qemu-devel@nongnu.org; Fri, 05 Sep 2025 10:20:26 -0400 Received: from mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:cf2d:0:640:140f:0]) by forwardcorp1d.mail.yandex.net (Yandex) with ESMTPS id B98A882595; Fri, 05 Sep 2025 17:20:08 +0300 (MSK) Received: from vsementsov-lin.. (unknown [2a02:6bf:8080:b8f::1:11]) by mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id 7KjETd4Ft4Y0-N13GkYh7; Fri, 05 Sep 2025 17:20:08 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1757082008; bh=N+I1NJouYOnrrHASJrjD5TNHeMpicQO5xeT8Na9pxR0=; h=Message-ID:Date:Cc:Subject:To:From; b=z27zLHzkGSMZhdHIMtWjMSM5SUNXC1iqeZZ/XLtuJRlzg0XiCIyFl3hpUkt/v4AYL orfjurf3HzwkxvdbhG7F42tvWidSi+wGMzXNU+BLOFdhiWiCtWIjE6t3+vhlH3JzQu n9zkQZBQ47xfWJFk1vKbzKW18Sw8tBSeWbr99fJI= Authentication-Results: mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: berrange@redhat.com Cc: qemu-devel@nongnu.org, vsementsov@yandex-team.ru Subject: [PATCH] util/hexdump: fix QEMU_HEXDUMP_LINE_WIDTH logic Date: Fri, 5 Sep 2025 17:20:06 +0300 Message-ID: <20250905142006.2230345-1-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.154.239.200; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp1d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1757082121125116600 Content-Type: text/plain; charset="utf-8" QEMU_HEXDUMP_LINE_WIDTH calculation doesn't correspond to qemu_hexdump_line(). This leads to last line of the dump (when length is not multiply of 16) has badly aligned ASCII part. Let's calculate length the same way. Signed-off-by: Vladimir Sementsov-Ogievskiy --- util/hexdump.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/util/hexdump.c b/util/hexdump.c index f29ffceb74..7cfc547261 100644 --- a/util/hexdump.c +++ b/util/hexdump.c @@ -22,6 +22,19 @@ static inline char hexdump_nibble(unsigned x) return (x < 10 ? '0' : 'a' - 10) + x; } =20 +static size_t hexdump_line_length(size_t buf_len, size_t unit_len, + size_t block_len) +{ + size_t est =3D buf_len * 2; + if (unit_len) { + est +=3D buf_len / unit_len; + } + if (block_len) { + est +=3D buf_len / block_len; + } + return est; +} + GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len, size_t unit_len, size_t block_len) { @@ -30,14 +43,8 @@ GString *qemu_hexdump_line(GString *str, const void *vbu= f, size_t len, =20 if (str =3D=3D NULL) { /* Estimate the length of the output to avoid reallocs. */ - size_t est =3D len * 2; - if (unit_len) { - est +=3D len / unit_len; - } - if (block_len) { - est +=3D len / block_len; - } - str =3D g_string_sized_new(est + 1); + str =3D g_string_sized_new(hexdump_line_length(len, unit_len, bloc= k_len) + + 1); } =20 for (u =3D 0, b =3D 0; len; u++, b++, len--, buf++) { @@ -76,13 +83,16 @@ static void asciidump_line(char *line, const void *bufp= tr, size_t len) } =20 #define QEMU_HEXDUMP_LINE_BYTES 16 -#define QEMU_HEXDUMP_LINE_WIDTH \ - (QEMU_HEXDUMP_LINE_BYTES * 2 + QEMU_HEXDUMP_LINE_BYTES / 4) +#define QEMU_HEXDUMP_UNIT 1 +#define QEMU_HEXDUMP_BLOCK 4 =20 void qemu_hexdump(FILE *fp, const char *prefix, const void *bufptr, size_t size) { - g_autoptr(GString) str =3D g_string_sized_new(QEMU_HEXDUMP_LINE_WIDTH = + 1); + int width =3D hexdump_line_length(QEMU_HEXDUMP_LINE_BYTES, + QEMU_HEXDUMP_UNIT, + QEMU_HEXDUMP_BLOCK); + g_autoptr(GString) str =3D g_string_sized_new(width + 1); char ascii[QEMU_HEXDUMP_LINE_BYTES + 1]; size_t b, len; =20 @@ -90,11 +100,11 @@ void qemu_hexdump(FILE *fp, const char *prefix, len =3D MIN(size - b, QEMU_HEXDUMP_LINE_BYTES); =20 g_string_truncate(str, 0); - qemu_hexdump_line(str, bufptr + b, len, 1, 4); + qemu_hexdump_line(str, bufptr + b, len, + QEMU_HEXDUMP_UNIT, QEMU_HEXDUMP_BLOCK); asciidump_line(ascii, bufptr + b, len); =20 - fprintf(fp, "%s: %04zx: %-*s %s\n", - prefix, b, QEMU_HEXDUMP_LINE_WIDTH, str->str, ascii); + fprintf(fp, "%s: %04zx: %-*s %s\n", prefix, b, width, str->str, as= cii); } =20 } --=20 2.48.1