Show
Ignore:
Timestamp:
03/08/04 05:39:50 (11 years ago)
Author:
mmh
Message:

Patch by Philipp Thomas <pth@…>:

Current gcc versions warn when type punning is used, as this breaks the
strict aliasing analysis gcc 3.3 does by default. When checking the causes
for these warnings I discovered that sensors_malloc_array,
sensors_free_array, sensors_add_array_el and sensors_add_array_els in
lib/general.c all take a 'void **' for the list. This is plain wrong as
the C standard only allow 'char *' and 'void *' to alias any other type, so
if these functions should take a generic pointer, it *must* be a 'void *'
that's internally cast to a 'void **'.

The attached patch does this and adapts all places where these functions are
called. Note that when done correctly, no cast what-so-ever in the calling
code is needed.

The patch is against current lm_sensors2 cvs version.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/trunk/lib/general.c

    r207 r2345  
    2828#define A_BUNCH 16 
    2929 
    30 void sensors_malloc_array(void **list, int *num_el, int *max_el, int el_size) 
     30void sensors_malloc_array(void *list, int *num_el, int *max_el, int el_size) 
    3131{ 
    32   *list = malloc(el_size*A_BUNCH); 
    33   if (! *list) 
     32  void **my_list = (void **)list; 
     33 
     34  *my_list = malloc(el_size*A_BUNCH); 
     35  if (! *my_list) 
    3436    sensors_fatal_error("sensors_malloc_array","Allocating new elements"); 
    3537  *max_el = A_BUNCH; 
     
    3739} 
    3840 
    39 void sensors_free_array(void **list, int *num_el, int *max_el) 
     41void sensors_free_array(void *list, int *num_el, int *max_el) 
    4042{ 
    41   free(*list); 
    42   *list = NULL; 
     43  void **my_list = (void **)list; 
     44 
     45  free(*my_list); 
     46  *my_list = NULL; 
    4347  *num_el = 0; 
    4448  *max_el = 0; 
    4549} 
    4650 
    47 void sensors_add_array_el(const void *el, void **list, int *num_el, 
     51void sensors_add_array_el(const void *el, void *list, int *num_el, 
    4852                          int *max_el, int el_size) 
    4953{ 
    5054  int new_max_el; 
     55  void **my_list = (void *)list; 
    5156  if (*num_el + 1 > *max_el) { 
    5257    new_max_el = *max_el + A_BUNCH; 
    53     *list = realloc(*list,new_max_el * el_size); 
    54     if (! *list) 
     58    *my_list = realloc(*my_list,new_max_el * el_size); 
     59    if (! *my_list) 
    5560      sensors_fatal_error("sensors_add_array_el","Allocating new elements"); 
    5661    *max_el = new_max_el; 
    5762  } 
    58   memcpy(((char *) *list) + *num_el * el_size, el, el_size); 
     63  memcpy(((char *) *my_list) + *num_el * el_size, el, el_size); 
    5964  (*num_el) ++; 
    6065} 
    6166 
    62 void sensors_add_array_els(const void *els, int nr_els, void **list,  
     67void sensors_add_array_els(const void *els, int nr_els, void *list,  
    6368                           int *num_el, int *max_el, int el_size) 
    6469{ 
    6570  int new_max_el; 
     71  void **my_list = (void *)list; 
    6672  if (*num_el + nr_els > *max_el) { 
    6773    new_max_el = (*max_el + nr_els + A_BUNCH); 
    6874    new_max_el -= new_max_el % A_BUNCH; 
    69     *list = realloc(*list,new_max_el * el_size); 
    70     if (! *list) 
     75    *my_list = realloc(*my_list,new_max_el * el_size); 
     76    if (! *my_list) 
    7177      sensors_fatal_error("sensors_add_array_els","Allocating new elements"); 
    7278    *max_el = new_max_el; 
    7379  } 
    74   memcpy(((char *)*list) + *num_el * el_size, els, el_size * nr_els); 
     80  memcpy(((char *)*my_list) + *num_el * el_size, els, el_size * nr_els); 
    7581  *num_el += nr_els; 
    7682}