|
1 | 1 | package checks; |
2 | 2 |
|
3 | | -import play.Logger; |
4 | | - |
| 3 | +import io.vavr.control.Try; |
| 4 | +import java.io.ByteArrayOutputStream; |
5 | 5 | import java.io.IOException; |
| 6 | +import java.io.OutputStream; |
| 7 | +import java.io.Writer; |
6 | 8 | import java.net.InetAddress; |
| 9 | +import java.util.function.Consumer; |
7 | 10 | import javax.naming.NamingException; |
8 | 11 | import javax.servlet.ServletException; |
9 | 12 | import javax.servlet.annotation.WebServlet; |
10 | 13 | import javax.servlet.http.HttpServlet; |
11 | 14 | import javax.servlet.http.HttpServletRequest; |
12 | 15 | import javax.servlet.http.HttpServletResponse; |
13 | | -import java.util.function.Consumer; |
14 | | -import io.vavr.control.Try; |
| 16 | +import play.Logger; |
| 17 | + |
| 18 | +// @formatter:off |
15 | 19 |
|
16 | 20 | // http://localhost:9090/securityapp/s1989/noncompliantvavr |
17 | 21 | @WebServlet(urlPatterns = "/s1989/noncompliantvavr") |
@@ -146,3 +150,187 @@ protected void doPut(jakarta.servlet.http.HttpServletRequest request, jakarta.se |
146 | 150 | } |
147 | 151 | } |
148 | 152 | } |
| 153 | + |
| 154 | +// @formatter:on |
| 155 | + |
| 156 | +class ShouldNotRaiseIfOuterTryCatchesException extends HttpServlet { |
| 157 | + @Override |
| 158 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) { |
| 159 | + try (Writer writer = response.getWriter()) { |
| 160 | + try { |
| 161 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 162 | + } |
| 163 | + |
| 164 | + // This is a regression test: |
| 165 | + // There used to be an FP here because the handling of the inner catch erased the information about the outer one. |
| 166 | + // There is no issue here, because the IOException thrown by the method below is caught by the outer try-catch |
| 167 | + writer.write("Just writing stuff."); // Compliant |
| 168 | + } catch (IOException e) { |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +class ShouldHandleNestedTryCatchConstructs { |
| 174 | + static class ShouldDetectUncaughtExceptionInDoublyNestedTryCatch extends HttpServlet { |
| 175 | + @Override |
| 176 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 177 | + try { |
| 178 | + try { |
| 179 | + throwIOException(); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}} |
| 180 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 181 | + } |
| 182 | + } catch (IllegalArgumentException e) { |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + static class ShouldNotRaiseForCaughtExceptionInDoublyNestedTryCatch extends HttpServlet { |
| 188 | + @Override |
| 189 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) { |
| 190 | + try { |
| 191 | + try { |
| 192 | + throwIOException(); // Compliant |
| 193 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 194 | + } |
| 195 | + } catch (IOException e) { |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) { |
| 201 | + try { |
| 202 | + try { |
| 203 | + throwIOException(); // Compliant |
| 204 | + } catch (IOException e) { |
| 205 | + } |
| 206 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + static class ShouldDetectUncaughtExceptionIfTryCatchIsOnLowerLevel extends HttpServlet { |
| 212 | + @Override |
| 213 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 214 | + try { |
| 215 | + throwIOException(); // Compliant |
| 216 | + } catch (IOException e) { |
| 217 | + } |
| 218 | + |
| 219 | + throwIOException(); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}} |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 224 | + throwIOException(); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}} |
| 225 | + |
| 226 | + try { |
| 227 | + throwIOException(); // Compliant |
| 228 | + } catch (IOException e) { |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 234 | + try { |
| 235 | + throwIOException(); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}} |
| 236 | + |
| 237 | + try { |
| 238 | + throwIOException(); // Compliant |
| 239 | + } catch (IOException e) { |
| 240 | + } |
| 241 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 242 | + |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + static class ShouldDetectForMixedExceptions extends HttpServlet { |
| 248 | + @Override |
| 249 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 250 | + try { |
| 251 | + try { |
| 252 | + throwIOException(); // Compliant: Caught by inner try-catch |
| 253 | + throwServletException(); // Compliant: Caught by outer try-catch |
| 254 | + } catch (IOException e) { |
| 255 | + |
| 256 | + } |
| 257 | + |
| 258 | + throwIOException(); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}} |
| 259 | + } catch (ServletException e) { |
| 260 | + |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + static class ShouldDetectAcrossTryWithResources extends HttpServlet { |
| 266 | + @Override |
| 267 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 268 | + try { |
| 269 | + throwServletException(); |
| 270 | + try (OutputStream stream = new ByteArrayOutputStream()) { |
| 271 | + stream.write(42); // Noncompliant {{Handle the following exception that could be thrown by "write": IOException.}} |
| 272 | + } |
| 273 | + } catch (ServletException e) { |
| 274 | + |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + @Override |
| 279 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) { |
| 280 | + try { |
| 281 | + try (OutputStream stream = new ByteArrayOutputStream()) { |
| 282 | + stream.write(42); // Compliant: Caught by outer try-catch |
| 283 | + } |
| 284 | + } catch (IOException e) { |
| 285 | + |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 291 | + try (OutputStream stream = new ByteArrayOutputStream()) { |
| 292 | + try { |
| 293 | + stream.write(42); // Compliant |
| 294 | + } catch (IOException e) { |
| 295 | + |
| 296 | + } |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + private static void throwIOException() throws IOException { |
| 302 | + throw new IOException(); |
| 303 | + } |
| 304 | + |
| 305 | + private static void throwServletException() throws ServletException { |
| 306 | + throw new ServletException(); |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +class ShouldHandleMultipleCatchBlocksInSeries extends HttpServlet { |
| 311 | + @Override |
| 312 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) { |
| 313 | + try { |
| 314 | + throwIOException(); // Compliant: Caught by second catch block |
| 315 | + } catch (IllegalArgumentException e) { |
| 316 | + |
| 317 | + } catch (IOException e) { |
| 318 | + |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 324 | + try { |
| 325 | + throwIOException(); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}} |
| 326 | + } catch (IllegalArgumentException e) { |
| 327 | + |
| 328 | + } catch (IllegalStateException e) { |
| 329 | + |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + private static void throwIOException() throws IOException { |
| 334 | + throw new IOException(); |
| 335 | + } |
| 336 | +} |
0 commit comments