{"record":{"id":"0e46408e91120ad3","repo":"stride3d/stride","slug":"specified-argument-was-out-of-the-range-of-valid-values-0e4640","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'stretchDirection')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'stretchDirection'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.UI/ImageSizeHelper.cs","lineNumber":83,"sourceCode":"                default:\n                    throw new ArgumentOutOfRangeException(nameof(stretchType));\n            }\n\n            // adjust the scales depending on the stretch directions\n            switch (stretchDirection)\n            {\n                case StretchDirection.Both:\n                    break;\n                case StretchDirection.DownOnly:\n                    desiredScale.X = Math.Min(desiredScale.X, 1);\n                    desiredScale.Y = Math.Min(desiredScale.Y, 1);\n                    break;\n                case StretchDirection.UpOnly:\n                    desiredScale.X = Math.Max(1, desiredScale.X);\n                    desiredScale.Y = Math.Max(1, desiredScale.Y);\n                    break;\n                default:\n                    throw new ArgumentOutOfRangeException(nameof(stretchDirection));\n            }\n\n            // update the desired size based on the desired scales\n            desiredSize = new Vector3(idealSize.X * desiredScale.X, idealSize.Y * desiredScale.Y, 0f);\n\n            if (!isMeasuring || !sprite.HasBorders)\n                return desiredSize;\n\n            // consider sprite borders\n            var borderSum = new Vector2(sprite.BordersInternal.X + sprite.BordersInternal.Z, sprite.BordersInternal.Y + sprite.BordersInternal.W);\n            if (sprite.Orientation == ImageOrientation.Rotated90)\n                MemoryUtilities.Swap(ref borderSum.X, ref borderSum.Y);\n\n            return new Vector3(Math.Max(desiredSize.X, borderSum.X), Math.Max(desiredSize.Y, borderSum.Y), desiredSize.Z);\n        }\n    }\n}\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.UI/ImageSizeHelper.cs#L65-L101","documentation":"CalculateImageSizeFromAvailable's stretchDirection switch has a default branch that throws ArgumentOutOfRangeException for any StretchDirection outside DownOnly/UpOnly/Both. This guards against undefined enum values reaching the scaling math.","triggerScenarios":"Passing an out-of-range cast value like (StretchDirection)7, or a default-initialized enum field whose 0 value is not a defined member; serializing/deserializing a bad numeric value.","commonSituations":"Config or XAML-like files storing integer stretch directions; version drift where enum members changed; uninitialized struct properties.","solutions":["Pass a valid StretchDirection (DownOnly, UpOnly, or Both)","Sanitize deserialized values with Enum.IsDefined before use","Default to StretchDirection.Both when the stored value is invalid"],"exampleFix":"// before\nvar dir = (StretchDirection)storedValue;\nvar size = ImageSizeHelper.CalculateImageSizeFromAvailable(avail, ideal, sprite, stretch, dir);\n// after\nvar dir = Enum.IsDefined(typeof(StretchDirection), storedValue) ? (StretchDirection)storedValue : StretchDirection.Both;\nvar size = ImageSizeHelper.CalculateImageSizeFromAvailable(avail, ideal, sprite, stretch, dir);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(StretchDirection), stretchDirection))\n    stretchDirection = StretchDirection.Both;","typeGuard":"static bool IsValidStretchDirection(StretchDirection d) => d == StretchDirection.DownOnly || d == StretchDirection.UpOnly || d == StretchDirection.Both;","tryCatchPattern":"try { size = ImageSizeHelper.CalculateImageSizeFromAvailable(avail, ideal, sprite, stretch, dir); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"stretchDirection\") { size = ImageSizeHelper.CalculateImageSizeFromAvailable(avail, ideal, sprite, stretch, StretchDirection.Both); }","preventionTips":["Validate deserialized enum values before use","Keep enum definitions in sync across library versions","Default to StretchDirection.Both for unknown values"],"tags":["csharp","ui","enum","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}