From eb82e1a1f84bc195501db410174e1ec84b8e2307 Mon Sep 17 00:00:00 2001 From: Maxim Konovalov Date: Sat, 7 Oct 2006 12:11:21 +0000 Subject: [PATCH] o Avoid division by zero. o Place error checking code near to the syscall. Submitted by: bde --- bin/cp/utils.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 64ebc52037fe..5286c74af037 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -54,7 +54,8 @@ __FBSDID("$FreeBSD$"); #include #include "extern.h" -#define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y)) + +#define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y))) int copy_file(const FTSENT *entp, int dne) @@ -149,6 +150,8 @@ copy_file(const FTSENT *entp, int dne) for (bufp = p, wresid = fs->st_size; ; bufp += wcount, wresid -= (size_t)wcount) { wcount = write(to_fd, bufp, wresid); + if (wcount <= 0) + break; wtotal += wcount; if (info) { info = 0; @@ -158,7 +161,7 @@ copy_file(const FTSENT *entp, int dne) cp_pct(wtotal, fs->st_size)); } - if (wcount >= (ssize_t)wresid || wcount <= 0) + if (wcount >= (ssize_t)wresid) break; } if (wcount != (ssize_t)wresid) { @@ -179,6 +182,8 @@ copy_file(const FTSENT *entp, int dne) for (bufp = buf, wresid = rcount; ; bufp += wcount, wresid -= wcount) { wcount = write(to_fd, bufp, wresid); + if (wcount <= 0) + break; wtotal += wcount; if (info) { info = 0; @@ -188,7 +193,7 @@ copy_file(const FTSENT *entp, int dne) cp_pct(wtotal, fs->st_size)); } - if (wcount >= (ssize_t)wresid || wcount <= 0) + if (wcount >= (ssize_t)wresid) break; } if (wcount != (ssize_t)wresid) {