From 07ea6a5e62f958a65755d41d05b6a76fce3317f3 Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Fri, 13 Sep 2024 17:45:23 +0200 Subject: [PATCH] handle unsigned int errorcodes more gracefully --- .../plugins/durabletask/FileMonitoringTask.java | 7 ++++++- .../durabletask/WindowsBatchScriptTest.java | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/durabletask/FileMonitoringTask.java b/src/main/java/org/jenkinsci/plugins/durabletask/FileMonitoringTask.java index b5a85d4d..b19561d8 100644 --- a/src/main/java/org/jenkinsci/plugins/durabletask/FileMonitoringTask.java +++ b/src/main/java/org/jenkinsci/plugins/durabletask/FileMonitoringTask.java @@ -393,7 +393,12 @@ public Integer invoke(File f, VirtualChannel channel) throws IOException, Interr return null; } else { try { - return Integer.valueOf(text); + long value = Long.parseLong(text); + if (value > Integer.MAX_VALUE) { + LOGGER.warning("ErrorCode greater than max integer detected, limited to max value"); + value = Integer.MAX_VALUE; + } + return (Integer) (int) value; } catch (NumberFormatException x) { throw new IOException("corrupted content in " + f + ": " + x, x); } diff --git a/src/test/java/org/jenkinsci/plugins/durabletask/WindowsBatchScriptTest.java b/src/test/java/org/jenkinsci/plugins/durabletask/WindowsBatchScriptTest.java index 4691ca91..bd055852 100644 --- a/src/test/java/org/jenkinsci/plugins/durabletask/WindowsBatchScriptTest.java +++ b/src/test/java/org/jenkinsci/plugins/durabletask/WindowsBatchScriptTest.java @@ -114,6 +114,22 @@ private void testWithPath(String path) throws Exception { c.cleanup(ws); } + @Test public void exitCommandUnsignedInt() throws Exception { + Controller c = new WindowsBatchScript("echo hello world\r\nexit 3221225477").launch(new EnvVars(), ws, launcher, listener); + awaitCompletion(c); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + c.writeLog(ws, baos); + if (enableBinary) { + assertEquals(Integer.valueOf(Integer.MAX_VALUE), c.exitStatus(ws, launcher, listener)); + } else { + assertEquals(Integer.valueOf(-1073741819), c.exitStatus(ws, launcher, listener)); + } + + String log = baos.toString(); + assertTrue(log, log.contains("hello world")); + c.cleanup(ws); + } + @Test public void exitBCommandAfterError() throws Exception { Controller c = new WindowsBatchScript("cmd /c exit 42\r\nexit /b").launch(new EnvVars(), ws, launcher, listener); awaitCompletion(c);