unnecessary casts

Bug #592223 reported by Miika Komu
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
HIPL
New
Low
Diego Biurrun

Bug Description

DB: There are gazillions of casts, often two in a row. Casts should be
the exception, not the norm. If a large number of casts are
required, then the code structure and design is plain wrong.

Often the results of functions returning void* are cast to some type
even though void* is compatible with any pointer (C is not C++). I
already removed such pointless casts for malloc, memcpy, memset, free
and similar.

But many instances still remain, as an example, take the function
hip_get_param_contents_direct, it returns void*, but its results are
cast all the time, e.g. in lib/conf/conf.c.

MK: Suggest a systematic way to find casts?

---

DB: Another example from lib/opphip/wrap_db.c:

    ret = (hip_opp_socket_t *) hip_ht_find(socketdb, (void *) &opp);

Both casts are unnecessary, this is the prototype for hip_ht_find:

  void *hip_ht_find(void *head, void *data);

My personal guess is that at least two thirds of the casts in HIPL
could be avoided. Often the internal API is designed in a way that
seems to make casting back and forth a necessity. If this is the
case, then the API is badly designed.

---

I forgot the completely redundant casts: The results of functions returning
foo are cast to .. foo!

As a random example, grep for append_to_slist and witness that it returns
'SList *' but the result is cast to 'SList *'...

firewall/conntrack.c:

    connection->reply.esp_tuples = (SList *) append_to_slist((SList *)

Revision history for this message
René Hummen (rene-hummen) wrote :

Diego, can you have a quick look and asses the current situation. I guess casts should be at a level that is manageable.
Please, do _not_ start fixing.

Changed in hipl:
importance: Undecided → Low
assignee: nobody → Diego Biurrun (diego-biurrun)
Revision history for this message
Diego Biurrun (diego-biurrun) wrote :

The rampant casting is still not under control. Some void* casts are surely still left as are pointless return value casts.

Then there is the deeper issue of data structures that are (mis)designed to only work with casting.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.