NetBSD Problem Report #56443

From www@netbsd.org  Wed Oct  6 07:30:46 2021
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 2D31E1A921F
	for <gnats-bugs@gnats.NetBSD.org>; Wed,  6 Oct 2021 07:30:46 +0000 (UTC)
Message-Id: <20211006073044.CAFB51A9239@mollari.NetBSD.org>
Date: Wed,  6 Oct 2021 07:30:44 +0000 (UTC)
From: rvp@SDF.ORG
Reply-To: rvp@SDF.ORG
To: gnats-bugs@NetBSD.org
Subject: dd conv=swab doesn't always work
X-Send-Pr-Version: www-1.0

>Number:         56443
>Category:       bin
>Synopsis:       dd conv=swab doesn't always work
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Oct 06 07:35:00 +0000 2021
>Closed-Date:    Tue May 24 21:42:25 +0000 2022
>Last-Modified:  Tue May 24 21:42:25 +0000 2022
>Originator:     RVP
>Release:        NetBSD/amd64 9.99.90
>Organization:
>Environment:
NetBSD x202e.localdomain 9.99.90 NetBSD 9.99.90 (MYKERNEL) #0: Wed Oct  6 03:29:01 UTC 2021  bld@x202e.localdomain:/usr/obj/sys/arch/amd64/compile/MYKERNEL amd64
>Description:
dd conv=swab doesn't swap the final pair of bytes at the end of input or
of the buffer:

$ printf 'abcdefgh' | dd conv=swab msgfmt=quiet
badcfegh
$ printf 'abcdefgh' | dd conv=swab msgfmt=quiet bs=4
bacdfegh
$
>How-To-Repeat:
See above, and also run this test script:

---START dd_test.sh---
#!/bin/sh

set -eu -o pipefail
opt="conv=swab msgfmt=quiet"
# make a ~1K buffer 
data=$(jot -b abcdefghijklmnopqrstuvwxyz 40 | tr -d '\n')
for n in $(jot 521)	# a prime no. > def. block size
do	in=$(printf '%.*s' $n "$data" | ./swab)
	out=$(printf '%.*s' $n "$data" | dd $opt)
	if [ "$in" != "$out" ]
	then	echo >&2 "$0: mismatch @ len $n:"
		echo >&2 "exp: $in"
		echo >&2 "got: $out"
		exit 1
	fi
done
---END dd_test.sh---

which needs this program to be compiled:

---START swab.c---
/**
 * swap byte-pairs
 */
#include <stdio.h>

int
main(void)
{
	int c1, c2;

	for (;;) {
		if ((c1 = getchar()) == EOF)
			break;
		if ((c2 = getchar()) == EOF) {
			printf("%c", c1);
			break;
		}
		printf("%c%c", c2, c1);
	}
	return 0;
}
---END swab.c---
>Fix:
diff -urN dd.orig/dd_swab.c dd/dd_swab.c
--- dd.orig/dd_swab.c	2019-10-04 08:57:38.000000000 +0000
+++ dd/dd_swab.c	2021-09-18 09:18:12.127706233 +0000
@@ -71,7 +71,7 @@
 	}

 	/* round to multiple of 8 */
-	while ((--len % 8) != 0)
+	for ( ;(len % 8) != 0; --len)
 		STEP;
 	len /= 8;
 	if (len == 0)

>Release-Note:

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56443 CVS commit: src/bin/dd
Date: Fri, 8 Oct 2021 10:44:49 -0400

 Module Name:	src
 Committed By:	christos
 Date:		Fri Oct  8 14:44:49 UTC 2021

 Modified Files:
 	src/bin/dd: dd_swab.c

 Log Message:
 PR/56443: RVP: dd conv=swab doesn't always work


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/bin/dd/dd_swab.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: rillig@NetBSD.org
State-Changed-When: Tue, 24 May 2022 21:42:25 +0000
State-Changed-Why:
Fixed for NetBSD 10, thanks for the PR.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.