sqc 0.4.13

Software Code Quality - CERT C compliance checker
[metadata]
id = "FIO51-C"
type = "recommendation"
category = "FIO"
number = 51
title = "PP. Close files when they are no longer needed"
description = """
A call to thestd::basic_filebuf<T>::open()function must be matched with a call
tostd::basic_filebuf<T>::close()before the lifetime of the last pointer that
stores the return value of the call has ended or before normal program
termination, whichever occurs first. Note
thatstd::basic_ifstream<T>,std::basic_ofstream<T>, andstd::basic_fstream<T>all
maintain an internal reference to astd::basic_filebuf<T>object on
whichopen()andclose()are called as needed. Properly managing an object of one of
these types (by not leaking the object) is sufficient to ensure compliance with
this rule. Often, the best solution is to use the stream object by value
semantics instead of via dynamic memory allocation, ensuring compliance
withMEM51-CPP. Properly deallocate dynamically allocated resources. However,
that is still insufficient for situations in which destructors are not
automatically called. In this noncompliant code example,
astd::fstreamobjectfileis constructed. The constructor
forstd::fstreamcallsstd::basic_filebuf<T>::open(), and the
defaultstd::terminate_handlercalled bystd::terminate()isstd::abort(), which does
not call destructors. Consequently, the underlyingstd::basic_filebuf<T>object
maintained by the object is not properly closed.
"""
severity = "Medium"
likelihood = "Unlikely"
priority = "P2"
level = "L3"
cert_version = "2016 Edition (Wiki)"
last_modified = "Jul 02, 2025"

[rules.cert_c.FIO51-C]
enabled = true

[references]
wiki = "https://wiki.sei.cmu.edu/confluence/display/cplusplus/FIO51-CPP.+Close+files+when+they+are+no+longer+needed"