{"record":{"id":"7cde538374d027d7","repo":"apache/cassandra","slug":"invalid-byte-for-ascii-byte-tostring-b","errorCode":null,"errorMessage":"Invalid byte for ascii: + Byte.toString(b)","messagePattern":"Invalid byte for ascii: \\+ Byte\\.toString\\(b\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/AsciiSerializer.java","lineNumber":40,"sourceCode":"import org.apache.cassandra.db.marshal.ValueAccessor;\n\npublic class AsciiSerializer extends AbstractTextSerializer\n{\n    public static final AsciiSerializer instance = new AsciiSerializer();\n\n    private AsciiSerializer()\n    {\n        super(StandardCharsets.US_ASCII);\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        // 0-127\n        for (int i=0, size=accessor.size(value); i < size; i++)\n        {\n            byte b = accessor.getByte(value, i);\n            if (b < 0)\n                throw new MarshalException(\"Invalid byte for ascii: \" + Byte.toString(b));\n        }\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":44,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/AsciiSerializer.java#L22-L44","documentation":"AsciiSerializer.validate rejects any byte with the high bit set: an ascii column may only contain bytes 0-127. MarshalException(\"Invalid byte for ascii: N\") is thrown when a value outside 7-bit ASCII is written or validated against an ascii column.","triggerScenarios":"Inserting a string containing non-ASCII characters (e.g. UTF-8 accented letters, emoji) into a column of type ascii; validate() iterates bytes and throws on the first negative byte (src/java/org/apache/cassandra/serializers/AsciiSerializer.java:40).","commonSituations":"Internationalized application data written to ascii columns; drivers that don't pre-validate on the client; schema migrations where a text column was recreated as ascii.","solutions":["Change the column type from ascii to text: ALTER TABLE t ALTER c TYPE text;","Strip/encode the data client-side before insert (e.g. replace non-ASCII chars or use CharsetEncoder with a replacement).","On the server, handle AsciiType instead of UTF8Type for validation if the data really is 7-bit.","Validate strings in application code before sending to avoid failed writes."],"exampleFix":"// before\nString s = \"café\"; // 'é' is non-ASCII -> MarshalException on ascii column\n// after\nString s = s.replaceAll(\"[^\\\\x00-\\\\x7F]\", \"\");\n// or: ALTER TABLE t ALTER c TYPE text;","handlingStrategy":"validation","validationCode":"boolean isAscii = s.chars().allMatch(c -> c < 128); // validate before writing to ascii column","typeGuard":"static boolean isAscii(String s) { return s != null && StandardCharsets.US_ASCII.newEncoder().canEncode(s); }","tryCatchPattern":"try { insertAscii(col, s); } catch (com.datastax.driver.core.exceptions.InvalidQueryException | MarshalException e) { /* non-ASCII data: switch to text column */ }","preventionTips":["Prefer text over ascii for any user-supplied string","Strip non-ASCII characters at ingestion time if ascii is required","Use US_ASCII encoder with replacement in pipelines","Catch driver invalid-query errors signaling failed validation"],"tags":["serialization","ascii","encoding","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}