{"record":{"id":"6cb58a06177b64e6","repo":"vitessio/vitess","slug":"typedcollation-should-fit-in-an-int32","errorCode":null,"errorMessage":"TypedCollation should fit in an int32","messagePattern":"TypedCollation should fit in an int32","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"go/mysql/collations/coercion.go","lineNumber":26,"sourceCode":"    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\npackage collations\n\nimport (\n\t\"fmt\"\n\t\"unsafe\"\n)\n\nfunc init() {\n\tif unsafe.Sizeof(TypedCollation{}) != 4 {\n\t\tpanic(\"TypedCollation should fit in an int32\")\n\t}\n}\n\n// Coercibility is a numeric value that represents the precedence of a collation\n// when applied to a SQL expression. When trying to coerce the collations\n// of two different expressions so that they can be compared, the expression\n// with the lowest coercibility value will win and its collation will be forced\n// upon the other expression.\n//\n// The rules for assigning a Coercibility value to an expression are as follows:\n//\n//   - An explicit COLLATE clause has a coercibility of 0 (not coercible at all).\n//   - The concatenation of two strings with different collations has a coercibility of 1.\n//   - The collation of a column or a stored routine parameter or local variable has a coercibility of 2.\n//   - A “system constant” (the string returned by functions such as USER() or VERSION()) has a coercibility of 3.\n//   - The collation of a literal has a coercibility of 4.\n//   - The collation of a numeric or temporal value has a coercibility of 5.\n//   - NULL or an expression that is derived from NULL has a coercibility of 6.","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/mysql/collations/coercion.go#L8-L44","documentation":"An init() guard in go/mysql/collations panics if the TypedCollation struct is not exactly 4 bytes on the current platform. TypedCollation must fit in an int32 because it is passed/stored as a 32-bit value across API boundaries; a layout change would silently corrupt those paths.","triggerScenarios":"Building or running code after someone adds a field to (or changes the packing of) TypedCollation so unsafe.Sizeof(TypedCollation{}) != 4. This fires at process start, before any user code runs.","commonSituations":"Developers extending TypedCollation with a new field (e.g. adding repertoire flags or a new collation id width); building on an exotic platform/architecture with different alignment rules.","solutions":["Revert or rework the struct change so TypedCollation stays 4 bytes (pack fields into existing bit fields).","If more state is genuinely needed, use a lookup table keyed by the int32 instead of enlarging the struct.","Update any accompanying codegen/definitions that regenerate the struct, then rebuild."],"exampleFix":"// before\ntype TypedCollation struct {\n    Collation    uint16\n    Coercibility Coercibility\n    Repertoire   Repertoire\n    NewField     uint8 // pushes size past 4 bytes\n}\n// after\ntype TypedCollation struct {\n    Collation    uint16\n    Coercibility Coercibility\n    Repertoire   Repertoire // keep total size at 4 bytes\n}","handlingStrategy":"validation","validationCode":"// cannot run before init; instead add a CI test:\nfunc TestTypedCollationSize(t *testing.T) {\n    require.Equal(t, uintptr(4), unsafe.Sizeof(mysql.TypedCollation{}))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never add fields to TypedCollation without recomputing its size; keep it 4 bytes.","Pack extra state into existing bit fields or an external lookup table.","Add a CI unit test asserting unsafe.Sizeof(TypedCollation{}) == 4 so it fails before the init panic ever runs."],"tags":["collations","memory-layout","init-guard","panics"],"backgroundTag":"struct-size-invariant-violation","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}