Skip to content

Commit d3a2bac

Browse files
committed
Validate FastSort input bounds
1 parent e7bcd4e commit d3a2bac

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

src/main/java/algorithms/sprint3/FastSort.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
public class FastSort {
1010

11+
private static final int MAX_PARTICIPANTS = 100_000;
12+
private static final int MAX_LOGIN_LENGTH = 256;
13+
1114
/*
1215
Принцип работы алгоритма:
1316
1) Считываем n участников в массив.
@@ -140,51 +143,64 @@ private int read() throws IOException {
140143
return buffer[ptr++];
141144
}
142145

143-
int nextInt() throws IOException {
144-
int c;
145-
do {
146-
c = read();
147-
if (c == -1) {
148-
throw new EOFException();
149-
}
150-
} while (c <= ' ');
151-
146+
int nextInt(String fieldName, int minValue, int maxValue) throws IOException {
147+
int c = nextNonWhitespace();
152148
int value = 0;
149+
153150
while (c > ' ') {
154-
value = value * 10 + c - '0';
151+
if (c < '0' || c > '9') {
152+
throw new IOException("Invalid integer token for " + fieldName);
153+
}
154+
int digit = c - '0';
155+
if (value > (maxValue - digit) / 10) {
156+
throw new IOException("Integer token for " + fieldName + " exceeds " + maxValue);
157+
}
158+
value = value * 10 + digit;
155159
c = read();
156160
}
161+
162+
if (value < minValue) {
163+
throw new IOException("Integer token for " + fieldName + " is below " + minValue);
164+
}
157165
return value;
158166
}
159167

160-
String next() throws IOException {
168+
String next(int maxLength) throws IOException {
169+
int c = nextNonWhitespace();
170+
StringBuilder sb = new StringBuilder(Math.min(maxLength, 16));
171+
172+
while (c > ' ') {
173+
if (sb.length() == maxLength) {
174+
throw new IOException("Token length exceeds " + maxLength);
175+
}
176+
sb.append((char) c);
177+
c = read();
178+
}
179+
return sb.toString();
180+
}
181+
182+
private int nextNonWhitespace() throws IOException {
161183
int c;
162184
do {
163185
c = read();
164186
if (c == -1) {
165187
throw new EOFException();
166188
}
167189
} while (c <= ' ');
168-
169-
StringBuilder sb = new StringBuilder();
170-
while (c > ' ') {
171-
sb.append((char) c);
172-
c = read();
173-
}
174-
return sb.toString();
190+
return c;
175191
}
176192
}
177193

178194
private static void run() throws Exception {
179195
FastIn in = new FastIn(System.in);
180196

181-
int n = in.nextInt(); // n — количество участников
197+
int n = in.nextInt("participant count", 0, MAX_PARTICIPANTS); // n — количество участников
182198
Participant[] participants = new Participant[n];
183199

184200
for (int i = 0; i < n; i++) {
185-
String login = in.next();
186-
int solved = in.nextInt();
187-
int penalty = in.nextInt();
201+
String login = in.next(MAX_LOGIN_LENGTH);
202+
int solved = in.nextInt("solved", 0, Integer.MAX_VALUE);
203+
int penalty = in.nextInt("penalty", 0, Integer.MAX_VALUE);
188204
participants[i] = new Participant(login, solved, penalty);
189205
}
190206

0 commit comments

Comments
 (0)