This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug regex/1357] New: memory leak in re_compile_pattern()


Calling re_compile_pattern results in a memory leak proportional to the size of 
the Pattern String. See Example code below:
#include <regex.h>
#include <stdio.h>

int main()
{
	struct re_pattern_buffer	*patBuf;
	const char			*ErrStr;
	char				 PatStr[128];
	unsigned int			 i;
	


	patBuf = (struct re_pattern_buffer *) malloc(sizeof(struct 
re_pattern_buffer));
	if (!patBuf)
		exit(0);
	memset(patBuf, 0, sizeof(struct re_pattern_buffer));

	patBuf->translate      = 0;
	patBuf->fastmap	       = 0;
	patBuf->buffer	       = (char *) malloc(2048); //Optional
	patBuf->allocated      = 2048;
	patBuf->regs_allocated = REGS_UNALLOCATED; /Optional
	patBuf->used           = 0;

        re_set_syntax (RE_SYNTAX_POSIX_EGREP); //Optional
	for (i = 0; i < 10000; i++) {
		sprintf(PatStr, "TEST.?STRING%d", i); //This can be anything
                memset(patBuf->buffer, 0, 2048);
		ErrStr = re_compile_pattern(PatStr,
			                    strlen(PatStr),
					    patBuf);
		if (ErrStr)
		    printf("ErrStr = %s\n",ErrStr);
		} //endof for (i = 0; i < 10000; i++)
	if (patBuf->buffer)
		free(patBuf->buffer);
	if (patBuf)
		free(patBuf);
	
	exit(1);
	} //endof main()

Leak was introduced after glibc-2.2.2. This code works fine on a linux 2.4 
system running with glibc-2.2.2, leak occures in glibc 2.3.5 and 2.3.1 
glibc 2.3.5 was compiled with:
CFLAGS="-O3 -march=i686 -mcpu=i686 -funroll-loops -fomit-frame-pointer -
fexpensive-optimizations -pipe"

-- 
           Summary: memory leak in re_compile_pattern()
           Product: glibc
           Version: 2.3.5
            Status: NEW
          Severity: normal
          Priority: P2
         Component: regex
        AssignedTo: gotom at debian dot or dot jp
        ReportedBy: root at liwave dot com
                CC: glibc-bugs-regex at sources dot redhat dot com,glibc-
                    bugs at sources dot redhat dot com
  GCC host triplet: Linux gentoo 2.6.7-hardened-r18 #2 SMP Sun Jan 16
                    18:37:38 EST 2


http://sourceware.org/bugzilla/show_bug.cgi?id=1357

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]