{"record":{"id":"f779f02afb39e0d4","repo":"XTLS/Xray-core","slug":"padding-turn-d-has-invalid-direction-d","errorCode":null,"errorMessage":"padding turn %d has invalid direction: %d","messagePattern":"padding turn (.+?) has invalid direction: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/padding.go","lineNumber":86,"sourceCode":"\t\t}\n\t}\n\treturn nil\n}\n\nfunc validatePaddingSchedule(schedule []paddingTurn, firstTurnPrefixLength int) error {\n\tif len(schedule) == 0 {\n\t\treturn fmt.Errorf(\"empty padding schedule\")\n\t}\n\tif firstTurnPrefixLength < 0 {\n\t\treturn fmt.Errorf(\"negative first turn prefix length: %d\", firstTurnPrefixLength)\n\t}\n\tif firstTurnPrefixLength > 0 && schedule[0].direction != paddingClientToServer {\n\t\treturn fmt.Errorf(\"first prefixed padding turn is not client-to-server\")\n\t}\n\n\tfor i, turn := range schedule {\n\t\tif turn.direction != paddingClientToServer && turn.direction != paddingServerToClient {\n\t\t\treturn fmt.Errorf(\"padding turn %d has invalid direction: %d\", i, turn.direction)\n\t\t}\n\t\tif err := validatePaddingDelayRange(turn.startDelay); err != nil {\n\t\t\treturn fmt.Errorf(\"padding turn %d has an invalid start delay: %w\", i, err)\n\t\t}\n\t\tif err := validatePaddingDelayRange(turn.chunkDelay); err != nil {\n\t\t\treturn fmt.Errorf(\"padding turn %d has an invalid chunk delay: %w\", i, err)\n\t\t}\n\t\tif turn.writeChunkMinLength < 0 || turn.writeChunkLength < turn.writeChunkMinLength || turn.writeChunkLength > maxPaddingChunkLength {\n\t\t\treturn fmt.Errorf(\"padding turn %d has an invalid write chunk range: %d-%d\", i, turn.writeChunkMinLength, turn.writeChunkLength)\n\t\t}\n\t\tif len(turn.variants) > 0 && turn.writeChunkLength != 0 {\n\t\t\treturn fmt.Errorf(\"padding turn %d combines variants with generated write chunks\", i)\n\t\t}\n\n\t\tminLength, maxLength, err := paddingTurnBounds(turn)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"padding turn %d: %w\", i, err)\n\t\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/padding.go#L68-L104","documentation":"validatePaddingSchedule found a turn whose direction field is neither paddingClientToServer (1) nor paddingServerToClient (2) — i.e. zero (unset) or an out-of-range value. Padding turns must alternate directions, so an unset or corrupted direction fails immediately.","triggerScenarios":"A paddingTurn literal where direction was never assigned (defaults to 0), or arithmetic on paddingDirection that overflows the two valid constants.","commonSituations":"Hand-written schedule literals in forks; omitting the Direction field when constructing turns from config structs; refactors that renamed the constants.","solutions":["Set direction explicitly on every turn using the paddingClientToServer/paddingServerToClient constants","Build schedules with a constructor that alternates directions automatically","Run validatePaddingSchedule in unit tests for every schedule your code ships"],"exampleFix":"// before\nturn := paddingTurn{minLength: 200, maxLength: 400} // direction unset (0)\n\n// after\nturn := paddingTurn{direction: paddingClientToServer, minLength: 200, maxLength: 400}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func validPaddingDirection(d paddingDirection) bool {\n    return d == paddingClientToServer || d == paddingServerToClient\n}","tryCatchPattern":null,"preventionTips":["Always set direction explicitly using the package constants","Generate alternating directions with a builder helper"],"tags":["go","padding","validation","schedule"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}