Skip to content

Commit 75e13c6

Browse files
Merge pull request #75 from lifebeyondfife/OverflowProtection
feat(expression): protect overflow on bounds
2 parents 095831d + 8a8b48e commit 75e13c6

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

.vscode/launch.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "coreclr",
1010
"request": "launch",
1111
"preLaunchTask": "build-league-generation",
12-
"program": "${workspaceFolder}/Examples/LeagueGeneration/bin/Debug/net6.0/LeagueGeneration.dll",
12+
"program": "${workspaceFolder}/Examples/LeagueGeneration/bin/Debug/net8.0/LeagueGeneration.dll",
1313
"args": [],
1414
"cwd": "${workspaceFolder}/Examples/LeagueGeneration",
1515
"stopAtEntry": false,
@@ -20,7 +20,7 @@
2020
"type": "coreclr",
2121
"request": "launch",
2222
"preLaunchTask": "build-nqueens",
23-
"program": "${workspaceFolder}/Examples/NQueens/bin/Debug/net6.0/NQueens.dll",
23+
"program": "${workspaceFolder}/Examples/NQueens/bin/Debug/net8.0/NQueens.dll",
2424
"args": [],
2525
"cwd": "${workspaceFolder}/Examples/NQueens",
2626
"stopAtEntry": false,
@@ -31,7 +31,7 @@
3131
"type": "coreclr",
3232
"request": "launch",
3333
"preLaunchTask": "build-optimisation",
34-
"program": "${workspaceFolder}/Examples/Optimisation/bin/Debug/net6.0/Optimisation.dll",
34+
"program": "${workspaceFolder}/Examples/Optimisation/bin/Debug/net8.0/Optimisation.dll",
3535
"args": [],
3636
"cwd": "${workspaceFolder}/Examples/Optimisation",
3737
"stopAtEntry": false,
@@ -42,7 +42,7 @@
4242
"type": "coreclr",
4343
"request": "launch",
4444
"preLaunchTask": "build-phase-locked-loop",
45-
"program": "${workspaceFolder}/Examples/PhaseLockedLoop/bin/Debug/net6.0/PhaseLockedLoop.dll",
45+
"program": "${workspaceFolder}/Examples/PhaseLockedLoop/bin/Debug/net8.0/PhaseLockedLoop.dll",
4646
"args": [],
4747
"cwd": "${workspaceFolder}/Examples/PhaseLockedLoop",
4848
"stopAtEntry": false,
@@ -53,7 +53,7 @@
5353
"type": "coreclr",
5454
"request": "launch",
5555
"preLaunchTask": "build-send-more-money",
56-
"program": "${workspaceFolder}/Examples/SendMoreMoney/bin/Debug/net6.0/SendMoreMoney.dll",
56+
"program": "${workspaceFolder}/Examples/SendMoreMoney/bin/Debug/net8.0/SendMoreMoney.dll",
5757
"args": [],
5858
"cwd": "${workspaceFolder}/Examples/SendMoreMoney",
5959
"console": "internalConsole",
@@ -64,7 +64,7 @@
6464
"type": "coreclr",
6565
"request": "launch",
6666
"preLaunchTask": "build-teacher-timetable",
67-
"program": "${workspaceFolder}/Examples/TeacherTimetable/bin/Debug/net6.0/TeacherTimetable.dll",
67+
"program": "${workspaceFolder}/Examples/TeacherTimetable/bin/Debug/net8.0/TeacherTimetable.dll",
6868
"args": [],
6969
"cwd": "${workspaceFolder}/Examples/TeacherTimetable",
7070
"stopAtEntry": false,

Csp/Integer/ExpressionInteger.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class ExpressionInteger : Expression<int>
2323

2424
return new Bounds<int>
2525
(
26-
leftBounds.LowerBound + rightBounds.LowerBound,
27-
leftBounds.UpperBound + rightBounds.UpperBound
26+
(int) Math.Max(int.MinValue, Math.Min(int.MaxValue, (long)leftBounds.LowerBound + rightBounds.LowerBound)),
27+
(int) Math.Min(int.MaxValue, Math.Max(int.MinValue, (long)leftBounds.UpperBound + rightBounds.UpperBound))
2828
);
2929
},
3030
propagator = (first, second, enforce) =>
@@ -74,8 +74,8 @@ public class ExpressionInteger : Expression<int>
7474

7575
return new Bounds<int>
7676
(
77-
leftBounds.LowerBound - rightBounds.UpperBound,
78-
leftBounds.UpperBound - rightBounds.LowerBound
77+
(int) Math.Max(int.MinValue, Math.Min(int.MaxValue, (long)leftBounds.LowerBound - rightBounds.UpperBound)),
78+
(int) Math.Min(int.MaxValue, Math.Max(int.MinValue, (long)leftBounds.UpperBound - rightBounds.LowerBound))
7979
);
8080
},
8181
propagator = (first, second, enforce) =>
@@ -142,8 +142,8 @@ public class ExpressionInteger : Expression<int>
142142

143143
return new Bounds<int>
144144
(
145-
leftBounds.LowerBound / rightBounds.UpperBound,
146-
leftBounds.UpperBound / rightBounds.LowerBound
145+
rightBounds.UpperBound == 0 ? (leftBounds.LowerBound >= 0 ? int.MaxValue : int.MinValue) : leftBounds.LowerBound / rightBounds.UpperBound,
146+
rightBounds.LowerBound == 0 ? (leftBounds.UpperBound >= 0 ? int.MaxValue : int.MinValue) : leftBounds.UpperBound / rightBounds.LowerBound
147147
);
148148
},
149149
propagator = (first, second, enforce) =>
@@ -199,8 +199,8 @@ public class ExpressionInteger : Expression<int>
199199

200200
return new Bounds<int>
201201
(
202-
leftBounds.LowerBound * rightBounds.LowerBound,
203-
leftBounds.UpperBound * rightBounds.UpperBound
202+
(leftBounds.LowerBound == 0 || rightBounds.LowerBound == 0) ? 0 : ((leftBounds.LowerBound < 0) != (rightBounds.LowerBound < 0) && Math.Abs((long)leftBounds.LowerBound) > int.MaxValue / Math.Abs((long)rightBounds.LowerBound)) ? int.MinValue : leftBounds.LowerBound * rightBounds.LowerBound,
203+
(leftBounds.UpperBound > 0 && rightBounds.UpperBound > 0 && leftBounds.UpperBound > int.MaxValue / rightBounds.UpperBound) ? int.MaxValue : leftBounds.UpperBound * rightBounds.UpperBound
204204
);
205205
},
206206
propagator = (first, second, enforce) =>

0 commit comments

Comments
 (0)