fix invalid read/write errors