1
0
mirror of https://github.com/MariaDB/server.git synced 2025-12-24 11:21:21 +03:00

Bug#42733: Type-punning warnings when compiling MySQL --

strict aliasing violations.

One somewhat major source of strict-aliasing violations and
related warnings is the SQL_LIST structure. For example,
consider its member function `link_in_list` which takes
a pointer to pointer of type T (any type) as a pointer to
pointer to unsigned char. Dereferencing this pointer, which
is done to reset the next field, violates strict-aliasing
rules and might cause problems for surrounding code that
uses the next field of the object being added to the list.

The solution is to use templates to parametrize the SQL_LIST
structure in order to deference the pointers with compatible
types. As a side bonus, it becomes possible to remove quite
a few casts related to acessing data members of SQL_LIST.

sql/handler.h:
  Use the appropriate template type argument.
sql/item.cc:
  Remove now-unnecessary cast.
sql/item_subselect.cc:
  Remove now-unnecessary casts.
sql/item_sum.cc:
  Use the appropriate template type argument.
  Remove now-unnecessary cast.
sql/mysql_priv.h:
  Move SQL_LIST structure to sql_list.h
  Use the appropriate template type argument.
sql/sp.cc:
  Remove now-unnecessary casts.
sql/sql_delete.cc:
  Use the appropriate template type argument.
  Remove now-unnecessary casts.
sql/sql_derived.cc:
  Remove now-unnecessary casts.
sql/sql_lex.cc:
  Remove now-unnecessary casts.
sql/sql_lex.h:
  SQL_LIST now takes a template type argument which must
  match the type of the elements of the list. Use forward
  declaration when the type is not available, it is used
  in pointers anyway.
sql/sql_list.h:
  Rename SQL_LIST to SQL_I_List. The template parameter is
  the type of object that is stored in the list.
sql/sql_olap.cc:
  Remove now-unnecessary casts.
sql/sql_parse.cc:
  Remove now-unnecessary casts.
sql/sql_prepare.cc:
  Remove now-unnecessary casts.
sql/sql_select.cc:
  Remove now-unnecessary casts.
sql/sql_show.cc:
  Remove now-unnecessary casts.
sql/sql_table.cc:
  Remove now-unnecessary casts.
sql/sql_trigger.cc:
  Remove now-unnecessary casts.
sql/sql_union.cc:
  Remove now-unnecessary casts.
sql/sql_update.cc:
  Remove now-unnecessary casts.
sql/sql_view.cc:
  Remove now-unnecessary casts.
sql/sql_yacc.yy:
  Remove now-unnecessary casts.
storage/myisammrg/ha_myisammrg.cc:
  Remove now-unnecessary casts.
This commit is contained in:
Davi Arnaut
2010-06-10 17:45:22 -03:00
parent 6f3a540c37
commit 0f9ddfa9d8
25 changed files with 216 additions and 210 deletions

View File

@@ -55,6 +55,73 @@ public:
};
/**
Simple intrusive linked list.
@remark Similar in nature to base_list, but intrusive. It keeps a
a pointer to the first element in the list and a indirect
reference to the last element.
*/
template <typename T>
class SQL_I_List :public Sql_alloc
{
public:
uint elements;
/** The first element in the list. */
T *first;
/** A reference to the next element in the list. */
T **next;
SQL_I_List() { empty(); }
SQL_I_List(const SQL_I_List &tmp)
{
elements= tmp.elements;
first= tmp.first;
next= elements ? tmp.next : &first;
}
inline void empty()
{
elements= 0;
first= NULL;
next= &first;
}
inline void link_in_list(T *element, T **next_ptr)
{
elements++;
(*next)= element;
next= next_ptr;
*next= NULL;
}
inline void save_and_clear(SQL_I_List<T> *save)
{
*save= *this;
empty();
}
inline void push_front(SQL_I_List<T> *save)
{
/* link current list last */
*save->next= first;
first= save->first;
elements+= save->elements;
}
inline void push_back(SQL_I_List<T> *save)
{
if (save->first)
{
*next= save->first;
next= save->next;
elements+= save->elements;
}
}
};
/*
Basic single linked list
Used for item and item_buffs.