changeset 1546:c48725959e63

rt_ereg: improve memory handling
author Thomas Klausner <tk@giga.or.at>
date Mon, 15 Jun 2020 20:33:03 +0200
parents 3a40bd55ee74
children 44bb63ba9c2c
files extensions/rt_ereg/rtereg.c
diffstat 1 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/extensions/rt_ereg/rtereg.c	Mon Jun 15 20:32:03 2020 +0200
+++ b/extensions/rt_ereg/rtereg.c	Mon Jun 15 20:33:03 2020 +0200
@@ -190,8 +190,12 @@
 
 					/* Augment the buffer if needed */
 					if (avp_hdr->avp_value->os.len >= bufsz) {
-						CHECK_MALLOC_DO( buf = realloc(buf, avp_hdr->avp_value->os.len + 1),
+						char *newbuf;
+						CHECK_MALLOC_DO( newbuf = realloc(buf, avp_hdr->avp_value->os.len + 1),
 							{ pthread_mutex_unlock(&mtx); return ENOMEM; } );
+						/* Update buffer and buffer size */
+						buf = newbuf;
+						bufsz = avp_hdr->avp_value->os.len + 1;
 					}
 
 					/* Copy the AVP value */
@@ -319,12 +323,11 @@
 static int rtereg_init_config(void)
 {
 	/* Initialize the configuration */
-	if ((rtereg_conf=malloc(sizeof(*rtereg_conf))) == NULL) {
+	if ((rtereg_conf=calloc(sizeof(*rtereg_conf), 1)) == NULL) {
 		LOG_E("[rt_ereg] malloc failured");
 		return 1;
 	}
 	rtereg_conf_size = 1;
-	memset(rtereg_conf, 0, sizeof(*rtereg_conf));
 
 	/* Parse the configuration file */
 	CHECK_FCT( rtereg_conf_handle(rt_ereg_config_file) );
@@ -371,6 +374,7 @@
 #ifndef HAVE_REG_STARTEND
 	free(buf);
 	buf = NULL;
+	bufsz = 0;
 #endif /* HAVE_REG_STARTEND */
 
 	if (pthread_rwlock_wrlock(&rte_lock) != 0) {
"Welcome to our mercurial repository"