Skip to contentSkip to conversation

iotests: Fix nonportable use of od --endian

PATCH Superseded Eric Blake 2 participants 4 messages Feb 19, 2020 – Feb 26, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 iotests: Fix nonportable use of od --endian

Tests 261 and 272 fail on RHEL 7 with coreutils 8.22, since od --endian was not added until coreutils 8.23. Fix this by manually constructing the final value one byte at a time.

---
 tests/qemu-iotests/common.rc | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
tests/qemu-iotests/common.rc +8 -2
index 8a6366c09daf..b77ef3d22cd1 100644
@@ -56,6 +56,12 @@ poke_file()
 # peek_file_le 'test.img' 512 2 => 65534
 peek_file_le()
 {
-    # Wrap in echo $() to strip spaces
-    echo $(od -j"$2" -N"$3" --endian=little -An -vtu"$3" "$1")
+    local val=0 shift=0 i
+
+    # coreutils' od --endian is not portable, so manually assemble bytes.
+    for i in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
+        val=$(( val | (i << shift) ))
+        shift=$((shift + 8))
+    done
+    echo $val
 }
 # peek_file_be 'test.img' 512 2 => 65279
 peek_file_be()
 {
-    # Wrap in echo $() to strip spaces
-    echo $(od -j"$2" -N"$3" --endian=big -An -vtu"$3" "$1")
+    local val=0 i
+
+    # coreutils' od --endian is not portable, so manually assemble bytes.
+    for i in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
+        val=$(( (val << 8) | i ))
+    done
+    echo $val
 }

-# peek_file_raw 'test.img' 512 2 => '\xff\xfe'
+# peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data +# is likely to contain \0 or trailing \n.
peek_file_raw()

 {
     dd if="$1" bs=1 skip="$2" count="$3" status=none
Signature · 1 line
2.24.1
author
#1 lore raw
Quoted 19 lines · Eric Blake

Tests 261 and 272 fail on RHEL 7 with coreutils 8.22, since od --endian was not added until coreutils 8.23. Fix this by manually constructing the final value one byte at a time.

---
  tests/qemu-iotests/common.rc | 22 +++++++++++++++++-----
  1 file changed, 17 insertions(+), 5 deletions(-)
tests/qemu-iotests/common.rc +0 -0
index 8a6366c09daf..b77ef3d22cd1 100644
@@ -56,6 +56,12 @@ poke_file()
  # peek_file_le 'test.img' 512 2 => 65534
  peek_file_le()
  {

tests/qemu-iotests/common.rc poke_file() · line 66

Quoted 10 lines
tests/qemu-iotests/common.rc +8 -2
-    # Wrap in echo $() to strip spaces
-    echo $(od -j"$2" -N"$3" --endian=little -An -vtu"$3" "$1")
+    local val=0 shift=0 i
+
+    # coreutils' od --endian is not portable, so manually assemble bytes.
+    for i in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
+        val=$(( val | (i << shift) ))
+        shift=$((shift + 8))
+    done
+    echo $val

Reports large 8-byte values as negative, whereas the pre-patch version reported them as positive. To preserve behavior, we should squash in:

tests/qemu-iotests/common.rc +2 -2
index 8966ad5cde78..a596856d4d8c 100644
@@ -63,7 +63,7 @@ peek_file_le()
         val=$(( val | (i << shift) ))
         shift=$((shift + 8))
     done
-    echo $val
+    printf %llu $val
 }
 
 # peek_file_be 'test.img' 512 2 => 65279
@@ -75,7 +75,7 @@ peek_file_be()
     for i in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
         val=$(( (val << 8) | i ))
     done
-    echo $val
+    printf %llu $val
 }
 
 # peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Blake

tests/qemu-iotests/common.rc poke_file()

Quoted 16 lines · Eric Blake

Tests 261 and 272 fail on RHEL 7 with coreutils 8.22, since od --endian was not added until coreutils 8.23. Fix this by manually constructing the final value one byte at a time.

---
 tests/qemu-iotests/common.rc | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
tests/qemu-iotests/common.rc +0 -0
index 8a6366c09daf..b77ef3d22cd1 100644
@@ -56,6 +56,12 @@ poke_file()

I don’t know how you did it, but something in your workflow broke this patch. This should be -56,18 +56,30.

(Note that git accepts this patch without error, but it drops everything after the first part. I noticed because your squash-in failed to apply.
I suppose I might have noticed later when reviewing, because only
peek_file_le() was touched, but who knows.)

tests/qemu-iotests/common.rc poke_file() · line 62

Quoted 9 lines
tests/qemu-iotests/common.rc +4 -2
 # peek_file_le 'test.img' 512 2 => 65534
 peek_file_le()
 {
-    # Wrap in echo $() to strip spaces
-    echo $(od -j"$2" -N"$3" --endian=little -An -vtu"$3" "$1")
+    local val=0 shift=0 i
+
+    # coreutils' od --endian is not portable, so manually assemble bytes.
+    for i in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do

“i” is a weird name for something that isn’t an index.

tests/qemu-iotests/common.rc poke_file() · line 67

Quoted 6 lines
tests/qemu-iotests/common.rc +4 -0
+        val=$(( val | (i << shift) ))
+        shift=$((shift + 8))
+    done
+    echo $val
 }

Regarding the broken patch format again: There are no spaces here...

Quoted 4 lines
 # peek_file_be 'test.img' 512 2 => 65279
 peek_file_be()
 {

tests/qemu-iotests/common.rc poke_file() · line 67

Quoted 10 lines
tests/qemu-iotests/common.rc +7 -2
-    # Wrap in echo $() to strip spaces
-    echo $(od -j"$2" -N"$3" --endian=big -An -vtu"$3" "$1")
+    local val=0 i
+
+    # coreutils' od --endian is not portable, so manually assemble bytes.
+    for i in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
+        val=$(( (val << 8) | i ))
+    done
+    echo $val
 }

...and here like there normally would be.

With the patch format fixed, and your proposed to-be-squashed patch squashed in, there isn’t anything wrong, so:

But if I’m already about to squash something in, would you allow me to rename all instances of “i” to e.g. “byte”?

Max

Quoted 7 lines
tests/qemu-iotests/common.rc +2 -1
-# peek_file_raw 'test.img' 512 2 => '\xff\xfe'
+# peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data
+# is likely to contain \0 or trailing \n.
 peek_file_raw()
 {
     dd if="$1" bs=1 skip="$2" count="$3" status=none
author
#3 lore raw
Quoted 12 lines · Max Reitz
Quoted 11 lines · Eric Blake

Tests 261 and 272 fail on RHEL 7 with coreutils 8.22, since od --endian was not added until coreutils 8.23. Fix this by manually constructing the final value one byte at a time.

---
  tests/qemu-iotests/common.rc | 22 +++++++++++++++++-----
  1 file changed, 17 insertions(+), 5 deletions(-)
Quoted 8 lines

tests/qemu-iotests/common.rc poke_file()

Quoted 5 lines
tests/qemu-iotests/common.rc +0 -0
index 8a6366c09daf..b77ef3d22cd1 100644
@@ -56,6 +56,12 @@ poke_file()

I don’t know how you did it, but something in your workflow broke this patch. This should be -56,18 +56,30.

I'm not sure, either. Would me sending a v2 make it easier?

Quoted 8 lines

(Note that git accepts this patch without error, but it drops everything after the first part. I noticed because your squash-in failed to apply.
I suppose I might have noticed later when reviewing, because only
peek_file_le() was touched, but who knows.)

Quoted 7 lines

With the patch format fixed, and your proposed to-be-squashed patch squashed in, there isn’t anything wrong, so:

But if I’m already about to squash something in, would you allow me to rename all instances of “i” to e.g. “byte”?

I'll do that on a v2.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org