git: 942e52f776e6 - main - test_diskread(): detect end of the disk

From: Toomas Soome <tsoome_at_FreeBSD.org>
Date: Sun, 19 Jun 2022 15:26:37 UTC
The branch main has been updated by tsoome:

URL: https://cgit.FreeBSD.org/src/commit/?id=942e52f776e6bbe016a3e920c96a1cd4dbddf7e3

commit 942e52f776e6bbe016a3e920c96a1cd4dbddf7e3
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2022-06-01 07:28:43 +0000
Commit:     Toomas Soome <tsoome@FreeBSD.org>
CommitDate: 2022-06-19 15:26:22 +0000

    test_diskread(): detect end of the disk
    
    Detect the end of the disk condition. This may happpen when
    disk image is truncated and the reads are addressing blocks past
    image end.
    
    MFC after:              1 week
    Differential Revision:  https://reviews.freebsd.org/D35432
---
 stand/userboot/test/test.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/stand/userboot/test/test.c b/stand/userboot/test/test.c
index baf1b6243c1f..4ad18176ba6e 100644
--- a/stand/userboot/test/test.c
+++ b/stand/userboot/test/test.c
@@ -254,6 +254,11 @@ test_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
 	if (unit > disk_index || disk_fd[unit] == -1)
 		return (EIO);
 	n = pread(disk_fd[unit], dst, size, offset);
+	if (n == 0) {
+		printf("%s: end of disk (%ju)\n", __func__, (intmax_t)offset);
+		return (EIO);
+	}
+
 	if (n < 0)
 		return (errno);
 	*resid_return = size - n;