Skip to content

Commit

Permalink
Fixing memory leak, addresses #75
Browse files Browse the repository at this point in the history
  • Loading branch information
vfonov committed Sep 13, 2016
1 parent 433acc1 commit eb7eb48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions libsrc/minc_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ static const char *_CONFIG_VAR[]=
"MINC_PREFER_V2_API"
};

enum {
_MICFG_MAX_STRING_LENGTH=256
};

/*settings cache*/
static const char*_CONFIG_VAL[MICFG_COUNT]={(const char*)0};
static int _CONFIG_PRESENT[MICFG_COUNT]={0};
static char _CONFIG_VAL[MICFG_COUNT][_MICFG_MAX_STRING_LENGTH];
static int _CONFIG_PRESENT[MICFG_COUNT]={0};
static int _CONFIG_INIT[MICFG_COUNT]={0};

/** Simple function to read a user's .mincrc file, if present.
*/
Expand Down Expand Up @@ -78,26 +83,26 @@ const char * miget_cfg_str(int id)
{
if(id<0 || id>=MICFG_COUNT) return "";

if(!_CONFIG_VAL[id])
if(!_CONFIG_INIT[id])
{
const char *name=_CONFIG_VAR[id];
char buffer[256];
char buffer[_MICFG_MAX_STRING_LENGTH];
char *var_ptr;

if ((var_ptr = getenv(name)) != NULL) {
strncpy(buffer, var_ptr, sizeof(buffer) - 1);
strncpy(buffer, var_ptr, _MICFG_MAX_STRING_LENGTH - 1);
_CONFIG_PRESENT[id]=1;
}
else {
if (!miread_cfg(name, buffer, sizeof(buffer))) {
} else {
if (!miread_cfg(name, buffer, _MICFG_MAX_STRING_LENGTH-1)) {
_CONFIG_PRESENT[id]=0;
buffer[0]='\0';
} else {
_CONFIG_PRESENT[id]=1;
}
}
/*small memory leak here, unless we deallocate the settings cache*/
_CONFIG_VAL[id]=strdup(buffer);
strncpy(_CONFIG_VAL[id],buffer,_MICFG_MAX_STRING_LENGTH-1);
_CONFIG_VAL[id][_MICFG_MAX_STRING_LENGTH-1]='\0';
_CONFIG_INIT[id]=1;
}
return _CONFIG_VAL[id];
}
Expand Down
2 changes: 1 addition & 1 deletion libsrc/minc_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ extern int miget_cfg_present(int);
extern int miget_cfg_bool(int);
extern int miget_cfg_int(int);
extern const char * miget_cfg_str(int);
extern double miget_cfg_double(int);
extern double miget_cfg_double(int);

#endif /* __MINC_CONFIG_H__ */

0 comments on commit eb7eb48

Please sign in to comment.