btree icon indicating copy to clipboard operation
btree copied to clipboard

NULL test in compact() fails

Open jchimene opened this issue 7 years ago • 0 comments

The NULL test in the current copyto() fails when its return result is zero. When $root is zero, the test (NULL != $root) is FALSE. In php, NULL and zero are equal in the current test. Perhaps you want to abort compacting when $root is zero, I can't tell from the copyto() method.

public function compact()
	{
		// I believe in uniqid(), so no checking if file already exists and locking
		$compact_filename = uniqid($this->filename . '~', TRUE);
		if (! ($compact = fopen($compact_filename, 'a+b'))) return FALSE;
		
		if (- 1 !== fseek($compact, 0, SEEK_END)) {
			$root = $this->copyto($compact);
			if (0 >= $root) {
				if (self::header($compact, $root)) {
					if (fflush($compact)) {
						@rename($compact_filename, $this->filename); /* will not work under windows, sorry */
						$this->nodecache = array();
						fclose($this->handle);
						$this->handle = $compact;
						return TRUE;
					}
				}
			}
		}
		fclose($compact);
		@unlink($compact_filename);
		return FALSE;

jchimene avatar Jul 07 '17 16:07 jchimene