From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7448 invoked by alias); 26 May 2011 15:30:02 -0000 Received: (qmail 7387 invoked by uid 22791); 26 May 2011 15:30:00 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO sourceware.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 26 May 2011 15:29:49 +0000 From: "emil at wojak dot eu" To: glibc-bugs-regex@sources.redhat.com Subject: [Bug regex/12811] New: regexec/re_search consumes huge amounts of memory X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: regex X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: emil at wojak dot eu X-Bugzilla-Status: NEW X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: drepper.fsp at gmail dot com X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Thu, 26 May 2011 15:30:00 -0000 Mailing-List: contact glibc-bugs-regex-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: glibc-bugs-regex-owner@sourceware.org X-SW-Source: 2011-05/txt/msg00001.txt.bz2 http://sourceware.org/bugzilla/show_bug.cgi?id=12811 Summary: regexec/re_search consumes huge amounts of memory Product: glibc Version: 2.13 Status: NEW Severity: normal Priority: P2 Component: regex AssignedTo: drepper.fsp@gmail.com ReportedBy: emil@wojak.eu Created attachment 5753 --> http://sourceware.org/bugzilla/attachment.cgi?id=5753 Fix for huge memory usage The bug is triggered under the following circumstances: - multibyte character encoding, like pl_PL.UTF-8 - either translation table is used or RE_ICASE flag is set - input buffer which ends with a UTF-8 character cut in the middle, ex. aaaaaaaaaaaa\xc4 - specific kind of regex, that does not match the input buffer, and that re_search would apply starting at each position of the input buffer ex. [^b]*ab or simply .*ab Here's a sample program that consumes 1.4 GB on 32-bit architecture and 5.2 GB on 64-bit machines (measured with valgrind --tool=massif). #include #include int main(void) { regex_t preg; setlocale(LC_CTYPE, "en_US.UTF-8"); regcomp(&preg, ".*ab", REG_ICASE); regexec(&preg, "aaaaaaaaaaaa\xc4", 0, NULL, 0); regfree(&preg); return 0; } The exhaustive memory usage is caused by calling extend_buffers with each re_search_internal iteration, even though internal buffers already are long enough to hold the whole string. When matching procedure reaches mctx->input.valid_len, internal buffer size is doubled and the rest of the input buffer is converted to wchar_t, except for the last byte, which is a UTF-8 character cut in the middle. This last character is never converted, because it's continuation never comes, but still internal buffers are needlessly doubled. A patch solving this problem is attached. There's another issue. Once the internal buffers are long enough to hold at least half of the input buffer, they shouldn't get doubled, because that's a waste of memory. Instead it's enough to extend them to the actual length of the input buffer. This can save significant amounts of memory for long input buffers. A patch for this issue is attached as well. -- Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are on the CC list for the bug.