Hum, I sent it two weeks ago

I.  Removed useless 'buf[sizeof(buf) - 1] = 0;', fgets()
    already does that

II. Ensure file descriptors are closed before 'goto error'

III.We don't need to nullcheck before freeing a buf, as it's
    already done by free()
--- tempget.c	2012-12-01 09:55:42.451612136 +0100
+++ tempget.c	2012-12-01 09:55:32.426779004 +0100
@@ -376,6 +376,7 @@
    int ret = 0;
    int temp = 0;
    char buf[4096];
+   char *s = NULL;
 #ifdef __FreeBSD__
    size_t len;
    size_t ftemp = 0;
@@ -422,14 +423,14 @@
           {
              char dummy[4096];
 
-             if (fgets(buf, sizeof(buf), f) == NULL) goto error;
+             s = fgets(buf, sizeof(buf), f);
+             fclose(f);
+             if (!s) goto error;
 
-             buf[sizeof(buf) - 1] = 0;
              if (sscanf(buf, "%s %s %i", dummy, dummy, &temp) == 3)
                ret = 1;
              else
                goto error;
-             fclose(f);
           }
         else
           goto error;
@@ -440,10 +441,10 @@
         f = fopen(sensor_path, "rb");
         if (f)
           {
-             if (fgets(buf, sizeof(buf), f) == NULL) goto error;
-
+             s = fgets(buf, sizeof(buf), f);
              fclose(f);
-             buf[sizeof(buf) - 1] = 0;
+             if (!s) goto error;
+
              if (sscanf(buf, "%i", &temp) == 1)
                ret = 1;
              else
@@ -459,9 +460,9 @@
         f = fopen(sensor_path, "r");
         if (f)
           {
-             if (fgets(buf, sizeof(buf), f) == NULL) goto error;
-
-             buf[sizeof(buf) - 1] = 0;
+             s = fgets(buf, sizeof(buf), f);
+             fclose(f);
+             if (!s) goto error;
 
              /* actually read the temp */
              if (sscanf(buf, "%i", &temp) == 1)
@@ -470,7 +471,6 @@
                goto error;
              /* Hack for temp */
              temp = temp / 1000;
-             fclose(f);
           }
         else
           goto error;
@@ -480,9 +480,9 @@
         f = fopen(sensor_path, "r");
         if (f)
           {
-             if (fgets(buf, sizeof(buf), f) == NULL) goto error;
-
-             buf[sizeof(buf) - 1] = 0;
+             s = fgets(buf, sizeof(buf), f);
+             fclose(f);
+             if (!s) goto error;
 
              /* actually read the temp */
              if (sscanf(buf, "%i", &temp) == 1)
@@ -491,7 +491,6 @@
                goto error;
              /* Hack for temp */
              temp = temp / 1000;
-             fclose(f);
           }
         else
           goto error;
@@ -503,10 +502,10 @@
           {
              char *p, *q;
 
-             if (fgets(buf, sizeof(buf), f) == NULL) goto error;
-
-             buf[sizeof(buf) - 1] = 0;
+             s = fgets(buf, sizeof(buf), f);
              fclose(f);
+             if (!s) goto error;
+
              p = strchr(buf, ':');
              if (p)
                {
@@ -529,10 +528,9 @@
         f = fopen(sensor_path, "r");
         if (f)
           {
-             if (fgets(buf, sizeof(buf), f) == NULL) goto error;
-
-             buf[sizeof(buf) - 1] = 0;
+             s = fgets(buf, sizeof(buf), f);
              fclose(f);
+             if (!s) goto error;
              temp = atoi(buf);
              temp /= 1000;
              ret = 1;
@@ -550,9 +548,9 @@
    return -999;
 error:
    sensor_type = SENSOR_TYPE_NONE;
-   if (sensor_name) free(sensor_name);
+   free(sensor_name);
    sensor_name = NULL;
-   if (sensor_path) free(sensor_path);
+   free(sensor_path);
    sensor_path = NULL;
    return -999;
 }

------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to