A bug in copy() function

From: Date: Thu, 08 Aug 2002 23:59:33 +0000
Subject: A bug in copy() function
Groups: php.dev 
Request: Send a blank email to php-dev+get-86675@lists.php.net to get a copy of this message
The copy() function will report failure when copying a 0 byte file, even though a duplicate file has successfully been created. The problem occurs because _php_stream_copy_to_stream function inside stream.c does not check if the source file is 0 bytes and returns haveread bytes, which in case of blank file is 0. This later on get interpreted as a failure to copy file. Below is a patch against the latest CVS, which fixes this problem. --- streams.c Thu Aug 8 19:57:50 2002 +++ streams.c Thu Aug 8 19:51:36 2002 @@ -473,6 +473,12 @@ if (fstat(srcfd, &sbuf) == 0) { void *srcfile; + + /* in the event the source file's size is 0 bytes, return 1 to + * indicate success, because opening the file for write had already + * created a copy + * */ + if (sbuf.st_size == 0) return 1; if (maxlen > sbuf.st_size || maxlen == 0) maxlen = sbuf.st_size; The bug can be replicated with a php script listed below. <?php touch("mini"); var_dump(copy("mini", "mini2")); ?> Ilia Alshanetsky

« previous php.dev (#86675) next »