tracker-0.16.2/src/libstemmer/libstemmer.c

Location Tool Test ID Function Issue
libstemmer.c:79:2 clang-analyzer Potential leak of memory pointed to by 'stemmer'
  1 /*
  2  * Copyright (c) 2001, Dr Martin Porter
  3  * All rights reserved.
  4  *
  5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8  * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9  *
 10  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 11  */
 12 
 13 
 14 
 15 #include <stdlib.h>
 16 #include <string.h>
 17 
 18 #include "libstemmer.h"
 19 #include "api.h"
 20 #include "modules.h"
 21 
 22 struct sb_stemmer {
 23     struct SN_env * (*create)(void);
 24     void (*close)(struct SN_env *);
 25     int (*stem)(struct SN_env *);
 26 
 27     struct SN_env * env;
 28 };
 29 
 30 extern const char **
 31 sb_stemmer_list(void)
 32 {
 33     return algorithm_names;
 34 }
 35 
 36 static stemmer_encoding sb_getenc(const char * charenc)
 37 {
 38     struct stemmer_encoding * encoding;
 39     if (charenc == NULL) return ENC_UTF_8;
 40     for (encoding = encodings; encoding->name != 0; encoding++) {
 41 	if (strcmp(encoding->name, charenc) == 0) break;
 42     }
 43     if (encoding->name == NULL) return ENC_UNKNOWN;
 44     return encoding->enc;
 45 }
 46 
 47 extern struct sb_stemmer *
 48 sb_stemmer_new(const char * algorithm, const char * charenc)
 49 {
 50     stemmer_encoding enc;
 51     struct stemmer_modules * module;
 52     struct sb_stemmer * stemmer =
 53 	    (struct sb_stemmer *) malloc(sizeof(struct sb_stemmer));
 54     if (stemmer == NULL) return NULL;
 55     enc = sb_getenc(charenc);
 56     if (enc == ENC_UNKNOWN) 
 57     {
 58             free(stemmer);
 59             return NULL;
 60     }
 61 
 62     for (module = modules; module->name != 0; module++) {
 63 	if (strcmp(module->name, algorithm) == 0 && module->enc == enc) break;
 64     }
 65     if (module->name == NULL) 
 66     {
 67             free(stemmer);
 68             return NULL;
 69     }
 70 
 71     stemmer->create = module->create;
 72     stemmer->close = module->close;
 73     stemmer->stem = module->stem;
 74 
 75     stemmer->env = stemmer->create();
 76     if (stemmer->env == NULL)
 77     {
 78 	sb_stemmer_delete(stemmer);
 79 	return NULL;
Potential leak of memory pointed to by 'stemmer'
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

80 } 81 82 return stemmer; 83 } 84 85 void 86 sb_stemmer_delete(struct sb_stemmer * stemmer) 87 { 88 if (stemmer == 0) return; 89 if (stemmer->close == 0) return; 90 stemmer->close(stemmer->env); 91 stemmer->close = 0; 92 free(stemmer); 93 } 94 95 const sb_symbol * 96 sb_stemmer_stem(struct sb_stemmer * stemmer, const sb_symbol * word, int size) 97 { 98 int ret; 99 if (SN_set_current(stemmer->env, size, (const symbol *)(word))) 100 { 101 stemmer->env->l = 0; 102 return NULL; 103 } 104 ret = stemmer->stem(stemmer->env); 105 if (ret < 0) return NULL; 106 stemmer->env->p[stemmer->env->l] = 0; 107 return (const sb_symbol *)(stemmer->env->p); 108 } 109 110 int 111 sb_stemmer_length(struct sb_stemmer * stemmer) 112 { 113 return stemmer->env->l; 114 }