2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-28 13:08:06 +00:00

Rename 'free' variable to 'nfree' to not clash with free()

The beauty and horrors of the C - the compiler properly detects variable
shadowing, but you can freely shadow a standard function 'free()' with
variable called 'free'.  And if you reference 'free()' just as 'free'
you get the function pointer which means you can do also pointer
arithmetics, so 'free > 0' is always valid even when you delete the
local variable.

Replace the local variables 'free' with a name that doesn't shadow the
'free()' function to prevent future hard to detect bugs.
This commit is contained in:
Ondřej Surý 2025-07-22 09:32:56 +02:00
parent 98148d8507
commit 855960ce46
No known key found for this signature in database
GPG Key ID: 2820F37E873DEA41
2 changed files with 21 additions and 20 deletions

View File

@ -716,7 +716,7 @@ chunk_free(dns_qp_t *qp, dns_qpchunk_t chunk) {
*/
static void
recycle(dns_qp_t *qp) {
unsigned int free = 0;
unsigned int nfree = 0;
isc_nanosecs_t start = isc_time_monotonic();
@ -725,15 +725,15 @@ recycle(dns_qp_t *qp) {
qp->usage[chunk].exists && !qp->usage[chunk].immutable)
{
chunk_free(qp, chunk);
free++;
nfree++;
}
}
isc_nanosecs_t time = isc_time_monotonic() - start;
atomic_fetch_add_relaxed(&recycle_time, time);
if (free > 0) {
LOG_STATS("qp recycle" PRItime "free %u chunks", time, free);
if (nfree > 0) {
LOG_STATS("qp recycle" PRItime "free %u chunks", time, nfree);
LOG_STATS("qp recycle leaf %u live %u used %u free %u hold %u",
qp->leaf_count, qp->used_count - qp->free_count,
qp->used_count, qp->free_count, qp->hold_count);
@ -757,7 +757,7 @@ reclaim_chunks_cb(struct rcu_head *arg) {
* If chunk_max is zero, chunks have already been freed.
*/
if (qp->chunk_max != 0) {
unsigned int free = 0;
unsigned int nfree = 0;
isc_nanosecs_t start = isc_time_monotonic();
INSIST(QP_VALID(qp));
@ -769,16 +769,16 @@ reclaim_chunks_cb(struct rcu_head *arg) {
qp->usage[chunk].snapfree = true;
} else {
chunk_free(qp, chunk);
free++;
nfree++;
}
}
isc_nanosecs_t time = isc_time_monotonic() - start;
recycle_time += time;
if (free > 0) {
if (nfree > 0) {
LOG_STATS("qp reclaim" PRItime "free %u chunks", time,
free);
nfree);
LOG_STATS(
"qp reclaim leaf %u live %u used %u free %u "
"hold %u",
@ -852,7 +852,7 @@ reclaim_chunks(dns_qpmulti_t *multi) {
*/
static void
marksweep_chunks(dns_qpmulti_t *multi) {
unsigned int free = 0;
unsigned int nfree = 0;
isc_nanosecs_t start = isc_time_monotonic();
@ -873,15 +873,15 @@ marksweep_chunks(dns_qpmulti_t *multi) {
qpw->usage[chunk].snapmark = false;
if (qpw->usage[chunk].snapfree && !qpw->usage[chunk].snapshot) {
chunk_free(qpw, chunk);
free++;
nfree++;
}
}
isc_nanosecs_t time = isc_time_monotonic() - start;
recycle_time += time;
if (free > 0) {
LOG_STATS("qp marksweep" PRItime "free %u chunks", time, free);
if (nfree > 0) {
LOG_STATS("qp marksweep" PRItime "free %u chunks", time, nfree);
LOG_STATS(
"qp marksweep leaf %u live %u used %u free %u hold %u",
qpw->leaf_count, qpw->used_count - qpw->free_count,
@ -1319,7 +1319,7 @@ dns_qpmulti_commit(dns_qpmulti_t *multi, dns_qp_t **qptp) {
*/
void
dns_qpmulti_rollback(dns_qpmulti_t *multi, dns_qp_t **qptp) {
unsigned int free = 0;
unsigned int nfree = 0;
REQUIRE(QPMULTI_VALID(multi));
REQUIRE(multi->writer.transaction_mode == QP_UPDATE);
@ -1342,7 +1342,7 @@ dns_qpmulti_rollback(dns_qpmulti_t *multi, dns_qp_t **qptp) {
INSIST(!multi->rollback->usage[chunk].exists);
multi->rollback->base->ptr[chunk] = NULL;
}
free++;
nfree++;
}
}
@ -1365,7 +1365,7 @@ dns_qpmulti_rollback(dns_qpmulti_t *multi, dns_qp_t **qptp) {
isc_nanosecs_t time = isc_time_monotonic() - start;
atomic_fetch_add_relaxed(&rollback_time, time);
LOG_STATS("qp rollback" PRItime "free %u chunks", time, free);
LOG_STATS("qp rollback" PRItime "free %u chunks", time, nfree);
*qptp = NULL;
UNLOCK(&multi->mutex);

View File

@ -177,8 +177,8 @@ qp_test_dumpmulti(dns_qpmulti_t *multi) {
void
qp_test_dumpchunks(dns_qp_t *qp) {
dns_qpcell_t used = 0;
dns_qpcell_t free = 0;
dns_qpcell_t used_count = 0;
dns_qpcell_t free_count = 0;
dumpqp(qp, "qp");
for (dns_qpchunk_t c = 0; c < qp->chunk_max; c++) {
printf("qp %p chunk %u base %p "
@ -186,10 +186,11 @@ qp_test_dumpchunks(dns_qp_t *qp) {
qp, c, qp->base->ptr[c], qp->usage[c].used,
qp->usage[c].free, qp->usage[c].immutable,
qp->usage[c].discounted);
used += qp->usage[c].used;
free += qp->usage[c].free;
used_count += qp->usage[c].used;
free_count += qp->usage[c].free;
}
printf("qp %p total used %u free %u\n", qp, used, free);
printf("qp %p total used %" PRIu32 " free %" PRIu32 "\n", qp,
used_count, free_count);
fflush(stdout);
}