This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] fix null pointer in mtrace
- From: liqingqing <liqingqing3 at huawei dot com>
- To: Liusirui <liusirui at huawei dot com>, <libc-alpha at sourceware dot org>
- Cc: <carlos at redhat dot com>, <siddhesh at gotplt dot org>, <dj at redhat dot com>, <hushiyuan at huawei dot com>
- Date: Tue, 12 Nov 2019 17:39:59 +0800
- Subject: Re: [PATCH] fix null pointer in mtrace
- References: <1573550539-34259-1-git-send-email-liusirui@huawei.com>
On 2019/11/12 17:22, Liusirui wrote:
> In a multi-threaded program, some threads request or free memory and try to write trace info
> into file which "mallstream" points to. At the same time, another thread calls "muntrace" and
> set "mallstream" to NULL. This may cause a segmentation fault.
>
> The comment in malloc/mtrace.c says "We could be printing a NULL here; that's OK.". Although
> the functions mtrace/muntrace are used for debugging, program isn't expected to crash while using
> these functions.
>
> ---
> malloc/mtrace.c | 29 ++++++++++++++++-------------
> 1 file changed, 16 insertions(+), 13 deletions(-)
>
> diff --git a/malloc/mtrace.c b/malloc/mtrace.c
> index 707f998..33f01b4 100644
> --- a/malloc/mtrace.c
> +++ b/malloc/mtrace.c
> @@ -44,6 +44,10 @@
>
> #define TRACE_BUFFER_SIZE 512
>
> +#define mtrace_print(file, format, ...) do { \
> +if (file != NULL) mtrace_print(file, format,##__VA_ARGS__); \
> +} while(0)
> +
I had tested this scenario, it seems like that the fprintf and other file operation function do not check the invalid argument like the null pointer.
does any one knows why fprintf do not check the input? thanks