{"record":{"id":"c1756483f4ac31e8","repo":"appwrite/appwrite","slug":"a-read-needs-a-positive-length","errorCode":null,"errorMessage":"A read needs a positive length","messagePattern":"A read needs a positive length","errorType":"validation","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"packages/smtp/src/Transport/Native.php","lineNumber":62,"sourceCode":"        if ($stream === false) {\n            $reason = \"Cannot reach {$this->host}:{$this->port}: {$error}\";\n\n            // Which errno means \"timed out\" differs by platform, and reading the\n            // message is worse. We set the deadline, so we can tell by the clock.\n            throw microtime(true) - $started >= $timeout\n                ? new TimeoutException($reason, $code ?? 0)\n                : new ConnectionException($reason, $code ?? 0);\n        }\n\n        $this->stream = $stream;\n        $this->tls = $tls;\n        $this->timeout($timeout);\n    }\n\n    public function read(int $length, float $timeout): string\n    {\n        if ($length < 1) {\n            throw new \\InvalidArgumentException('A read needs a positive length');\n        }\n\n        $stream = $this->stream();\n        $this->timeout($timeout);\n\n        $data = @fread($stream, $length);\n\n        if ($data === false || $data === '') {\n            throw stream_get_meta_data($stream)['timed_out']\n                ? new TimeoutException('Timed out waiting for the server')\n                : new ConnectionException('The server closed the connection');\n        }\n\n        return $data;\n    }\n\n    public function write(string $data, float $timeout): void\n    {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/appwrite/appwrite/blob/7eab5f1970bacb06f8cce069c27b7d14a4178153/packages/smtp/src/Transport/Native.php#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Ensure the length argument is >= 1 before calling read(); skip the call when there are 0 bytes to read","Fix the length computation so buffer arithmetic cannot produce 0/negative values","Clamp with max(1, $length) only if your protocol genuinely allows 1-byte reads in place of zero-length ones"],"exampleFix":"// before\n$chunk = $transport->read($remaining, $timeout);\n// after\nif ($remaining > 0) {\n    $chunk = $transport->read($remaining, $timeout);\n}","handlingStrategy":"validation","validationCode":"if ($length < 1) { return ''; } // or throw in the caller's own terms\n$chunk = $transport->read($length, $timeout);","typeGuard":"null","tryCatchPattern":"try {\n    $data = $transport->read($length, $timeout);\n} catch (\\InvalidArgumentException $e) {\n    // fix caller's length math; log $length that was passed\n}","preventionTips":["Assert chunk/remaining sizes are >= 1 before any socket read","Exit read loops when the remaining byte count reaches 0","Check length fields parsed from protocol headers for 0 before reading"],"tags":["smtp","io","socket","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"7eab5f1970bacb06f8cce069c27b7d14a4178153","analyzedAt":"2026-09-23T00:38:38.905Z","contentChangedAt":"2026-09-23T00:38:38.905Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}