mirror of
https://github.com/ppy/osu.git
synced 2024-11-06 06:57:39 +08:00
Merge pull request #15745 from bdach/settings-number-box-stack-overflow
Fix crashes on trying to play back replays of seeded mods with seed value over 1 billion
This commit is contained in:
commit
fd4d5e98a7
83
osu.Game.Tests/Visual/Settings/TestSceneSettingsNumberBox.cs
Normal file
83
osu.Game.Tests/Visual/Settings/TestSceneSettingsNumberBox.cs
Normal file
@ -0,0 +1,83 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Testing;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Overlays.Settings;
|
||||
|
||||
namespace osu.Game.Tests.Visual.Settings
|
||||
{
|
||||
public class TestSceneSettingsNumberBox : OsuTestScene
|
||||
{
|
||||
private SettingsNumberBox numberBox;
|
||||
private OsuTextBox textBox;
|
||||
|
||||
[SetUpSteps]
|
||||
public void SetUpSteps()
|
||||
{
|
||||
AddStep("create number box", () => Child = numberBox = new SettingsNumberBox());
|
||||
AddStep("get inner text box", () => textBox = numberBox.ChildrenOfType<OsuTextBox>().Single());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLargeInteger()
|
||||
{
|
||||
AddStep("set current to 1,000,000,000", () => numberBox.Current.Value = 1_000_000_000);
|
||||
AddAssert("text box text is correct", () => textBox.Text == "1000000000");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestUserInput()
|
||||
{
|
||||
inputText("42");
|
||||
currentValueIs(42);
|
||||
currentTextIs("42");
|
||||
|
||||
inputText(string.Empty);
|
||||
currentValueIs(null);
|
||||
currentTextIs(string.Empty);
|
||||
|
||||
inputText("555");
|
||||
currentValueIs(555);
|
||||
currentTextIs("555");
|
||||
|
||||
inputText("-4444");
|
||||
// attempting to input the minus will raise an input error, the rest will pass through fine.
|
||||
currentValueIs(4444);
|
||||
currentTextIs("4444");
|
||||
|
||||
// checking the upper bound.
|
||||
inputText(int.MaxValue.ToString());
|
||||
currentValueIs(int.MaxValue);
|
||||
currentTextIs(int.MaxValue.ToString());
|
||||
|
||||
inputText(smallestOverflowValue.ToString());
|
||||
currentValueIs(int.MaxValue);
|
||||
currentTextIs(int.MaxValue.ToString());
|
||||
|
||||
inputText("0");
|
||||
currentValueIs(0);
|
||||
currentTextIs("0");
|
||||
|
||||
// checking that leading zeroes are stripped.
|
||||
inputText("00");
|
||||
currentValueIs(0);
|
||||
currentTextIs("0");
|
||||
|
||||
inputText("01");
|
||||
currentValueIs(1);
|
||||
currentTextIs("1");
|
||||
}
|
||||
|
||||
private void inputText(string text) => AddStep($"set textbox text to {text}", () => textBox.Text = text);
|
||||
private void currentValueIs(int? value) => AddAssert($"current value is {value?.ToString() ?? "null"}", () => numberBox.Current.Value == value);
|
||||
private void currentTextIs(string value) => AddAssert($"current text is {value}", () => textBox.Text == value);
|
||||
|
||||
/// <summary>
|
||||
/// The smallest number that overflows <see langword="int"/>.
|
||||
/// </summary>
|
||||
private static long smallestOverflowValue => 1L + int.MaxValue;
|
||||
}
|
||||
}
|
@ -35,7 +35,6 @@ namespace osu.Game.Overlays.Settings
|
||||
{
|
||||
numberBox = new OutlinedNumberBox
|
||||
{
|
||||
LengthLimit = 9, // limited to less than a value that could overflow int32 backing.
|
||||
Margin = new MarginPadding { Top = 5 },
|
||||
RelativeSizeAxes = Axes.X,
|
||||
CommitOnFocusLost = true
|
||||
@ -44,12 +43,19 @@ namespace osu.Game.Overlays.Settings
|
||||
|
||||
numberBox.Current.BindValueChanged(e =>
|
||||
{
|
||||
int? value = null;
|
||||
if (string.IsNullOrEmpty(e.NewValue))
|
||||
{
|
||||
Current.Value = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (int.TryParse(e.NewValue, out int intVal))
|
||||
value = intVal;
|
||||
Current.Value = intVal;
|
||||
else
|
||||
numberBox.NotifyInputError();
|
||||
|
||||
current.Value = value;
|
||||
// trigger Current again to either restore the previous text box value, or to reformat the new value via .ToString().
|
||||
Current.TriggerChange();
|
||||
});
|
||||
|
||||
Current.BindValueChanged(e =>
|
||||
@ -62,6 +68,8 @@ namespace osu.Game.Overlays.Settings
|
||||
private class OutlinedNumberBox : OutlinedTextBox
|
||||
{
|
||||
protected override bool CanAddCharacter(char character) => char.IsNumber(character);
|
||||
|
||||
public new void NotifyInputError() => base.NotifyInputError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user