diff options
author | Tomas Chvatal <tchvatal@suse.cz> | 2011-09-23 17:03:04 +0200 |
---|---|---|
committer | Tomas Chvatal <tchvatal@suse.cz> | 2011-09-23 17:23:16 +0200 |
commit | d9f4ffae38b2e50ae4417cf84d6e1d79759941bc (patch) | |
tree | d1cd08b00f0054656c0eaaeb86782a86167fce4f /src | |
parent | c963e8ab9df2bb5a57585c016808790d8dee5928 (diff) |
Drop useless wg_free and wg_memset functions and cleanup.
The wg_free function does safeguards that are not needed as ansi
C ensures free to behave properly.
The other fixes are based on patch from Lorenzo Cogotti
<miciamail@hotmail.it>.
Diffstat (limited to 'src')
-rw-r--r-- | src/common.c | 10 | ||||
-rw-r--r-- | src/common.h | 2 | ||||
-rw-r--r-- | src/createfp.c | 2 | ||||
-rw-r--r-- | src/fingerprint.c | 18 | ||||
-rw-r--r-- | src/libtextcat.map | 1 | ||||
-rw-r--r-- | src/testtextcat.c | 2 | ||||
-rw-r--r-- | src/textcat.c | 62 | ||||
-rw-r--r-- | src/textcat.h | 2 | ||||
-rw-r--r-- | src/wg_mempool.c | 18 |
9 files changed, 51 insertions, 66 deletions
diff --git a/src/common.c b/src/common.c index 96394cb..27ba550 100644 --- a/src/common.c +++ b/src/common.c @@ -36,9 +36,6 @@ #include "config.h" #endif #include <stdio.h> -#ifdef HAVE_STDLIB_H -#include <stdlib.h> -#endif #ifdef HAVE_STRINGS_H #include <strings.h> #endif @@ -134,13 +131,6 @@ extern void* wg_realloc( void *ptr, size_t size ) return( result ); } -extern void wg_free( void *mem ) -{ - if ( mem ) { - free(mem); - } -} - extern char *wg_getline( char *line, int size, FILE *fp ) { char *p; diff --git a/src/common.h b/src/common.h index 723a502..9048847 100644 --- a/src/common.h +++ b/src/common.h @@ -38,6 +38,7 @@ #include <stdio.h> #include <time.h> +#include <stdlib.h> #ifdef __cplusplus extern "C" { @@ -81,7 +82,6 @@ extern void *wg_calloc( size_t nmemb, size_t size ); extern void *wg_zalloc( size_t size ); extern char* wg_strdup( const char *s ); extern void* wg_realloc( void *ptr, size_t size ) ; -extern void wg_free( void *mem ); extern char *wg_getline( char *line, int size, FILE *fp ); diff --git a/src/createfp.c b/src/createfp.c index e9da043..f2ae9d9 100644 --- a/src/createfp.c +++ b/src/createfp.c @@ -86,7 +86,7 @@ int main() } fp_Print(h,stdout); fp_Done(h); - wg_free(buf); + free(buf); return 0; } diff --git a/src/fingerprint.c b/src/fingerprint.c index db0a1ef..158af24 100644 --- a/src/fingerprint.c +++ b/src/fingerprint.c @@ -336,9 +336,9 @@ static void tabledone( table_t *t ) return; } wgmempool_Done(t->pool); - wg_free(t->table); - wg_free(t->heap); - wg_free(t); + free(t->table); + free(t->heap); + free(t); } @@ -359,13 +359,13 @@ extern void fp_Done( void *handle ) fp_t *h = (fp_t *)handle; if ( h->name ) { - wg_free( (void *)h->name ); + free( (void *)h->name ); } if ( h->fprint ) { - wg_free( h->fprint ); + free( h->fprint ); } - wg_free( h ); + free( h ); } extern const char *fp_Name( void *handle ) @@ -442,7 +442,7 @@ static char *prepbuffer( const char *src, size_t bufsize ) /*** Docs that are too small for a fingerprint, are refused ***/ if ( w - dest < MINDOCSIZE ) { - wg_free(dest); + free(dest); return NULL; } @@ -576,7 +576,7 @@ extern int fp_Create( void *handle, const char *buffer, uint4 bufsize, uint4 max } tabledone(t); - wg_free(tmp); + free(tmp); /*** Sort n-grams alphabetically, for easy comparison ***/ qsort( h->fprint, h->size, sizeof(ngram_t), ngramcmp_str ); @@ -659,7 +659,7 @@ extern void fp_Print( void *handle, FILE *fp ) /*fprintf( fp, "%s\t%i\n", tmp[i].str, tmp[i].rank );*/ fprintf( fp, "%s\n", tmp[i].str); } - wg_free( tmp ); + free( tmp ); } diff --git a/src/libtextcat.map b/src/libtextcat.map index dc8b20e..02b1b04 100644 --- a/src/libtextcat.map +++ b/src/libtextcat.map @@ -25,7 +25,6 @@ fp_Read heapextract wg_calloc - wg_free wg_getline wg_malloc wg_split diff --git a/src/testtextcat.c b/src/testtextcat.c index 87eb9b5..e5cb98e 100644 --- a/src/testtextcat.c +++ b/src/testtextcat.c @@ -103,7 +103,7 @@ int main( int argc, char **argv ) textcat_Done(h); - wg_free(buf); + free(buf); return 0; } diff --git a/src/textcat.c b/src/textcat.c index 9742be0..78bdbc2 100644 --- a/src/textcat.c +++ b/src/textcat.c @@ -91,16 +91,9 @@ typedef struct { static int cmpcandidates(const void *a, const void *b) { - candidate_t *x = (candidate_t *)a; - candidate_t *y = (candidate_t *)b; - - if ( x->score < y->score ) { - return -1; - } - if ( x->score > y->score ) { - return 1; - } - return 0; + const candidate_t *x = (const candidate_t *)a; + const candidate_t *y = (const candidate_t *)b; + return ( x->score - y->score ); } @@ -112,9 +105,9 @@ extern void textcat_Done( void *handle ) for (i=0; i<h->size; i++) { fp_Done( h->fprint[i] ); } - wg_free( h->fprint ); - wg_free( h->fprint_disable ); - wg_free( h ); + free( h->fprint ); + free( h->fprint_disable ); + free( h ); } @@ -145,12 +138,12 @@ extern void *special_textcat_Init( const char *conffile, const char *prefix ) h->size = 0; h->maxsize = 16; h->fprint = (void **)wg_malloc( sizeof(void*) * h->maxsize ); - h->fprint_disable = (unsigned char *)wg_malloc( sizeof(unsigned char*) * h->maxsize ); /*added to store the state of languages*/ + h->fprint_disable = (unsigned char *)wg_malloc( sizeof(unsigned char) * h->maxsize ); /*added to store the state of languages*/ while ( wg_getline( line, 1024, fp ) ) { char *p; char *segment[4]; - char finger_print_file_name[512]; + char finger_print_file_name[512 + 1]; int res; /*** Skip comments ***/ @@ -165,19 +158,23 @@ extern void *special_textcat_Init( const char *conffile, const char *prefix ) if ( h->size == h->maxsize ) { h->maxsize *= 2; h->fprint = (void **)wg_realloc( h->fprint, sizeof(void*) * h->maxsize ); - h->fprint_disable = (unsigned char *)wg_realloc( h->fprint_disable, sizeof(unsigned char*) * h->maxsize ); + h->fprint_disable = (unsigned char *)wg_realloc( h->fprint_disable, sizeof(unsigned char) * h->maxsize ); } /*** Load data ***/ - if ((h->fprint[ h->size ] = fp_Init( segment[1] ))==NULL) { + if ((h->fprint[ h->size ] = fp_Init( segment[1] )) == NULL) { + goto BAILOUT; + } + + /*** Check filename overflow ***/ + finger_print_file_name[0] = finger_print_file_name[512] = '\0'; + strncat(finger_print_file_name, prefix, 512); + strncat(finger_print_file_name, segment[0], 512); + if (finger_print_file_name[512] != '\0') { goto BAILOUT; } - finger_print_file_name[0] = '\0'; - strcat(finger_print_file_name, prefix); - strcat(finger_print_file_name, segment[0]); if ( fp_Read( h->fprint[h->size], finger_print_file_name, 400 ) == 0 ) { - textcat_Done(h); goto BAILOUT; } h->fprint_disable[h->size] = 0xF0; /*0xF0 is the code for enabled languages, 0x0F is for disabled*/ @@ -188,6 +185,7 @@ extern void *special_textcat_Init( const char *conffile, const char *prefix ) return h; BAILOUT: + textcat_Done(h); /*don't leak h*/ fclose(fp); return NULL; @@ -197,15 +195,15 @@ extern void *special_textcat_Init( const char *conffile, const char *prefix ) extern char *textcat_Classify( void *handle, const char *buffer, size_t size ) { textcat_t *h = (textcat_t *)handle; - uint4 i, cnt = 0; int minscore = MAXSCORE; int threshold = minscore; char *result = h->output; + uint4 i, cnt; #ifdef HAVE_ALLOCA candidate_t *candidates = (candidate_t *)alloca( sizeof(candidate_t) * h->size ); #else - candidate_t *candidates = (candidate_t *)malloc( sizeof(candidate_t) * h->size ); + candidate_t *candidates = (candidate_t *)wg_malloc( sizeof(candidate_t) * h->size ); #define SHOULD_FREE 1 #endif @@ -219,12 +217,12 @@ extern char *textcat_Classify( void *handle, const char *buffer, size_t size ) } /*** Calculate the score for each category. ***/ - for (i=0; i<h->size; i++) { + for ( i = 0; i < h->size; i++) { int score; if(h->fprint_disable[i] & 0x0F){ /*if this language is disabled*/ score = MAXSCORE; } - else{ + else { score = fp_Compare( h->fprint[i], unknown, threshold ); /*printf("Score for %s : %i\n", fp_Name(h->fprint[i]), score);*/ } @@ -237,29 +235,29 @@ extern char *textcat_Classify( void *handle, const char *buffer, size_t size ) } /*** Find the best performers ***/ - for (i=0; i<h->size; i++) { + for ( i = 0, cnt = 0; i < h->size; i++) { if ( candidates[i].score < threshold ) { - if ( ++cnt == MAXCANDIDATES+1 ) { + if ( ++cnt == MAXCANDIDATES + 1 ) { break; } - memcpy( &candidates[cnt-1], &candidates[i], sizeof(candidate_t) ); + memcpy( &candidates[cnt - 1], &candidates[i], sizeof(candidate_t) ); } } /*** The verdict ***/ - if ( cnt == MAXCANDIDATES+1 ) { + if ( cnt == MAXCANDIDATES + 1 ) { result = _TEXTCAT_RESULT_UNKOWN; } else { + const char *plimit = result + MAXOUTPUTSIZE; char *p = result; - char *plimit = result+MAXOUTPUTSIZE; qsort( candidates, cnt, sizeof(candidate_t), cmpcandidates ); *p = '\0'; - for (i=0; i<cnt; i++) { + for ( i = 0; i < cnt; i++ ) { p = wg_strgmov( p, "[", plimit ); p = wg_strgmov( p, candidates[i].name, plimit ); p = wg_strgmov( p, "]", plimit ); @@ -275,7 +273,7 @@ extern char *textcat_Classify( void *handle, const char *buffer, size_t size ) } -extern char *textcat_Version() +extern char *textcat_Version(void) { #ifdef PACKAGE_VERSION return "TextCat " PACKAGE_VERSION " (" DESCRIPTION ")"; diff --git a/src/textcat.h b/src/textcat.h index b658731..6552992 100644 --- a/src/textcat.h +++ b/src/textcat.h @@ -88,7 +88,7 @@ extern char *textcat_Classify( void *handle, const char *buffer, size_t size ); /** * textcat_Version() - Returns a string describing the version of this classifier. */ -extern char *textcat_Version(); +extern char *textcat_Version(void); #ifdef __cplusplus } diff --git a/src/wg_mempool.c b/src/wg_mempool.c index 805a6ed..9789157 100644 --- a/src/wg_mempool.c +++ b/src/wg_mempool.c @@ -60,8 +60,6 @@ typedef struct mempool_s { size_t blocksize; } mempool_t; -#define wg_memset memset - static void addblock( mempool_t *h ) { memblock_t *block; @@ -109,10 +107,10 @@ extern void wgmempool_Done( void *handle ) p = h->first; while (p) { memblock_t *next = p->next; - wg_free( p->pool ); + free( p->pool ); - wg_memset( p, 0, sizeof(memblock_t)); /* for safety */ - wg_free( p ); + memset( p, 0, sizeof(memblock_t)); /* for safety */ + free( p ); p = next; } @@ -121,16 +119,16 @@ extern void wgmempool_Done( void *handle ) p = h->spare; while (p) { memblock_t *next = p->next; - wg_free( p->pool ); + free( p->pool ); - wg_memset( p, 0, sizeof(memblock_t)); /* for safety */ - wg_free( p ); + memset( p, 0, sizeof(memblock_t)); /* for safety */ + free( p ); p = next; } - wg_memset( h, 0, sizeof(mempool_t)); /* for safety */ - wg_free(h); + memset( h, 0, sizeof(mempool_t)); /* for safety */ + free(h); } extern void wgmempool_Reset( void *handle ) |