commit e93038836288c8e138827022be0bbfe217606bc7
parent c25c7d613e75702c0febbea8f3fe83c6d3618c2e
Author: Matthias Jaros <jaros@mailbox.org>
Date: Mon, 16 Feb 2026 17:24:19 +0100
Added checks to prevent illegal port numbers
Diffstat:
1 file changed, 28 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/it_jaros/networkScanner/PortRange.java b/src/main/java/com/it_jaros/networkScanner/PortRange.java
@@ -21,25 +21,50 @@ public class PortRange {
}
String[] splitComma = ports.split(",");
- for (String commaValue: splitComma) {
+ for (String commaValue : splitComma) {
if (commaValue.contains("-")) {
String[] rangeValue = commaValue.split("-");
if (rangeValue.length > 2) {
throw new IllegalArgumentException(String.format("Argument contains too many '-'' %s", commaValue));
}
-
+
int start = Integer.parseInt(rangeValue[0]);
int end = Integer.parseInt(rangeValue[1]);
+ checkValues(start, end);
specifiedPorts.set(start, end + 1);
} else {
+ int port = Integer.parseInt(commaValue);
+ checkValue(port);
specifiedPorts.set(Integer.parseInt(commaValue));
}
}
-
+
availablePorts.or(specifiedPorts);
cursor = availablePorts.nextSetBit(MIN_PORT);
}
+ private void checkValues(int start, int end) {
+ checkValue(start);
+ checkValue(end);
+
+ if (start >= end) {
+ throw new IllegalArgumentException(
+ String.format("Start value cannot be equal or bigger than end value '%s >= %s'", start, end));
+ }
+ }
+
+ private void checkValue(int port) {
+ if (port < MIN_PORT) {
+ throw new IllegalArgumentException(
+ String.format("Start value smaller than allowed range %s < %s", port, MIN_PORT));
+ }
+
+ if (port > MAX_PORT) {
+ throw new IllegalArgumentException(
+ String.format("End value bigger than allowed range %s > %s", port, MAX_PORT));
+ }
+ }
+
public synchronized int next() {
int p = availablePorts.nextSetBit(cursor);
if (p > 0) {