-
Notifications
You must be signed in to change notification settings - Fork 133
fix: write openocd debug output to a file #1274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
755a916
de12f51
9cd8d24
39609fc
f39a5ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved. | ||
| * Use is subject to license terms. | ||
| *******************************************************************************/ | ||
| package com.espressif.idf.core.logging; | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.io.Writer; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class LogFileWriterManager | ||
| { | ||
| private static final Map<String, PrintWriter> writers = new ConcurrentHashMap<>(); | ||
|
|
||
| private LogFileWriterManager() | ||
| { | ||
| } | ||
|
|
||
| public static PrintWriter getWriter(String path, boolean append) | ||
| { | ||
| if (path == null || path.isEmpty()) | ||
| { | ||
| return new PrintWriter(Writer.nullWriter()); | ||
| } | ||
|
|
||
| return writers.computeIfAbsent(path, p -> { | ||
| try | ||
| { | ||
| File file = new File(p); | ||
| File parent = file.getParentFile(); | ||
| if (parent != null && !parent.exists()) | ||
| { | ||
| parent.mkdirs(); | ||
| } | ||
| if (!file.exists()) | ||
| { | ||
| file.createNewFile(); | ||
| } | ||
| return new PrintWriter(new BufferedWriter(new FileWriter(file, append)), true); | ||
| } | ||
| catch (IOException e) | ||
| { | ||
| Logger.log(e); | ||
| return new PrintWriter(Writer.nullWriter()); | ||
| } | ||
| }); | ||
| } | ||
|
Comment on lines
+25
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix append mode handling for existing writers. The current implementation ignores the Consider this scenario:
Apply this refactor to include append mode in the key: -return writers.computeIfAbsent(path, p -> {
+String key = path + ":" + append;
+return writers.computeIfAbsent(key, k -> {Also update the -public static void closeWriter(String path)
+public static void closeWriter(String path, boolean append)
{
- if (path == null || path.isEmpty())
- return;
- PrintWriter writer = writers.remove(path);
+ if (path == null || path.isEmpty())
+ return;
+ String key = path + ":" + append;
+ PrintWriter writer = writers.remove(key);
🤖 Prompt for AI AgentsCritical issue: Append parameter is ignored for existing writers. The
This could lead to unexpected behavior where the append mode doesn't match the caller's intention. Consider this approach to ensure append behavior is consistent: -public static PrintWriter getWriter(String path, boolean append)
+public static PrintWriter getWriter(String path, boolean append)
{
if (path == null || path.isEmpty())
{
return new PrintWriter(Writer.nullWriter());
}
- return writers.computeIfAbsent(path, p -> {
+ // Create a composite key that includes append mode
+ String key = path + "|append=" + append;
+ return writers.computeIfAbsent(key, k -> {
try
{
- File file = new File(p);
+ File file = new File(path);
File parent = file.getParentFile();
if (parent != null && !parent.exists())
{
parent.mkdirs();
}
if (!file.exists())
{
file.createNewFile();
}
return new PrintWriter(new BufferedWriter(new FileWriter(file, append)), true);
}
catch (IOException e)
{
Logger.log(e);
return new PrintWriter(Writer.nullWriter());
}
});
}And update the closeWriter method accordingly: public static void closeWriter(String path)
{
if (path == null || path.isEmpty())
return;
- PrintWriter writer = writers.remove(path);
+ // Remove both append modes for the path
+ PrintWriter writer1 = writers.remove(path + "|append=true");
+ PrintWriter writer2 = writers.remove(path + "|append=false");
+ PrintWriter writer = writer1 != null ? writer1 : writer2;
if (writer != null)
{
writer.println("=== Session ended at " + LocalDateTime.now() + " ===");
writer.close();
}
}
🤖 Prompt for AI Agents |
||
|
|
||
| public static void closeWriter(String path) | ||
| { | ||
| if (path == null || path.isEmpty()) | ||
| return; | ||
| PrintWriter writer = writers.remove(path); | ||
| if (writer != null) | ||
| { | ||
| writer.println("=== Session ended at " + LocalDateTime.now() + " ==="); //$NON-NLS-1$ //$NON-NLS-2$ | ||
| writer.close(); | ||
| } | ||
| } | ||
|
|
||
| public static void closeAll() | ||
| { | ||
| for (PrintWriter writer : writers.values()) | ||
| { | ||
| writer.close(); | ||
| } | ||
| writers.clear(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Potential security issue: Directory creation without validation.
The
mkdirs()call could create directories in unintended locations if the path is malicious or contains path traversal sequences (e.g.,../../../etc/passwd).Add path validation before creating directories:
if (parent != null && !parent.exists()) { + // Validate parent path to prevent directory traversal attacks + String canonicalParent = parent.getCanonicalPath(); + String expectedParent = parent.getAbsolutePath(); + if (!canonicalParent.equals(expectedParent)) + { + throw new IOException("Invalid path detected: " + path); + } parent.mkdirs(); }🤖 Prompt for AI Agents