From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 6C4AC3858C66 for ; Tue, 1 Aug 2023 12:36:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C4AC3858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690893361; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=J8w69cOHda5tLs2WcnyD7k++MPd4WzwruPrTP7MEbps=; b=OmYygsptx5vGRUTQiOMAFPPkOZfTD9q2d9Rpaw/aokGi8wSeJ9H59h6e4Av0fpzHJMZoe/ ozHzxBpX/7iL0JbpEoEJYSFVOHQtF6J0E51c4sFZlg8QI3epq6RGjLxBnxcNqLPh62/796 InK55YiIbOigPoRA3UFekh94WOg9f+4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-91-MvuRq9ZUMoCGD8An0DzonQ-1; Tue, 01 Aug 2023 08:35:58 -0400 X-MC-Unique: MvuRq9ZUMoCGD8An0DzonQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2F4BD858EED; Tue, 1 Aug 2023 12:35:58 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.193.241]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0D7102166B25; Tue, 1 Aug 2023 12:35:57 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id 14EF1A80982; Tue, 1 Aug 2023 14:35:54 +0200 (CEST) Date: Tue, 1 Aug 2023 14:35:54 +0200 From: Corinna Vinschen To: Takashi Yano Cc: newlib@sourceware.org Subject: Re: [PATCH] newlib: Fix memory leak regarding gdtoa-based _ldtoa_r(). Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: Takashi Yano , newlib@sourceware.org References: <20230801085731.1831-1-takashi.yano@nifty.ne.jp> MIME-Version: 1.0 In-Reply-To: <20230801085731.1831-1-takashi.yano@nifty.ne.jp> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi Takashi, On Aug 1 17:57, Takashi Yano wrote: > After the commit a4705d387f78, printf() for floating-point values > causes a memory leak. The legacy _ldtoa_r() assumed the char pointer > returned will be free'ed by Bfree(). However, gdtoa-based _ldtoa_r() > returns the pointer returned by gdtoa() which should be free'ed by > freedtoa(). Due to this issue, the caller of _ldtoa_r() fails to free > the allocated char buffer. This is the cause of the said memory leak. > https://cygwin.com/pipermail/cygwin/2023-July/254054.html > > With this patch, a new buffer is allocated using Balloc() and the > buffer returned by gdtoa() is copied into it. Then, free the original > buffer using freedtoa(). Basically this is ok. But the question is this. Isn't it possible to redefine the allocator in gdtoa so that it uses a Balloced buffer right away? Forinstance, by using overloading macros? That would allow to get away without having to copy stuff... Thanks, Corinna > > Fixes: a4705d387f78 ("ldtoa: Import gdtoa from OpenBSD.") > Reported-by: natan_b > Signed-off-by: Takashi Yano > --- > newlib/libc/stdlib/gdtoa-ldtoa.c | 21 +++++++++++++++++++-- > winsup/cygwin/release/3.4.8 | 3 +++ > 2 files changed, 22 insertions(+), 2 deletions(-) > > diff --git a/newlib/libc/stdlib/gdtoa-ldtoa.c b/newlib/libc/stdlib/gdtoa-ldtoa.c > index 14b99042c..7a484613f 100644 > --- a/newlib/libc/stdlib/gdtoa-ldtoa.c > +++ b/newlib/libc/stdlib/gdtoa-ldtoa.c > @@ -63,7 +63,8 @@ _ldtoa_r(struct _reent *ptr, > #endif > }; > int be, kind; > - char *ret; > + int i, j; > + char *ret, *outbuf; > struct ieee_ext *p = (struct ieee_ext *)&ld; > uint32_t bits[(LDBL_MANT_DIG + 31) / 32]; > void *vbits = bits; > @@ -113,7 +114,23 @@ _ldtoa_r(struct _reent *ptr, > abort(); > } > > - ret = gdtoa(ptr, &fpi, be, vbits, &kind, mode, ndigits, decpt, rve); > + outbuf = gdtoa(ptr, &fpi, be, vbits, &kind, mode, ndigits, decpt, rve); > + > + i = strlen (outbuf); > + j = sizeof (__ULong); > + for (_REENT_MP_RESULT_K (ptr) = 0; > + sizeof (_Bigint) - sizeof (__ULong) + j <= i; j <<= 1) > + _REENT_MP_RESULT_K (ptr)++; > + _REENT_MP_RESULT (ptr) = eBalloc (ptr, _REENT_MP_RESULT_K (ptr)); > + > + /* Copy from gdtoa-type buffer (which is allocated by rv_alloc()) > + to the buffer used by ldtoa (which is allocated by Balloc()). */ > + ret = (char *) _REENT_MP_RESULT (ptr); > + strcpy (ret, outbuf); > + if (rve) > + *rve = ret + (*rve - outbuf); > + freedtoa (ptr, outbuf); > + > if (*decpt == -32768) > *decpt = INT_MAX; > return ret; > diff --git a/winsup/cygwin/release/3.4.8 b/winsup/cygwin/release/3.4.8 > index d37272eef..448831c65 100644 > --- a/winsup/cygwin/release/3.4.8 > +++ b/winsup/cygwin/release/3.4.8 > @@ -14,3 +14,6 @@ Bug Fixes > - Rename internal macros _NL_CTYPE_OUTDIGITSx_MB/WC to GLibc compatible > _NL_CTYPE_OUTDIGITx_MB/WC. > Addresses: https://cygwin.com/pipermail/cygwin-developers/2023-July/012637.html > + > +- Fix memory leak in printf() regarding gdtoa-based _ldtoa_r(). > + Addresses: https://cygwin.com/pipermail/cygwin/2023-July/254054.html > -- > 2.39.0