[Nagios-devel] [PATCH] Fix some minor memory leaks

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

[Nagios-devel] [PATCH] Fix some minor memory leaks

Post by Guest »


--=-W1NtOoxyd/ipG+s37I7E
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

There are some instances where asprintf() are used to construct strings
but the resulting allocated memory isn't ever free()d. This patch
(against Nagios 3.2.0) makes sure that the strings allocated by asprintf
are freed when they are no longer needed.

Thanks,
Sean


--=-W1NtOoxyd/ipG+s37I7E
Content-Disposition: attachment; filename="nagios-fix-asprintf-leaks.patch"
Content-Type: text/x-patch; name="nagios-fix-asprintf-leaks.patch";
charset="UTF-8"
Content-Transfer-Encoding: 7bit

diff --git a/base/nebmods.c b/base/nebmods.c
index acf7cc4..c6945d5 100644
--- a/base/nebmods.c
+++ b/base/nebmods.c
@@ -244,8 +244,10 @@ int neb_load_module(nebmodule *mod){
logit(NSLOG_RUNTIME_ERROR,FALSE,"Error: Could not delete temporary file '%s' used for module '%s'. The module will be unloaded: %s\n",output_file,mod->filename,strerror(errno));
neb_unload_module(mod,NEBMODULE_FORCE_UNLOAD,NEBMODULE_ERROR_API_VERSION);

+ my_free(output_file);
return ERROR;
}
+ my_free(output_file);

/* find module API version */
#ifdef USE_LTDL
diff --git a/base/netutils.c b/base/netutils.c
index 443bc91..fabad77 100644
--- a/base/netutils.c
+++ b/base/netutils.c
@@ -47,6 +47,7 @@ int my_tcp_connect(char *host_name, int port, int *sd, int timeout){

asprintf(&port_str,"%d",port);
result=getaddrinfo(host_name,port_str,&hints,&res);
+ my_free(port_str);
if(result!=0){
/*printf("GETADDRINFO: %s (%s) = %s\n",host_name,port_str,gai_strerror(result));*/
return ERROR;
diff --git a/base/utils.c b/base/utils.c
index ad4b627..726f307 100644
--- a/base/utils.c
+++ b/base/utils.c
@@ -4444,6 +4444,7 @@ int query_update_api(void){
char *api_query=NULL;
char *api_query_opts=NULL;
char *buf=NULL;
+ char *buf2=NULL;
char recv_buf[1024];
int report_install=FALSE;
int result=OK;
@@ -4471,19 +4472,32 @@ int query_update_api(void){
}

/* generate the query */
- asprintf(&api_query,"v=1&product=nagios&tinycheck=1&stableonly=1");
- if(bare_update_check==FALSE)
- asprintf(&api_query,"%s&version=%s%s",api_query,PROGRAM_VERSION,(api_query_opts==NULL)?"":api_query_opts);
+ asprintf(&buf2,"v=1&product=nagios&tinycheck=1&stableonly=1");
+ if(bare_update_check==FALSE) {
+ asprintf(&api_query,"%s&version=%s%s",buf2,PROGRAM_VERSION,(api_query_opts==NULL)?"":api_query_opts);
+ my_free(buf2);
+ } else {
+ api_query = buf2;
+ }
+

/* generate the HTTP request */
asprintf(&buf,"POST %s HTTP/1.0\r\n",api_path);
- asprintf(&buf,"%sUser-Agent: Nagios/%s\r\n",buf,PROGRAM_VERSION);
- asprintf(&buf,"%sConnection: close\r\n",buf);
- asprintf(&buf,"%sHost: %s\r\n",buf,api_server);
- asprintf(&buf,"%sContent-Type: application/x-www-form-urlencoded\r\n",buf);
- asprintf(&buf,"%sContent-Length: %d\r\n",buf,strlen(api_query));
- asprintf(&buf,"%s\r\n",buf);
- asprintf(&buf,"%s%s\r\n",buf,api_query);
+ asprintf(&buf2,"%sUser-Agent: Nagios/%s\r\n",buf,PROGRAM_VERSION);
+ my_free(buf);
+ asprintf(&buf,"%sConnection: close\r\n",buf2);
+ my_free(buf2);
+ asprintf(&buf2,"%sHost: %s\r\n",buf,api_server);
+ my_free(buf);
+ asprintf(&buf,"%sContent-Type: application/x-www-form-urlencoded\r\n",buf2);
+ my_free(buf2);
+ asprintf(&buf2,"%sContent-Length: %d\r\n",buf,strlen(api_query));
+ my_free(buf);
+ asprintf(&buf,"%s\r\n",buf2);
+ my_free(buf2);
+ asprintf(&buf2,"%s%s\r\n",buf,api_query);
+ my_free(buf);
+ buf=buf2;

/*
printf("SENDING...\n");

--=-W1NtOoxyd/ipG+s37I7E--






This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]
Locked