From 0413d6063ab04f3599dba1c423565a19dfd8b766 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Thu, 22 Oct 2020 11:29:23 -0700
Subject: [PATCH v21 2/2] Fixing sloppy regression test coding

---
 contrib/amcheck/t/001_verify_heapam.pl | 27 +++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/contrib/amcheck/t/001_verify_heapam.pl b/contrib/amcheck/t/001_verify_heapam.pl
index e7526c17b8..9b9190db35 100644
--- a/contrib/amcheck/t/001_verify_heapam.pl
+++ b/contrib/amcheck/t/001_verify_heapam.pl
@@ -152,6 +152,20 @@ sub fresh_test_table
 	));
 }
 
+sub syswrite_or_bail
+{
+	my ($fh, $contents, $length) = @_;
+
+	my $written = syswrite($fh, $contents, $length);
+
+	BAIL_OUT("syswrite failed: $!")
+		unless defined $written;
+	BAIL_OUT("short syswrite: $written bytes written, $length expected")
+		if ($written < $length);
+	BAIL_OUT("long syswrite: $written bytes written, $length expected")
+		if ($written > $length);
+}
+
 # Stops the test node, corrupts the first page of the named relation, and
 # restarts the node.
 sub corrupt_first_page_internal
@@ -161,19 +175,22 @@ sub corrupt_first_page_internal
 
 	$node->stop;
 	my $fh;
-	open($fh, '+<', $relpath);
+	open($fh, '+<', $relpath)
+		or BAIL_OUT("open failed: $!");
 	binmode $fh;
 
 	# If we corrupt the header, postgres won't allow the page into the buffer.
-	syswrite($fh, '\xFF\xFF\xFF\xFF', 8) if ($corrupt_header);
+	syswrite_or_bail($fh, '\xFF' x 8, 8) if ($corrupt_header);
 
 	# Corrupt at least the line pointers.  Exactly what this corrupts will
 	# depend on the page, as it may run past the line pointers into the user
 	# data.  We stop short of writing 2048 bytes (2k), the smallest supported
 	# page size, as we don't want to corrupt the next page.
-	seek($fh, 32, 0);
-	syswrite($fh, '\x77\x77\x77\x77', 500);
-	close($fh);
+	seek($fh, 32, 0)
+		or BAIL_OUT("seek failed: $!");
+	syswrite_or_bail($fh, '\x77' x 2000, 2000);
+	close($fh)
+		or BAIL_OUT("close failed: $!");;
 	$node->start;
 }
 
-- 
2.21.1 (Apple Git-122.3)

