(trunk libT) #3293 "fggets() is overkill for parsing the blocklist" -- fixed in trunk for 2.00

This commit is contained in:
Charles Kerr 2010-06-15 00:38:10 +00:00
parent 3e7730e9b7
commit 3ae99f79ec
4 changed files with 6 additions and 151 deletions

View File

@ -26,7 +26,6 @@ libtransmission_a_SOURCES = \
ConvertUTF.c \
crypto.c \
fdlimit.c \
ggets.c \
handshake.c \
history.c \
inout.c \
@ -77,7 +76,6 @@ noinst_HEADERS = \
crypto.h \
completion.h \
fdlimit.h \
ggets.h \
handshake.h \
history.h \
inout.h \

View File

@ -27,8 +27,6 @@
#include <unistd.h>
#include <assert.h>
#include "ggets.h"
#include "transmission.h"
#include "platform.h"
#include "blocklist.h"
@ -304,11 +302,11 @@ int
_tr_blocklistSetContent( tr_blocklist * b,
const char * filename )
{
FILE * in;
FILE * out;
char * line;
int inCount = 0;
int outCount = 0;
FILE * in;
FILE * out;
int inCount = 0;
int outCount = 0;
char line[2048];
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
if( !filename )
@ -334,7 +332,7 @@ _tr_blocklistSetContent( tr_blocklist * b,
return 0;
}
while( !fggets( &line, in ) )
while( fgets( line, sizeof( line ), in ) != NULL )
{
char * walk;
struct tr_ip_range range;
@ -349,12 +347,9 @@ _tr_blocklistSetContent( tr_blocklist * b,
{
/* don't try to display the actual lines - it causes issues */
tr_err( _( "blocklist skipped invalid address at line %d" ), inCount );
free( line );
continue;
}
free( line );
if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1 )
{
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), b->filename,

View File

@ -1,84 +0,0 @@
/* File ggets.c - goodgets is a safe alternative to gets */
/* By C.B. Falconer. Public domain 2002-06-22 */
/* attribution appreciated. */
/* Revised 2002-06-26 New prototype.
2002-06-27 Incomplete final lines
2006-06-14 Simplified, using getc, not fgets
2006-06-15 Fixed memory leak at EOF
*/
/* fggets and ggets [which is fggets(ln, stdin)] set *ln to
a buffer filled with the next complete line from the text
stream f. The storage has been allocated within fggets,
and is normally reduced to be an exact fit. The trailing
\n has been removed, so the resultant line is ready for
dumping with puts. The buffer will be as large as is
required to hold the complete line.
Note: this means a final file line without a \n terminator
has an effective \n appended, as EOF occurs within the read.
If no error occurs fggets returns 0. If an EOF occurs on
the input file, EOF is returned. For memory allocation
errors some positive value is returned. In this case *ln
may point to a partial line. For other errors memory is
freed and *ln is set to NULL.
Freeing of assigned storage is the callers responsibility
*/
#include <stdio.h>
#include <stdlib.h>
#include "ggets.h"
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */
#define DELTASIZE ( INITSIZE + 16 )
enum {OK = 0, NOMEM};
int
fggets( char* *ln,
FILE * f )
{
int cursize, ch, ix;
char *buffer, *temp;
*ln = NULL; /* default */
if( NULL == ( buffer = malloc( INITSIZE ) ) ) return NOMEM;
cursize = INITSIZE;
ix = 0;
while( ( EOF != ( ch = getc( f ) ) ) && ( '\n' != ch ) )
{
if( ix >= ( cursize - 1 ) ) /* extend buffer */
{
cursize += DELTASIZE;
if( NULL == ( temp = realloc( buffer, (size_t)cursize ) ) )
{
/* ran out of memory, return partial line */
buffer[ix] = '\0';
*ln = buffer;
return NOMEM;
}
buffer = temp;
}
buffer[ix++] = ch;
}
if( ( EOF == ch ) && ( 0 == ix ) )
{
free( buffer );
return EOF;
}
buffer[ix] = '\0';
if( NULL == ( temp = realloc( buffer, (size_t)ix + 1 ) ) )
{
*ln = buffer; /* without reducing it */
}
else *ln = temp;
return OK;
} /* fggets */
/* End of ggets.c */

View File

@ -1,54 +0,0 @@
#ifndef __TRANSMISSION__
#error only libtransmission should #include this header.
#endif
/* File ggets.h - goodgets is a safe alternative to gets */
/* By C.B. Falconer. Public domain 2002-06-22 */
/* attribution appreciated. */
/* Revised 2002-06-26 New prototype.
2002-06-27 Incomplete final lines
*/
/* fggets and ggets [which is fggets(ln, stdin)] set *ln to
a buffer filled with the next complete line from the text
stream f. The storage has been allocated within fggets,
and is normally reduced to be an exact fit. The trailing
\n has been removed, so the resultant line is ready for
dumping with puts. The buffer will be as large as is
required to hold the complete line.
Note: this means a final file line without a \n terminator
has an effective \n appended, as EOF occurs within the read.
If no error occurs fggets returns 0. If an EOF occurs on
the input file, EOF is returned. For memory allocation
errors some positive value is returned. In this case *ln
may point to a partial line. For other errors memory is
freed and *ln is set to NULL.
Freeing of assigned storage is the callers responsibility
*/
#ifndef ggets_h_
#define ggets_h_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief read a line from a file and place it in a newly-allocated string
* @ingroup utils
*/
int fggets( char* *ln, FILE * f );
#define ggets( ln ) fggets( ln, stdin )
#ifdef __cplusplus
}
#endif
#endif
/* END ggets.h */