Query module FileMayNotBeClosed

name
Open file may not be closed
description
A function may return before closing a file that was opened in the function. Closing resources in the same function that opened them ties the lifetime of the resource to that of the function call, making it easier to avoid and detect resource leaks.
kind
problem
id
cpp/file-may-not-be-closed
problem.severity
warning
tags
efficiency security external/cwe/cwe-775

Imports

Predicates

assignedToFieldOrGlobal
fcloseCallOrIndirect
fopenCallOrIndirect
fopenDefinition
fopenReaches

The value returned by fopen def has not been closed, confirmed to be null, or potentially leaked globally upon reaching node (regardless of what variable it’s still held in, if any).

fopenVariableReaches

The value from fopen at def is still held in Variable v upon entering node.

mayCallFunction

‘call’ is either a direct call to f, or a possible call to f via a function pointer.

Classes

FOpenReachability
FOpenVariableReachability
MinusOne

Extend the NullValue class used by Nullness.qll to include simple -1 as a ‘null’ value (for example ‘open’ returns -1 if there was an error)