{"record":{"id":"078140efcf66f480","repo":"lionsoul2014/ip2region","slug":"incomplete-read-read-bytes-should-be-len-078140","errorCode":null,"errorMessage":"incomplete read: read bytes should be {$len}","messagePattern":"incomplete read: read bytes should be (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"binding/php/xdb/Searcher.class.php","lineNumber":567,"sourceCode":"        // check the in-memory buffer first\n        if ($this->contentBuff != null) {\n            return substr($this->contentBuff, $offset, $len);\n        }\n\n        // read from the file\n        $r = fseek($this->handle, $offset);\n        if ($r == -1) {\n            throw new Exception(\"failed to fseek to {$offset}\");\n        }\n\n        $this->ioCount++;\n        $buff = fread($this->handle, $len);\n        if ($buff === false) {\n            throw new Exception(\"failed to fread from {$len}\");\n        }\n\n        if (strlen($buff) != $len) {\n            throw new Exception(\"incomplete read: read bytes should be {$len}\");\n        }\n\n        return $buff;\n    }\n\n}\n","sourceCodeStart":549,"sourceCodeEnd":574,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/php/xdb/Searcher.class.php#L549-L574","documentation":"fread() succeeded but returned fewer bytes than requested ($len). A short read on a well-formed xdb means the file is truncated or the offset is past the end, so the library refuses to return partial data.","triggerScenarios":"search()/read() where strlen($buff) != $len — reading past EOF because the xdb file is truncated/corrupted, or the data was written by an incompatible structure version.","commonSituations":"Partially downloaded or interrupted copy of the xdb file; wrong xdb file version paired with a newer/older searcher; file replaced mid-search with a smaller file.","solutions":["Re-download/regenerate the xdb file and verify its size and checksum against the source","Confirm the xdb structure version is compatible with the library version you run (v2/v3)","Run the library's structure verification (e.g. util verify) against the file before searching"],"exampleFix":"// before\n$searcher = ...newWithFileOnly('/path/truncated.xdb');\n// after\n// re-copy full file, then verify:\n$searcher = ...newWithFileOnly('/path/full.xdb');\n// or check: filesize($dbFile) matches the publisher's expected size","handlingStrategy":"validation","validationCode":"$expected = 11; // check header/expected size from your xdb source\nif (filesize($dbFile) < $expected) {\n    throw new RuntimeException(\"xdb file truncated: {$dbFile}\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $region = $searcher->search($ip);\n} catch (\\Exception $e) {\n    if (strpos($e->getMessage(), 'incomplete read') !== false) {\n        // file truncated/corrupt: re-fetch the xdb and rebuild the searcher\n        $region = null;\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Verify the xdb file size/checksum after download or deploy","Do not truncate copies with scp/ftp interruptions; use atomic replace","Keep searcher and xdb structure versions in sync"],"tags":["php","file-io","corruption","short-read"],"backgroundTag":"xdb-file-truncated","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}