[Snowball-discuss] Memory Leak in sb_stemmer_new()

Ralf Junker ralfjunker at gmx.de
Mon Nov 30 11:24:50 GMT 2009


sb_stemmer_new() leaks memory if either the algorithm or charenc parameters are invalid, i.e. they do not match any of the bult-in algorithms or encodings.

The fix is simple and straightforward - just free the memory allocated to the stemmer variable prior to return NULL.

Ralf

------------------------------------------------------

Fixed version:

extern struct sb_stemmer *
sb_stemmer_new(const char * algorithm, const char * charenc)
{
    stemmer_encoding_t enc;
    struct stemmer_modules * module;
    struct sb_stemmer * stemmer =
	    (struct sb_stemmer *) malloc(sizeof(struct sb_stemmer));
    if (stemmer == NULL) return NULL;
    enc = sb_getenc(charenc);
    if (enc == ENC_UNKNOWN)
    {
      free(stemmer); /* Was missing, added. */
      return NULL;
    }
    for (module = modules; module->name != 0; module++) {
	if (strcmp(module->name, algorithm) == 0 && module->enc == enc) break;
    }
    if (module->name == NULL)
    {
      free(stemmer); /* Was missing, added. */
      return NULL;
    }
    stemmer->create = module->create;
    stemmer->close = module->close;
    stemmer->stem = module->stem;
    stemmer->env = stemmer->create();
    if (stemmer->env == NULL)
    {
        sb_stemmer_delete(stemmer);
        return NULL;
    }
    return stemmer;
}




More information about the Snowball-discuss mailing list