Browse code

core/mem: moved safety check for null before range check in *free()

- affects only when memory debug is enabled at compilation
- apparently libssl has some free(0) which makes it not possible to work
with memory debugging (reported on irc channel)

Daniel-Constantin Mierla authored on 27/09/2012 12:57:51
Showing 2 changed files
... ...
@@ -507,18 +507,21 @@ void fm_free(struct fm_block* qm, void* p)
507 507
 
508 508
 #ifdef DBG_F_MALLOC
509 509
 	MDBG("fm_free(%p, %p), called from %s: %s(%d)\n", qm, p, file, func, line);
510
+#endif
511
+	if (p==0) {
512
+		LOG(L_WARN, "WARNING:fm_free: free(0) called\n");
513
+		return;
514
+	}
515
+#ifdef DBG_F_MALLOC
510 516
 	if (p>(void*)qm->last_frag || p<(void*)qm->first_frag){
511 517
 		LOG(L_CRIT, "BUG: fm_free: bad pointer %p (out of memory block!),"
512 518
 				" called from %s: %s(%d) - aborting\n", p,
513 519
 				file, func, line);
514 520
 		if(likely(cfg_get(core, core_cfg, mem_safety)==0))
515 521
 			abort();
522
+		else return;
516 523
 	}
517 524
 #endif
518
-	if (p==0) {
519
-		LOG(L_WARN, "WARNING:fm_free: free(0) called\n");
520
-		return;
521
-	}
522 525
 	f=(struct fm_frag*) ((char*)p-sizeof(struct fm_frag));
523 526
 #ifdef DBG_F_MALLOC
524 527
 	MDBG("fm_free: freeing block alloc'ed from %s: %s(%ld)\n",
... ...
@@ -435,18 +435,25 @@ void qm_free(struct qm_block* qm, void* p)
435 435
 
436 436
 #ifdef DBG_QM_MALLOC
437 437
 	MDBG("qm_free(%p, %p), called from %s: %s(%d)\n", qm, p, file, func, line);
438
+#endif
439
+
440
+	if (p==0) {
441
+		LOG(L_WARN, "WARNING:qm_free: free(0) called\n");
442
+		return;
443
+	}
444
+
445
+#ifdef DBG_QM_MALLOC
438 446
 	if (p>(void*)qm->last_frag_end || p<(void*)qm->first_frag){
439 447
 		LOG(L_CRIT, "BUG: qm_free: bad pointer %p (out of memory block!)"
440 448
 				" called from %s: %s(%d) - aborting\n", p, file, func, line);
441 449
 		if(likely(cfg_get(core, core_cfg, mem_safety)==0))
442 450
 			abort();
451
+		else return;
443 452
 	}
444 453
 #endif
445
-	if (p==0) {
446
-		LOG(L_WARN, "WARNING:qm_free: free(0) called\n");
447
-		return;
448
-	}
454
+
449 455
 	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
456
+
450 457
 #ifdef DBG_QM_MALLOC
451 458
 	qm_debug_frag(qm, f);
452 459
 	if (f->u.is_free){
... ...
@@ -455,6 +462,7 @@ void qm_free(struct qm_block* qm, void* p)
455 455
 				p, file, func, line, f->file, f->func, f->line);
456 456
 		if(likely(cfg_get(core, core_cfg, mem_safety)==0))
457 457
 			abort();
458
+		else return;
458 459
 	}
459 460
 	MDBG("qm_free: freeing frag. %p alloc'ed from %s: %s(%ld)\n",
460 461
 			f, f->file, f->func, f->line);