hexdump(1)/od(1) skip function off-by-one when offset == file length

Alexander Best arundel at freebsd.org
Sun Aug 29 16:27:08 UTC 2010

just discovered this issue while going through some linux mailinglists:

otaku% dd if=/dev/urandom of=testfile bs=1 count=42 
42+0 records in
42+0 records out
42 bytes transferred in 0.000393 secs (106894 bytes/sec)

otaku% hexdump -s 42 testfile 
000002a 134d b7b9 e085 da16 63b0 554a 1603 ead0
000003a 4bd1 fbfd c329 b606 e592 1377 6e10 4b9d
000004a c018 0fc9 ebf4 9ae2 9f1a               

otaku% hexdump -s 43 testfile

otaku% hexdump -s 41 testfile
0000029 009f                                   

the attached patch fixes this issue for HEAD. i also checked out any license
issues which could pop up. this fix comes from the util-linux-ng repository [1]
which seems entirely GPLv2'ed. :)


[1] http://git.kernel.org/?p=utils/util-linux-ng/util-linux-ng.git;a=tree;hb=HEAD

ps: no fix for od(1) necessary since it's simply a hardlink to hexdump(1). ;)
-------------- next part --------------
diff --git a/usr.bin/hexdump/display.c b/usr.bin/hexdump/display.c
index db04c49..3e3d903 100644
--- a/usr.bin/hexdump/display.c
+++ b/usr.bin/hexdump/display.c
@@ -378,7 +378,7 @@ doskip(const char *fname, int statok)
 	if (statok) {
 		if (fstat(fileno(stdin), &sb))
 			err(1, "%s", fname);
-		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
+		if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
 			address += sb.st_size;
 			skip -= sb.st_size;

More information about the freebsd-hackers mailing list