Le 01/12/2012 09:41, Michael Blumenkrantz a écrit :
> On Sat, 01 Dec 2012 08:01:28 +0100
> rustyBSD <rusty...@gmx.fr> wrote:
> 
>> Hi,
>> just a patch to avoid
>>
>> temperature/tempget.c: In function 'init':
>> temperature/tempget.c:271: warning: passing argument 3 of 'sysctlnametomib' 
>> from incompatible pointer type
>> temperature/tempget.c: In function 'check':
>> temperature/tempget.c:379: warning: passing argument 4 of 'sysctl' from 
>> incompatible pointer type
>>
>> this file should be formatted
>>
>> thanks
> 
> formatting done
> 

We can also fix leaks.


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;
 }
------------------------------------------------------------------------------
Keep yourself connected to Go Parallel: 
INSIGHTS What's next for parallel hardware, programming and related areas?
Interviews and blogs by thought leaders keep you ahead of the curve.
http://goparallel.sourceforge.net
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to