Good new everyone,

I found the bug (or at least the one that made the plugin crash for _me_).
It's in panel-plugin/acpi.c in refresh_acpi().
The (broken) code is at line 475:

case STATE:
        file = g_strdup_printf ("%s/%s/state", ACPI_DIR_FAN, cf->devicename);
        cf->raw_value = strcmp(get_acpi_value(file), "on")==0 ? 1.0 : 0.0;

The problem is, that get_acpi_value() (in contrast to
get_acpi_zone_value() for example) expects a _full_ path to the file.
So the code has to be changed to:

case STATE:
        // get_acpi_value() expects a _full_ path (unlike get_acpi_zone_value() 
etc)!
        file = g_strdup_printf ("%s/%s/%s/state", ACPI_PATH,
ACPI_DIR_FAN, cf->devicename);
        cf->raw_value = strcmp(get_acpi_value(file), "on")==0 ? 1.0 : 0.0;

otherwise get_acpi_value() returns NULL which causes strcmp() to crash.
Also it should be checked first, whether get_acpi_value() has returned
NULL to prevent a crash if (for whatever reason) the fan isn't listed
in /proc/acpi anymore or something like that.

Last but not least strcmp() doesn't work there for me, as
get_acpi_values() returns "on\n" - so it should be replaced by
strncmp().
I've attached a diff for panel-plugin/acpi.c that fixes the crash by
using the right path for the file, checks if get_acpi_value() has
returned NULL (and prevents a possible crash) and uses strncmp to()
compare the extracted content of the file with "on", so we don't have
to care whether the file contained "on" or "on\n"

Cheers,
- Daniel
--- xfce4-sensors-plugin-0.10.99.5.orig/panel-plugin/acpi.c	2008-08-06 01:56:00.063518101 +0200
+++ xfce4-sensors-plugin-0.10.99.5/panel-plugin/acpi.c	2008-08-06 03:11:08.635447284 +0200
@@ -445,7 +445,7 @@
 void
 refresh_acpi (gpointer chip_feature, gpointer data)
 {
-    char *file, *zone;
+    char *file, *zone, *state;
     t_chipfeature *cf;
 
     TRACE ("enters refresh_acpi");
@@ -472,8 +472,18 @@
             break;
 
         case STATE:
-            file = g_strdup_printf ("%s/%s/state", ACPI_DIR_FAN, cf->devicename);
-            cf->raw_value = strcmp(get_acpi_value(file), "on")==0 ? 1.0 : 0.0;
+            // get_acpi_value() expects a _full_ path (unlike get_acpi_zone_value() etc)!
+            file = g_strdup_printf ("%s/%s/%s/state", ACPI_PATH, ACPI_DIR_FAN, cf->devicename);
+            state = get_acpi_value(file);
+            // if get_acpi_value has returned NULL (=> the file hasn't been found)
+            // we display the fan as "off"
+            if(state==NULL){
+                DBG("get_acpi_value has returned NULL!");
+                cf->raw_value = 0.0;
+                break;
+            }
+            // on my box there is a \n after "on".. dunno if that's normal, so i only compare 2 chars
+            cf->raw_value = strncmp(state, "on", 2)==0 ? 1.0 : 0.0;
             g_free (file);
             /* g_free (cf->formatted_value);
             cf->formatted_value = g_strdup_printf (_("%.0f"), cf->raw_value); */

Reply via email to