From fb16e7c6ad0e8d27cba8ee6279f71f88c7f95fd7 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Wed, 12 Apr 2023 20:13:51 -0700 Subject: [PATCH] dd: Consider block count in inner read loop When ibs is smaller than obs, checking the block count in the outer loop is not sufficient; we need to break out of the inner read loop once we've read the specified number of blocks. Thanks to phoebos for reporting this issue. --- dd.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dd.c b/dd.c index 350843b..36eb409 100644 --- a/dd.c +++ b/dd.c @@ -173,8 +173,12 @@ main(int argc, char *argv[]) eprintf("lseek:"); /* XXX: handle non-seekable files */ } - while (!eof && (count == -1 || ifull + ipart < count)) { + while (!eof) { while (ipos - opos < obs) { + if (ifull + ipart == count) { + eof = 1; + break; + } ret = read(ifd, buf + ipos, ibs); if (ret == 0) { eof = 1;