appwrite/appwrite · error · InvalidArgumentException

A read needs a positive length

Error message

A read needs a positive length

What it means

Native transport's read() in packages/smtp/src/Transport/Native.php requires a length of at least 1 octet. fread() with length 0 or negative is undefined/useless, so the library rejects it early with an InvalidArgumentException before touching the socket. Callers must request a positive number of bytes.

Solutions

  1. Ensure the length argument is >= 1 before calling read(); skip the call when there are 0 bytes to read
  2. Fix the length computation so buffer arithmetic cannot produce 0/negative values
  3. Clamp with max(1, $length) only if your protocol genuinely allows 1-byte reads in place of zero-length ones

Example fix

// before
$chunk = $transport->read($remaining, $timeout);
// after
if ($remaining > 0) {
    $chunk = $transport->read($remaining, $timeout);
}
Defensive patterns

Strategy: validation

Validate before calling

if ($length < 1) { return ''; } // or throw in the caller's own terms
$chunk = $transport->read($length, $timeout);

Type guard

null

Try / catch

try {
    $data = $transport->read($length, $timeout);
} catch (\InvalidArgumentException $e) {
    // fix caller's length math; log $length that was passed
}

Prevention

When it happens

Trigger: Calling $transport->read(0, $timeout) or read(-1, ...) — typically a computed chunk size of 0 from an empty buffer/length math, or a miswritten loop reading 'remaining' bytes where remaining starts at 0.

Common situations: Looping socket readers where the remaining-size variable underflows; protocol parsers that compute 'read length' from a header field that is 0; tests probing boundary conditions (as seen in the test callers).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of appwrite/appwrite@7eab5f1970 (2026-09-23). Data as JSON: /api/errors/c1756483f4ac31e8. Report an issue: GitHub.

Appendix: source

Thrown at packages/smtp/src/Transport/Native.php:62

        if ($stream === false) {
            $reason = "Cannot reach {$this->host}:{$this->port}: {$error}";

            // Which errno means "timed out" differs by platform, and reading the
            // message is worse. We set the deadline, so we can tell by the clock.
            throw microtime(true) - $started >= $timeout
                ? new TimeoutException($reason, $code ?? 0)
                : new ConnectionException($reason, $code ?? 0);
        }

        $this->stream = $stream;
        $this->tls = $tls;
        $this->timeout($timeout);
    }

    public function read(int $length, float $timeout): string
    {
        if ($length < 1) {
            throw new \InvalidArgumentException('A read needs a positive length');
        }

        $stream = $this->stream();
        $this->timeout($timeout);

        $data = @fread($stream, $length);

        if ($data === false || $data === '') {
            throw stream_get_meta_data($stream)['timed_out']
                ? new TimeoutException('Timed out waiting for the server')
                : new ConnectionException('The server closed the connection');
        }

        return $data;
    }

    public function write(string $data, float $timeout): void
    {

View on GitHub (pinned to 7eab5f1970)