-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor cleanup logic and improve argument validation in AsusFanControl #6
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
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 |
|---|---|---|
|
|
@@ -7,16 +7,27 @@ | |
|
|
||
| namespace AsusFanControl | ||
| { | ||
| public class AsusControl | ||
| public class AsusControl : IDisposable | ||
| { | ||
| public AsusControl() | ||
| { | ||
| AsusWinIO64.InitializeWinIo(); | ||
| } | ||
|
|
||
| ~AsusControl() | ||
| public void Dispose() | ||
| { | ||
| AsusWinIO64.ShutdownWinIo(); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| public void ResetToDefault() | ||
| { | ||
| var fanCount = AsusWinIO64.HealthyTable_FanCounts(); | ||
| for (byte fanIndex = 0; fanIndex < fanCount; fanIndex++) | ||
| { | ||
| SetFanSpeed(0, fanIndex); | ||
| System.Threading.Thread.Sleep(20); | ||
| } | ||
|
Comment on lines
+23
to
+30
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. 🧩 Analysis chain🏁 Script executed: # First, let's find and examine the AsusControl.cs file
find . -name "AsusControl.cs" -type fRepository: beenycool/AsusFanControl Length of output: 99 🏁 Script executed: # Search for the HealthyTable_FanCounts method definition
rg "HealthyTable_FanCounts" -A 5 -B 2Repository: beenycool/AsusFanControl Length of output: 3445 🏁 Script executed: # Search for the SetFanSpeed method signature
rg "SetFanSpeed" -B 2 -A 10Repository: beenycool/AsusFanControl Length of output: 6332 🏁 Script executed: # Check for any other uses of byte loop counters in the codebase
rg "for\s*\(\s*byte\s+" -A 3Repository: beenycool/AsusFanControl Length of output: 953 Use an
🛠️ Suggested change public void ResetToDefault()
{
var fanCount = AsusWinIO64.HealthyTable_FanCounts();
- for (byte fanIndex = 0; fanIndex < fanCount; fanIndex++)
+ for (int fanIndex = 0; fanIndex < fanCount; fanIndex++)
{
- SetFanSpeed(0, fanIndex);
+ SetFanSpeed(0, (byte)fanIndex);
System.Threading.Thread.Sleep(20);
}
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| public void SetFanSpeed(byte value, byte fanIndex = 0) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,66 +23,103 @@ static int Main(string[] args) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var asusControl = new AsusControl(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool skipResetOnExit = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var arg in args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AppDomain.CurrentDomain.ProcessExit += (sender, e) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-fan-speeds")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!skipResetOnExit) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSpeeds = asusControl.GetFanSpeeds(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Current fan speeds: {string.Join(" ", fanSpeeds)} RPM"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.ResetToDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
33
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. The To safely call if (!skipResetOnExit)
{
asusControl.ResetToDefault();
asusControl.Dispose();
} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--set-fan-speeds")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var arg in args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var newSpeedStr = arg.Split('=')[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var newSpeed = int.Parse(newSpeedStr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.SetFanSpeeds(newSpeed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-fan-speeds")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSpeeds = asusControl.GetFanSpeeds(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Current fan speeds: {string.Join(" ", fanSpeeds)} RPM"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(newSpeed == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine("Test mode turned off"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"New fan speeds: {newSpeed}%"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--set-fan-speeds")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var newSpeedStr = arg.Split('=')[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var newSpeed = int.Parse(newSpeedStr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.SetFanSpeeds(newSpeed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-fan-speed=")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanIds = arg.Split('=')[1].Split(','); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var fanIdStr in fanIds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (newSpeed == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine("Test mode turned off"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"New fan speeds: {newSpeed}%"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+55
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. For robustness and consistency with other argument parsing improvements in this PR, you should use if (int.TryParse(newSpeedStr, out int newSpeed) && newSpeed >= 0 && newSpeed <= 100)
{
asusControl.SetFanSpeeds(newSpeed);
if (newSpeed == 0)
Console.WriteLine("Test mode turned off");
else
Console.WriteLine($"New fan speeds: {newSpeed}%");
}
else
{
Console.WriteLine($"Error: Invalid fan speed value. Must be a percentage between 0 and 100. Value: '{newSpeedStr}'");
} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+56
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. 🧩 Analysis chain🏁 Script executed: find . -name "Program.cs" -type f | head -20Repository: beenycool/AsusFanControl Length of output: 126 🏁 Script executed: wc -l ./AsusFanControl/Program.csRepository: beenycool/AsusFanControl Length of output: 99 🏁 Script executed: cat -n ./AsusFanControl/Program.csRepository: beenycool/AsusFanControl Length of output: 6086 🏁 Script executed: find . -name "*.cs" -type f | xargs grep -l "SetFanSpeeds" | head -10Repository: beenycool/AsusFanControl Length of output: 156 🏁 Script executed: grep -n "SetFanSpeeds" ./AsusFanControl/AsusControl.csRepository: beenycool/AsusFanControl Length of output: 206 🏁 Script executed: sed -n '46,65p' ./AsusFanControl/AsusControl.csRepository: beenycool/AsusFanControl Length of output: 722 🏁 Script executed: # Verify the wrapping behavior with a quick test
cat > /tmp/test_cast.cs << 'EOF'
using System;
class Program {
static void Main() {
// Test wrapping behavior
int[] testPercents = { -1, 0, 50, 100, 101, 200, 256, 300, 1000 };
foreach (var percent in testPercents) {
var value = (byte)(percent / 100.0f * 255);
Console.WriteLine($"percent={percent} -> (int)formula={(int)(percent / 100.0f * 255)} -> byte={value}");
}
}
}
EOF
csc /tmp/test_cast.cs && mono /tmp/test_cast.exe 2>/dev/null || dotnet /tmp/test_cast.cs 2>/dev/null || echo "C# compiler not available, but cast behavior is standard"Repository: beenycool/AsusFanControl Length of output: 184 Harden The code has two vulnerabilities: 🐛 Proposed fix- if (arg.StartsWith("--set-fan-speeds"))
+ if (arg.StartsWith("--set-fan-speeds"))
{
- var newSpeedStr = arg.Split('=')[1];
- var newSpeed = int.Parse(newSpeedStr);
+ var parts = arg.Split('=', 2);
+ if (parts.Length != 2 || !int.TryParse(parts[1], out var newSpeed) || newSpeed < 0 || newSpeed > 100)
+ {
+ Console.WriteLine("Error: --set-fan-speeds expects a value between 0 and 100");
+ continue;
+ }
asusControl.SetFanSpeeds(newSpeed);
if (newSpeed == 0)
Console.WriteLine("Test mode turned off");
else
Console.WriteLine($"New fan speeds: {newSpeed}%");
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-fan-speed=")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanId = int.Parse(fanIdStr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSpeed = asusControl.GetFanSpeed((byte)fanId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Current fan speed for fan {fanId}: {fanSpeed} RPM"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanIds = arg.Split('=')[1].Split(','); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var fanIdStr in fanIds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (int.TryParse(fanIdStr, out int fanId)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fanId >= 0 && fanId <= 255) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSpeed = asusControl.GetFanSpeed((byte)fanId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Current fan speed for fan {fanId}: {fanSpeed} RPM"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Error: fan id must be between 0 and 255: {fanId}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-fan-count")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanCount = asusControl.HealthyTable_FanCounts(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Fan count: {fanCount}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-fan-count")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanCount = asusControl.HealthyTable_FanCounts(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Fan count: {fanCount}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--set-fan-speed=")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSettings = arg.Split('=')[1].Split(','); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var fanSetting in fanSettings) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--set-fan-speed=")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanId = int.Parse(fanSetting.Split(':')[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSpeed = int.Parse(fanSetting.Split(':')[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.SetFanSpeed(fanSpeed, (byte)fanId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSettings = arg.Split('=')[1].Split(','); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var fanSetting in fanSettings) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var settingParts = fanSetting.Split(':'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (settingParts.Length == 2 && int.TryParse(settingParts[0], out int fanId)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fanId >= 0 && fanId <= 255) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fanSpeed = int.Parse(settingParts[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.SetFanSpeed(fanSpeed, (byte)fanId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fanSpeed == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Test mode turned off for fan {fanId}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"New fan speed for fan {fanId}: {fanSpeed}%"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fanSpeed == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Test mode turned off for fan {fanId}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"New fan speed for fan {fanId}: {fanSpeed}%"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+100
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. For consistency and robustness, you should use if (int.TryParse(settingParts[1], out int fanSpeed) && fanSpeed >= 0 && fanSpeed <= 100)
{
asusControl.SetFanSpeed(fanSpeed, (byte)fanId);
if (fanSpeed == 0)
Console.WriteLine($"Test mode turned off for fan {fanId}");
else
Console.WriteLine($"New fan speed for fan {fanId}: {fanSpeed}%");
}
else
{
Console.WriteLine($"Error: Invalid fan speed value for fan {fanId}. Must be a percentage between 0 and 100. Value: '{settingParts[1]}'");
} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Error: fan id must be between 0 and 255: {fanId}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+107
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. 🧩 Analysis chain🏁 Script executed: git ls-files "AsusFanControl/Program.cs"Repository: beenycool/AsusFanControl Length of output: 93 🏁 Script executed: cat -n AsusFanControl/Program.cs | head -150Repository: beenycool/AsusFanControl Length of output: 6086 🏁 Script executed: sed -n '50,115p' AsusFanControl/Program.csRepository: beenycool/AsusFanControl Length of output: 3195 🏁 Script executed: grep -n "255\|100\|fan.*speed\|fan.*count" AsusFanControl/Program.cs | head -20Repository: beenycool/AsusFanControl Length of output: 1592 🏁 Script executed: sed -n '40,120p' AsusFanControl/Program.csRepository: beenycool/AsusFanControl Length of output: 3809 Add explicit error handling and validation for per-fan IDs and speeds. The code has three critical issues:
Add explicit error handling with informative messages and validate both fan IDs (0–255) and speeds (0–100) before proceeding. Proposed fix if (arg.StartsWith("--get-fan-speed="))
{
var fanIds = arg.Split('=')[1].Split(',');
foreach (var fanIdStr in fanIds)
{
- if (int.TryParse(fanIdStr, out int fanId))
- {
- if (fanId >= 0 && fanId <= 255)
- {
- var fanSpeed = asusControl.GetFanSpeed((byte)fanId);
- Console.WriteLine($"Current fan speed for fan {fanId}: {fanSpeed} RPM");
- }
- else
- {
- Console.WriteLine($"Error: fan id must be between 0 and 255: {fanId}");
- }
- }
+ if (!int.TryParse(fanIdStr, out int fanId))
+ {
+ Console.WriteLine($"Error: invalid fan id '{fanIdStr}'");
+ continue;
+ }
+ if (fanId < 0 || fanId > 255)
+ {
+ Console.WriteLine($"Error: fan id must be between 0 and 255: {fanId}");
+ continue;
+ }
+ var fanSpeed = asusControl.GetFanSpeed((byte)fanId);
+ Console.WriteLine($"Current fan speed for fan {fanId}: {fanSpeed} RPM");
}
}
if (arg.StartsWith("--set-fan-speed="))
{
var fanSettings = arg.Split('=')[1].Split(',');
foreach (var fanSetting in fanSettings)
{
var settingParts = fanSetting.Split(':');
- if (settingParts.Length == 2 && int.TryParse(settingParts[0], out int fanId))
+ if (settingParts.Length == 2 &&
+ int.TryParse(settingParts[0], out int fanId) &&
+ int.TryParse(settingParts[1], out int fanSpeed))
{
if (fanId >= 0 && fanId <= 255)
{
- var fanSpeed = int.Parse(settingParts[1]);
- asusControl.SetFanSpeed(fanSpeed, (byte)fanId);
+ if (fanSpeed < 0 || fanSpeed > 100)
+ {
+ Console.WriteLine($"Error: fan speed must be between 0 and 100: {fanSpeed}");
+ continue;
+ }
+ asusControl.SetFanSpeed(fanSpeed, (byte)fanId);
if (fanSpeed == 0)
Console.WriteLine($"Test mode turned off for fan {fanId}");
else
Console.WriteLine($"New fan speed for fan {fanId}: {fanSpeed}%");
}
else
{
Console.WriteLine($"Error: fan id must be between 0 and 255: {fanId}");
}
}
+ else
+ {
+ Console.WriteLine($"Error: invalid fan setting '{fanSetting}' (expected fanId:percent)");
+ }
}
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-cpu-temp")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var cpuTemp = asusControl.Thermal_Read_Cpu_Temperature(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Current CPU temp: {cpuTemp}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (arg.StartsWith("--get-cpu-temp")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var cpuTemp = asusControl.Thermal_Read_Cpu_Temperature(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Console.WriteLine($"Current CPU temp: {cpuTemp}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skipResetOnExit = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.ResetToDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+122
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. 🧩 Analysis chain🏁 Script executed: cat -n AsusFanControl/Program.cs | sed -n '100,140p'Repository: beenycool/AsusFanControl Length of output: 1200 🏁 Script executed: cat -n AsusFanControl/Program.cs | sed -n '1,130p'Repository: beenycool/AsusFanControl Length of output: 6086 🏁 Script executed: find . -name "AsusControl.cs" -type fRepository: beenycool/AsusFanControl Length of output: 99 🏁 Script executed: cat -n AsusFanControl/AsusControl.csRepository: beenycool/AsusFanControl Length of output: 3302 Ensure If 🛠️ Suggested change finally
{
skipResetOnExit = true;
- asusControl.ResetToDefault();
- asusControl.Dispose();
+ try
+ {
+ asusControl.ResetToDefault();
+ }
+ finally
+ {
+ asusControl.Dispose();
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -60,7 +60,24 @@ private void updateUIState() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void OnProcessExit(object sender, EventArgs e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Properties.Settings.Default.turnOffControlOnExit) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.SetFanSpeeds(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.ResetToDefault(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| catch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Ignore exceptions during shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (asusControl != null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl.Dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| asusControl = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
62
to
+80
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. 🧩 Analysis chain🏁 Script executed: cd AsusFanControlGUI && head -150 Form1.cs | tail -100Repository: beenycool/AsusFanControl Length of output: 3120 🏁 Script executed: find . -name "Form1.cs" -type fRepository: beenycool/AsusFanControl Length of output: 96 🏁 Script executed: sed -n '55,85p' AsusFanControlGUI/Form1.csRepository: beenycool/AsusFanControl Length of output: 939 🏁 Script executed: rg "OnProcessExit" -A 30 AsusFanControlGUI/Form1.csRepository: beenycool/AsusFanControl Length of output: 2455 🏁 Script executed: rg "asusControl" -B 2 -A 2 AsusFanControlGUI/Form1.cs | head -100Repository: beenycool/AsusFanControl Length of output: 1419 🏁 Script executed: find . -name "AsusControl.cs" -type fRepository: beenycool/AsusFanControl Length of output: 99 🏁 Script executed: rg "class AsusControl" -A 50 AsusFanControlGUI/AsusControl.cs | head -100Repository: beenycool/AsusFanControl Length of output: 140 🏁 Script executed: rg "Dispose" -B 2 -A 10 AsusFanControlGUI/AsusControl.csRepository: beenycool/AsusFanControl Length of output: 140 🏁 Script executed: rg "override.*Dispose|Dispose\(" AsusFanControlGUI/Form1.csRepository: beenycool/AsusFanControl Length of output: 114 🏁 Script executed: cat AsusFanControl/AsusControl.csRepository: beenycool/AsusFanControl Length of output: 2651 🏁 Script executed: rg "Dispose|IDisposable|WinIo" AsusFanControl/AsusControl.cs -B 2 -A 8Repository: beenycool/AsusFanControl Length of output: 562 Ensure Disposal is inside the 🐛 Proposed fix private void OnProcessExit(object sender, EventArgs e)
{
- if (Properties.Settings.Default.turnOffControlOnExit)
- {
- try
- {
- asusControl.ResetToDefault();
- }
- catch
- {
- // Ignore exceptions during shutdown
- }
- finally
- {
- if (asusControl != null)
- {
- asusControl.Dispose();
- asusControl = null;
- }
- }
- }
+ try
+ {
+ if (Properties.Settings.Default.turnOffControlOnExit)
+ {
+ asusControl?.ResetToDefault();
+ }
+ }
+ catch
+ {
+ // Ignore exceptions during shutdown
+ }
+ finally
+ {
+ asusControl?.Dispose();
+ asusControl = null;
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void Form1_Load(object sender, EventArgs e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Implementing
IDisposableis a good step. However, the current implementation has removed the finalizer (~AsusControl), which acted as a safety net for resource cleanup. IfDispose()isn't called,ShutdownWinIo()will not be called, leading to a resource leak. Also,GC.SuppressFinalize(this)is ineffective without a finalizer.It's best practice to implement the full dispose pattern for classes that wrap unmanaged resources. This makes your class more robust by making
Dispose()idempotent (safe to call multiple times) and ensuring cleanup even ifDispose()is not called explicitly.Here's an example of how you could implement it: