Merge bitcoin/bitcoin#36805: Enclose all header content in include guards
What changed, and why it matters
This commit is a code-quality cleanup, not a security fix. It moves include guards in three header files so they wrap all content, and updates a lint script to enforce that rule going forward. Include guards prevent the compiler from accidentally processing the same header twice, which avoids build errors and wasted compile time. There is no runtime security impact.
No security action required. Treat as normal code-quality/maintenance merge.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The merge commit encloses all content in three headers (src/net_permissions.h, src/txgraph.h, src/wallet/test/init_test_fixture.h) inside their #ifndef/#define/#endif include guards. It also strengthens test/lint/lint-include-guards.py to reject any non-empty, non-//-comment lines outside the guard. This is a compile-time hygiene change with no functional or security behavior change in the built software.
Changed components
src/net_permissions.hsrc/txgraph.hsrc/wallet/test/init_test_fixture.htest/lint/lint-include-guards.pyInspect captured patch +17 / −17
### src/net_permissions.h
@@ -2,16 +2,16 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#ifndef BITCOIN_NET_PERMISSIONS_H
+#define BITCOIN_NET_PERMISSIONS_H
+
#include <netaddress.h>
#include <netbase.h>
#include <string>
#include <type_traits>
#include <vector>
-#ifndef BITCOIN_NET_PERMISSIONS_H
-#define BITCOIN_NET_PERMISSIONS_H
-
struct bilingual_str;
extern const std::vector<std::string> NET_PERMISSIONS_DOC;
### src/txgraph.h
@@ -2,6 +2,9 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#ifndef BITCOIN_TXGRAPH_H
+#define BITCOIN_TXGRAPH_H
+
#include <compare>
#include <cstdint>
#include <functional>
@@ -12,9 +15,6 @@
#include <util/feefrac.h>
-#ifndef BITCOIN_TXGRAPH_H
-#define BITCOIN_TXGRAPH_H
-
inline constexpr unsigned MAX_CLUSTER_COUNT_LIMIT{64};
/** Data structure to encapsulate fees, sizes, and dependencies for a set of transactions.
### src/wallet/test/init_test_fixture.h
@@ -24,5 +24,6 @@ struct InitWalletDirTestingSetup: public BasicTestingSetup {
std::unique_ptr<interfaces::WalletLoader> m_wallet_loader;
};
-#endif // BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H
} // namespace wallet
+
+#endif // BITCOIN_WALLET_TEST_INIT_TEST_FIXTURE_H
### test/lint/lint-include-guards.py
@@ -8,7 +8,6 @@
Check include guards.
"""
-import re
import sys
from subprocess import check_output
@@ -68,25 +67,25 @@ def main():
for header_file in header_file_lst:
header_id = _get_header_id(header_file)
- regex_pattern = f'^#(ifndef|define|endif //) {header_id}'
-
with open(header_file, 'r') as f:
header_file_contents = f.readlines()
- count = 0
- for header_file_contents_string in header_file_contents:
- include_guard_lst = re.findall(
- regex_pattern, header_file_contents_string)
-
- count += len(include_guard_lst)
+ # Filter out empty lines ('\n') and lines starting with '//'
+ code = [line for line in header_file_contents if line != '\n' and not line.startswith('//')]
- if count != 3:
+ if (
+ len(code) < 3
+ or code[0] != f'#ifndef {header_id}\n'
+ or code[1] != f'#define {header_id}\n'
+ or code[-1] != f'#endif // {header_id}\n'
+ ):
print(f'{header_file} seems to be missing the expected '
'include guard to prevent the double inclusion problem:')
print(f' #ifndef {header_id}')
print(f' #define {header_id}')
print(' ...')
print(f' #endif // {header_id}\n')
+ print("Note that only empty lines or //-comment lines are allowed outside the include guard.")
exit_code = 1
sys.exit(exit_code)Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.