From 76ecf81428c161a98a5621b55a64cb8515f80585 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 10 Jul 2005 01:10:09 +0000 Subject: r8273: fixed some memory leaks in smbscript. This required converting file_load() to use talloc, which impacted quite a few bits of code, including our smb.conf processing. took the opportunity to remove the gloabls in params.c while doing this (This used to be commit b220756cb4f1d201ba3e771ca67e4bfae5eae748) --- source4/param/params.c | 112 ++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 62 deletions(-) (limited to 'source4/param') diff --git a/source4/param/params.c b/source4/param/params.c index 34d537c395..d59e11a895 100644 --- a/source4/param/params.c +++ b/source4/param/params.c @@ -89,25 +89,14 @@ #define BUFR_INC 1024 -/* -------------------------------------------------------------------------- ** - * Variables... - * - * DEBUGLEVEL - The ubiquitous DEBUGLEVEL. This determines which DEBUG() - * messages will be produced. - * bufr - pointer to a global buffer. This is probably a kludge, - * but it was the nicest kludge I could think of (for now). - * bSize - The size of the global buffer . - */ - -static char *bufr = NULL; -static int bSize = 0; - /* we can't use FILE* due to the 256 fd limit - use this cheap hack instead */ typedef struct { char *buf; char *p; size_t size; + char *bufr; + int bSize; } myFILE; static int mygetc(myFILE *f) @@ -119,9 +108,7 @@ static int mygetc(myFILE *f) static void myfile_close(myFILE *f) { - if (!f) return; - SAFE_FREE(f->buf); - SAFE_FREE(f); + talloc_free(f); } /* -------------------------------------------------------------------------- ** @@ -235,58 +222,58 @@ static BOOL Section( myFILE *InFile, BOOL (*sfunc)(const char *) ) { /* Check that the buffer is big enough for the next character. */ - if( i > (bSize - 2) ) + if( i > (InFile->bSize - 2) ) { char *tb; - tb = realloc_p(bufr, char, bSize + BUFR_INC); + tb = talloc_realloc(InFile, InFile->bufr, char, InFile->bSize + BUFR_INC); if( NULL == tb ) { DEBUG(0, ("%s Memory re-allocation failure.", func) ); return( False ); } - bufr = tb; - bSize += BUFR_INC; + InFile->bufr = tb; + InFile->bSize += BUFR_INC; } /* Handle a single character. */ switch( c ) { case ']': /* Found the closing bracket. */ - bufr[end] = '\0'; + InFile->bufr[end] = '\0'; if( 0 == end ) /* Don't allow an empty name. */ { DEBUG(0, ("%s Empty section name in configuration file.\n", func )); return( False ); } - if( !sfunc(bufr) ) /* Got a valid name. Deal with it. */ + if( !sfunc(InFile->bufr) ) /* Got a valid name. Deal with it. */ return( False ); (void)EatComment( InFile ); /* Finish off the line. */ return( True ); case '\n': /* Got newline before closing ']'. */ - i = Continuation( bufr, i ); /* Check for line continuation. */ + i = Continuation( InFile->bufr, i ); /* Check for line continuation. */ if( i < 0 ) { - bufr[end] = '\0'; + InFile->bufr[end] = '\0'; DEBUG(0, ("%s Badly formed line in configuration file: %s\n", - func, bufr )); + func, InFile->bufr )); return( False ); } - end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); + end = ( (i > 0) && (' ' == InFile->bufr[i - 1]) ) ? (i - 1) : (i); c = mygetc( InFile ); /* Continue with next line. */ break; default: /* All else are a valid name chars. */ if( isspace( c ) ) /* One space per whitespace region. */ { - bufr[end] = ' '; + InFile->bufr[end] = ' '; i = end + 1; c = EatWhitespace( InFile ); } else /* All others copy verbatim. */ { - bufr[i++] = c; + InFile->bufr[i++] = c; end = i; c = mygetc( InFile ); } @@ -294,7 +281,7 @@ static BOOL Section( myFILE *InFile, BOOL (*sfunc)(const char *) ) } /* We arrive here if we've met the EOF before the closing bracket. */ - DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr )); + DEBUG(0, ("%s Unexpected EOF in the configuration file\n", func)); return( False ); } /* Section */ @@ -332,18 +319,18 @@ static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *) while( 0 == vstart ) /* Loop until we've found the start of the value. */ { - if( i > (bSize - 2) ) /* Ensure there's space for next char. */ + if( i > (InFile->bSize - 2) ) /* Ensure there's space for next char. */ { char *tb; - tb = realloc_p( bufr, char, bSize + BUFR_INC ); + tb = talloc_realloc(InFile, InFile->bufr, char, InFile->bSize + BUFR_INC ); if( NULL == tb ) { DEBUG(0, ("%s Memory re-allocation failure.", func) ); return( False ); } - bufr = tb; - bSize += BUFR_INC; + InFile->bufr = tb; + InFile->bSize += BUFR_INC; } switch( c ) @@ -354,41 +341,41 @@ static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *) DEBUG(0, ("%s Invalid parameter name in config. file.\n", func )); return( False ); } - bufr[end++] = '\0'; /* Mark end of string & advance. */ + InFile->bufr[end++] = '\0'; /* Mark end of string & advance. */ i = end; /* New string starts here. */ vstart = end; /* New string is parameter value. */ - bufr[i] = '\0'; /* New string is nul, for now. */ + InFile->bufr[i] = '\0'; /* New string is nul, for now. */ break; case '\n': /* Find continuation char, else error. */ - i = Continuation( bufr, i ); + i = Continuation( InFile->bufr, i ); if( i < 0 ) { - bufr[end] = '\0'; + InFile->bufr[end] = '\0'; DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n", - func, bufr )); + func, InFile->bufr )); return( True ); } - end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); + end = ( (i > 0) && (' ' == InFile->bufr[i - 1]) ) ? (i - 1) : (i); c = mygetc( InFile ); /* Read past eoln. */ break; case '\0': /* Shouldn't have EOF within param name. */ case EOF: - bufr[i] = '\0'; - DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr )); + InFile->bufr[i] = '\0'; + DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, InFile->bufr )); return( True ); default: if( isspace( c ) ) /* One ' ' per whitespace region. */ { - bufr[end] = ' '; + InFile->bufr[end] = ' '; i = end + 1; c = EatWhitespace( InFile ); } else /* All others verbatim. */ { - bufr[i++] = c; + InFile->bufr[i++] = c; end = i; c = mygetc( InFile ); } @@ -400,18 +387,18 @@ static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *) while( (EOF !=c) && (c > 0) ) { - if( i > (bSize - 2) ) /* Make sure there's enough room. */ + if( i > (InFile->bSize - 2) ) /* Make sure there's enough room. */ { char *tb; - tb = realloc_p( bufr, char, bSize + BUFR_INC ); + tb = talloc_realloc(InFile, InFile->bufr, char, InFile->bSize + BUFR_INC ); if( NULL == tb ) { DEBUG(0, ("%s Memory re-allocation failure.", func) ); return( False ); } - bufr = tb; - bSize += BUFR_INC; + InFile->bufr = tb; + InFile->bSize += BUFR_INC; } switch( c ) @@ -421,28 +408,28 @@ static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *) break; /* removes them. */ case '\n': /* Marks end of value unless there's a '\'. */ - i = Continuation( bufr, i ); + i = Continuation( InFile->bufr, i ); if( i < 0 ) c = 0; else { - for( end = i; (end >= 0) && isspace((int)bufr[end]); end-- ) + for( end = i; (end >= 0) && isspace((int)InFile->bufr[end]); end-- ) ; c = mygetc( InFile ); } break; default: /* All others verbatim. Note that spaces do */ - bufr[i++] = c; /* not advance . This allows trimming */ + InFile->bufr[i++] = c; /* not advance . This allows trimming */ if( !isspace( c ) ) /* of whitespace at the end of the line. */ end = i; c = mygetc( InFile ); break; } } - bufr[end] = '\0'; /* End of value. */ + InFile->bufr[end] = '\0'; /* End of value. */ - return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ + return( pfunc( InFile->bufr, &InFile->bufr[vstart] ) ); /* Pass name & value to pfunc(). */ } /* Parameter */ static BOOL Parse( myFILE *InFile, @@ -520,20 +507,22 @@ static myFILE *OpenConfFile( const char *FileName ) const char *func = "params.c:OpenConfFile() -"; myFILE *ret; - ret = malloc_p(myFILE); + ret = talloc(talloc_autofree_context(), myFILE); if (!ret) return NULL; - ret->buf = file_load(FileName, &ret->size); + ret->buf = file_load(FileName, &ret->size, ret); if( NULL == ret->buf ) { DEBUG( 1, ("%s Unable to open configuration file \"%s\":\n\t%s\n", func, FileName, strerror(errno)) ); - SAFE_FREE(ret); + talloc_free(ret); return NULL; } ret->p = ret->buf; + ret->bufr = NULL; + ret->bSize = 0; return( ret ); } /* OpenConfFile */ @@ -564,24 +553,23 @@ BOOL pm_process( const char *FileName, DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) ); - if( NULL != bufr ) /* If we already have a buffer */ + if( NULL != InFile->bufr ) /* If we already have a buffer */ result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */ /* use it. */ else /* If we don't have a buffer */ { /* allocate one, then parse, */ - bSize = BUFR_INC; /* then free. */ - bufr = (char *)malloc( bSize ); - if( NULL == bufr ) + InFile->bSize = BUFR_INC; /* then free. */ + InFile->bufr = talloc_array(InFile, char, InFile->bSize ); + if( NULL == InFile->bufr ) { DEBUG(0,("%s memory allocation failure.\n", func)); myfile_close(InFile); return( False ); } result = Parse( InFile, sfunc, pfunc ); - SAFE_FREE( bufr ); - bufr = NULL; - bSize = 0; + InFile->bufr = NULL; + InFile->bSize = 0; } myfile_close(InFile); -- cgit