Mercurial > hg > freeDiameter
comparison extensions/rt_ereg/rtereg.c @ 1546:c48725959e63
rt_ereg: improve memory handling
author | Thomas Klausner <tk@giga.or.at> |
---|---|
date | Mon, 15 Jun 2020 20:33:03 +0200 |
parents | d70f5f6cb306 |
children | 566bb46cc73f |
comparison
equal
deleted
inserted
replaced
1545:3a40bd55ee74 | 1546:c48725959e63 |
---|---|
188 /* Lock the buffer */ | 188 /* Lock the buffer */ |
189 CHECK_POSIX( pthread_mutex_lock(&mtx) ); | 189 CHECK_POSIX( pthread_mutex_lock(&mtx) ); |
190 | 190 |
191 /* Augment the buffer if needed */ | 191 /* Augment the buffer if needed */ |
192 if (avp_hdr->avp_value->os.len >= bufsz) { | 192 if (avp_hdr->avp_value->os.len >= bufsz) { |
193 CHECK_MALLOC_DO( buf = realloc(buf, avp_hdr->avp_value->os.len + 1), | 193 char *newbuf; |
194 CHECK_MALLOC_DO( newbuf = realloc(buf, avp_hdr->avp_value->os.len + 1), | |
194 { pthread_mutex_unlock(&mtx); return ENOMEM; } ); | 195 { pthread_mutex_unlock(&mtx); return ENOMEM; } ); |
196 /* Update buffer and buffer size */ | |
197 buf = newbuf; | |
198 bufsz = avp_hdr->avp_value->os.len + 1; | |
195 } | 199 } |
196 | 200 |
197 /* Copy the AVP value */ | 201 /* Copy the AVP value */ |
198 memcpy(buf, avp_hdr->avp_value->os.data, avp_hdr->avp_value->os.len); | 202 memcpy(buf, avp_hdr->avp_value->os.data, avp_hdr->avp_value->os.len); |
199 buf[avp_hdr->avp_value->os.len] = '\0'; | 203 buf[avp_hdr->avp_value->os.len] = '\0'; |
317 } | 321 } |
318 | 322 |
319 static int rtereg_init_config(void) | 323 static int rtereg_init_config(void) |
320 { | 324 { |
321 /* Initialize the configuration */ | 325 /* Initialize the configuration */ |
322 if ((rtereg_conf=malloc(sizeof(*rtereg_conf))) == NULL) { | 326 if ((rtereg_conf=calloc(sizeof(*rtereg_conf), 1)) == NULL) { |
323 LOG_E("[rt_ereg] malloc failured"); | 327 LOG_E("[rt_ereg] malloc failured"); |
324 return 1; | 328 return 1; |
325 } | 329 } |
326 rtereg_conf_size = 1; | 330 rtereg_conf_size = 1; |
327 memset(rtereg_conf, 0, sizeof(*rtereg_conf)); | |
328 | 331 |
329 /* Parse the configuration file */ | 332 /* Parse the configuration file */ |
330 CHECK_FCT( rtereg_conf_handle(rt_ereg_config_file) ); | 333 CHECK_FCT( rtereg_conf_handle(rt_ereg_config_file) ); |
331 | 334 |
332 return 0; | 335 return 0; |
369 CHECK_FCT_DO( fd_rt_out_unregister ( rtereg_hdl, NULL ), /* continue */ ); | 372 CHECK_FCT_DO( fd_rt_out_unregister ( rtereg_hdl, NULL ), /* continue */ ); |
370 | 373 |
371 #ifndef HAVE_REG_STARTEND | 374 #ifndef HAVE_REG_STARTEND |
372 free(buf); | 375 free(buf); |
373 buf = NULL; | 376 buf = NULL; |
377 bufsz = 0; | |
374 #endif /* HAVE_REG_STARTEND */ | 378 #endif /* HAVE_REG_STARTEND */ |
375 | 379 |
376 if (pthread_rwlock_wrlock(&rte_lock) != 0) { | 380 if (pthread_rwlock_wrlock(&rte_lock) != 0) { |
377 fd_log_error("%s: write-locking failed in fini, giving up", MODULE_NAME); | 381 fd_log_error("%s: write-locking failed in fini, giving up", MODULE_NAME); |
378 return; | 382 return; |