github-advanced-security[bot] commented on code in PR #17935:
URL: 
https://github.com/apache/dolphinscheduler/pull/17935#discussion_r2757014114


##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/LogUtils.java:
##########
@@ -169,4 +182,148 @@
         return loggerContext.getProperty("log.base.ctx");
     }
 
+    /**
+     * Get all rolling log files for a given base file path.
+     * Returns a sorted list containing the base file and its rolled versions 
(e.g., .1, .2, etc.)
+     * ordered from newest to oldest (base file first, then .1, .2, etc.)
+     */
+    private static List<File> getRollingLogFiles(String basePath) {
+        List<File> allFiles = new ArrayList<>();
+
+        File baseFile = new File(basePath);
+        File parentDir = baseFile.getParentFile();
+        String fileName = baseFile.getName();
+
+        // Add the base file if it exists
+        if (baseFile.exists()) {
+            allFiles.add(baseFile);
+        }
+
+        // Look for rolling files with pattern: basePath.N
+        if (parentDir != null) {
+            File[] files = parentDir.listFiles((dir, name) -> 
name.startsWith(fileName + ".") &&
+                    Pattern.matches(Pattern.quote(fileName) + "\\.\\d+", 
name));
+
+            if (files != null) {
+                allFiles.addAll(Arrays.asList(files));
+            }
+        }
+
+        // Sort all files in reverse order based on rolling number
+        // Base file (without number) is treated as having number 0, so it 
comes last
+        // descending order (larger numbers first)
+        allFiles.sort((file1, file2) -> {
+            int num1 = isRollingFile(file1) ? extractRollingNumber(file1) : 0;
+            int num2 = isRollingFile(file2) ? extractRollingNumber(file2) : 0;
+            return Integer.compare(num2, num1);
+        });
+
+        return allFiles;
+    }
+
+    /**
+     * Extract the rolling number from a file name (e.g., from "xxx.log.3" 
extract 3)
+     */
+    private static int extractRollingNumber(File file) {
+        String fileName = file.getName();
+        int lastDotIndex = fileName.lastIndexOf('.');
+        if (lastDotIndex != -1 && lastDotIndex < fileName.length() - 1) {
+            try {
+                return Integer.parseInt(fileName.substring(lastDotIndex + 1));
+            } catch (NumberFormatException e) {
+                return Integer.MAX_VALUE; // Put invalid files at the end
+            }
+        }
+        return Integer.MAX_VALUE;
+    }
+
+    /**
+     * Check if the file is a rolling file (has a number suffix like .1, .2, 
etc.)
+     */
+    private static boolean isRollingFile(File file) {
+        String fileName = file.getName();
+        // Check if the filename matches the pattern of a rolling file 
(basename.number)
+        int lastDotIndex = fileName.lastIndexOf('.');
+        if (lastDotIndex != -1 && lastDotIndex < fileName.length() - 1) {
+            String suffix = fileName.substring(lastDotIndex + 1);
+            return suffix.matches("\\d+");
+        }
+        return false;
+    }
+
+    /**
+     * Read lines from multiple rolling log files in order
+     */
+    private static List<String> readFromRollingLogFiles(List<File> logFiles, 
int skipLine, int limit) {
+        List<String> allLines = new ArrayList<>();
+
+        // Read all lines from all log files in order
+        for (File file : logFiles) {
+            log.info("Reading log file: {}", file.getAbsolutePath());
+            try (Stream<String> stream = Files.lines(file.toPath())) {
+                List<String> fileLines = stream.collect(Collectors.toList());
+                allLines.addAll(fileLines);
+            } catch (IOException e) {
+                log.error("Error reading file: " + file.getAbsolutePath(), e);
+                throw new RuntimeException(String.format("Read file: %s 
error", file.getAbsolutePath()), e);
+            }
+        }
+
+        // Apply skip and limit
+        int startIndex = Math.min(skipLine, allLines.size());
+        int endIndex = Math.min(startIndex + limit, allLines.size());

Review Comment:
   ## User-controlled data in arithmetic expression
   
   This arithmetic expression depends on a [user-provided value](1), 
potentially causing an overflow.
   This arithmetic expression depends on a [user-provided value](2), 
potentially causing an overflow.
   
   [Show more 
details](https://github.com/apache/dolphinscheduler/security/code-scanning/5609)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to