Safeguard
Application Security•

CWE-568 finalize() Without super.finalize() in Java

A finalize() that skips super.finalize() can leave parent cleanup undone. Learn why CWE-568 matters and how to fix it.

Safeguard Security Team
2 min read

CWE-568 is a Java weakness: a class defines finalize() but does not call super.finalize(). The Java Language Specification calls it good practice for a finalize() method to do so.

If a parent class releases resources in its own finalizer, skipping the call means that cleanup never happens. Finalizers are also deprecated in modern Java, but plenty of older code still uses them.

How it happens

MITRE's example:

// Vulnerable
protected void finalize() {
    discardNative();
}

The subclass cleans up its own native resource and forgets the parent. Anything the superclass held, such as handles, buffers or locks, stays allocated until the process ends.

What an attacker gains

MITRE lists quality degradation only. The indirect risk is a resource leak that someone can trigger repeatedly to exhaust memory or handles.

How to find it

Automated static analysis can find some instances, per MITRE. A search for void finalize without super.finalize nearby finds the rest.

How to fix it

MITRE's mitigation: call super.finalize().

// Fixed: finally ensures the parent always runs
protected void finalize() throws Throwable {
    try {
        discardNative();
    } finally {
        super.finalize();
    }
}

Better still, move to try with resources and AutoCloseable, or java.lang.ref.Cleaner, and retire finalizers.

Quick check

Why call super.finalize()?

So the parent class can release whatever it holds. Skipping it leaves that cleanup undone.

The free in-browser scan at gold.safeguard.sh/scan lists the dependencies in your manifest. No login is needed, the file stays on your machine, and it handles up to 100 dependencies.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.