[PATCH] add support for -Wmismatched-dealloc
Florian Weimer
fweimer@redhat.com
Mon Jan 4 16:57:59 GMT 2021
* Martin Sebor:
> On 1/4/21 9:07 AM, Florian Weimer wrote:
>> * Martin Sebor via Libc-alpha:
>>
>>> diff --git a/wcsmbs/wchar.h b/wcsmbs/wchar.h
>>> index 9cf8b05a87..4c1c7f1119 100644
>>> --- a/wcsmbs/wchar.h
>>> +++ b/wcsmbs/wchar.h
>>> @@ -151,7 +151,8 @@ extern size_t wcsxfrm_l (wchar_t *__s1, const wchar_t *__s2,
>>> size_t __n, locale_t __loc) __THROW;
>>> /* Duplicate S, returning an identical malloc'd string. */
>>> -extern wchar_t *wcsdup (const wchar_t *__s) __THROW __attribute_malloc__;
>>> +extern wchar_t *wcsdup (const wchar_t *__s) __THROW
>>> + __attribute_malloc__ __attr_dealloc_free;
>>> #endif
>>> /* Find the first occurrence of WC in WCS. */
>>> @@ -562,9 +563,18 @@ extern wchar_t *wcpncpy (wchar_t *__restrict __dest,
>>> /* Wide character I/O functions. */
>>> #if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2)
>>> +# ifdef __REDIRECT
>>> +/* Declare the __fclose alias and associate it as a deallocator
>>> + with open_wmemstream below. */
>>> +extern int __REDIRECT (__fclose, (FILE *), fclose);
>>> +# define __attr_dealloc_fclose __attr_dealloc (__fclose, 1)
>>> +# else
>>> +# define __attr_dealloc_fclose /* empty */
>>> +# endif
>>> /* Like OPEN_MEMSTREAM, but the stream is wide oriented and produces
>>> a wide character string. */
>>> -extern __FILE *open_wmemstream (wchar_t **__bufloc, size_t *__sizeloc) __THROW;
>>> +extern __FILE *open_wmemstream (wchar_t **__bufloc, size_t *__sizeloc) __THROW
>>> + __attribute_malloc__ __attr_dealloc_fclose;
>>> #endif
>>> #if defined __USE_ISOC95 || defined __USE_UNIX98
>> Why is an alias for fclose needed here, but not for free?
>
> Because fclose is not a built-in so there's no __builtin_fclose
> to associate open_wmemstream with. free is a built-in and so
> __attr_dealloc_free just references __builtin_free and doesn't
> need an explicit declaration.
Ahh, that explains the discrepancy.
I'm a bit worried that the __fclose alias causes problems. Would it be
possible to add __builtin_fclose to GCC instead?
Based on how this patch appears to make both __fclose and fclose
acceptable as a deallocator, GCC resolves redirects as part of the
matching check. I wonder if this constrains the usefulness of the
attribute in some way. I can imagine situations where at the source
level, different deallocators should be used (say to support debugging
builds), but release builds redirect different deallocators to the same
implementation.
Thanks,
Florian
--
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill
More information about the Libc-alpha
mailing list