Skip to content

Commit 6ec60c9

Browse files
author
Uoti Urpala
committed
talloc.[ch]: remove "type safety" hack that violates C types
The destructors used by talloc take a "void *" first parameter. However talloc.h had a #define hack that treated the destructor as a function taking first parameter of type "typeof(ptr)" where ptr is the pointer the destructor is set for. I suppose this was done to add some kind of "type safety" against adding a destructor expecting another type of pointer; however this hack is questionable and violates the real C level typing. Remove the hack from the header and adjust talloc.c to avoid a warning about a C type violation that became visible after removing the hack.
1 parent a7fee50 commit 6ec60c9

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

talloc.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,9 @@ int talloc_increase_ref_count(const void *ptr)
432432
433433
this is referenced by a function pointer and should not be inline
434434
*/
435-
static int talloc_reference_destructor(struct talloc_reference_handle *handle)
435+
static int talloc_reference_destructor(void *ptr)
436436
{
437+
struct talloc_reference_handle *handle = ptr;
437438
struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
438439
_TLIST_REMOVE(ptr_tc->refs, handle);
439440
return 0;

talloc.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,15 @@ typedef void TALLOC_CTX;
7070
if we have a recent gcc */
7171
#if (__GNUC__ >= 3)
7272
#define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
73-
#define talloc_set_destructor(ptr, function) \
74-
do { \
75-
int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
76-
_talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
77-
} while(0)
7873
/* this extremely strange macro is to avoid some braindamaged warning
7974
stupidity in gcc 4.1.x */
8075
#define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); __talloc_steal_ret; })
8176
#else
82-
#define talloc_set_destructor(ptr, function) \
83-
_talloc_set_destructor((ptr), (int (*)(void *))(function))
8477
#define _TALLOC_TYPEOF(ptr) void *
8578
#define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
8679
#endif
80+
#define talloc_set_destructor(ptr, function) \
81+
_talloc_set_destructor((ptr), (function))
8782

8883
#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
8984
#define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))

0 commit comments

Comments
 (0)