Skip to content

Commit fa24fdc

Browse files
author
MarcoFalke
committed
lint: Remove string exclusion from locale check
The string exclusion would fail to detect `"bla" + wrong()`. Also, remove /* */ comment exclusion, which would fail to detect stuff like `/* bla */ wrong()`. Instead, require the function to be called by adding \\( to the regex. Finally, also remove the section in the dev notes, because: * It was outdated and missing some functions such as std::to_string in the list. * The maintenance overhead of having to update two places is fragile and questionable. * Many other linters are also not mentioned in the dev notes, even though they are important. * A dev (and CI) is more likely to run the linters than to read the dev notes. * The dev notes are more than 1000 lines of dense information. It would be easier to digest if they focused on the important stuff that is not checked by automated tools.
1 parent fa2c548 commit fa24fdc

File tree

2 files changed

+5
-32
lines changed

2 files changed

+5
-32
lines changed

doc/developer-notes.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,34 +1013,6 @@ Strings and formatting
10131013
10141014
- *Rationale*: These functions do overflow checking and avoid pesky locale issues.
10151015
1016-
- Avoid using locale dependent functions if possible. You can use the provided
1017-
[`lint-locale-dependence.py`](/test/lint/lint-locale-dependence.py)
1018-
to check for accidental use of locale dependent functions.
1019-
1020-
- *Rationale*: Unnecessary locale dependence can cause bugs that are very tricky to isolate and fix.
1021-
1022-
- These functions are known to be locale dependent:
1023-
`alphasort`, `asctime`, `asprintf`, `atof`, `atoi`, `atol`, `atoll`, `atoq`,
1024-
`btowc`, `ctime`, `dprintf`, `fgetwc`, `fgetws`, `fprintf`, `fputwc`,
1025-
`fputws`, `fscanf`, `fwprintf`, `getdate`, `getwc`, `getwchar`, `isalnum`,
1026-
`isalpha`, `isblank`, `iscntrl`, `isdigit`, `isgraph`, `islower`, `isprint`,
1027-
`ispunct`, `isspace`, `isupper`, `iswalnum`, `iswalpha`, `iswblank`,
1028-
`iswcntrl`, `iswctype`, `iswdigit`, `iswgraph`, `iswlower`, `iswprint`,
1029-
`iswpunct`, `iswspace`, `iswupper`, `iswxdigit`, `isxdigit`, `mblen`,
1030-
`mbrlen`, `mbrtowc`, `mbsinit`, `mbsnrtowcs`, `mbsrtowcs`, `mbstowcs`,
1031-
`mbtowc`, `mktime`, `putwc`, `putwchar`, `scanf`, `snprintf`, `sprintf`,
1032-
`sscanf`, `stoi`, `stol`, `stoll`, `strcasecmp`, `strcasestr`, `strcoll`,
1033-
`strfmon`, `strftime`, `strncasecmp`, `strptime`, `strtod`, `strtof`,
1034-
`strtoimax`, `strtol`, `strtold`, `strtoll`, `strtoq`, `strtoul`,
1035-
`strtoull`, `strtoumax`, `strtouq`, `strxfrm`, `swprintf`, `tolower`,
1036-
`toupper`, `towctrans`, `towlower`, `towupper`, `ungetwc`, `vasprintf`,
1037-
`vdprintf`, `versionsort`, `vfprintf`, `vfscanf`, `vfwprintf`, `vprintf`,
1038-
`vscanf`, `vsnprintf`, `vsprintf`, `vsscanf`, `vswprintf`, `vwprintf`,
1039-
`wcrtomb`, `wcscasecmp`, `wcscoll`, `wcsftime`, `wcsncasecmp`, `wcsnrtombs`,
1040-
`wcsrtombs`, `wcstod`, `wcstof`, `wcstoimax`, `wcstol`, `wcstold`,
1041-
`wcstoll`, `wcstombs`, `wcstoul`, `wcstoull`, `wcstoumax`, `wcswidth`,
1042-
`wcsxfrm`, `wctob`, `wctomb`, `wctrans`, `wctype`, `wcwidth`, `wprintf`
1043-
10441016
- For `strprintf`, `LogInfo`, `LogDebug`, etc formatting characters don't need size specifiers.
10451017
10461018
- *Rationale*: Bitcoin Core uses tinyformat, which is type safe. Leave them out to avoid confusion.

test/lint/lint-locale-dependence.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2018-2022 The Bitcoin Core developers
2+
# Copyright (c) 2018-present The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
#
@@ -43,6 +43,7 @@
4343

4444
KNOWN_VIOLATIONS = [
4545
"src/dbwrapper.cpp:.*vsnprintf",
46+
"src/span.h:.*printf",
4647
"src/test/fuzz/locale.cpp:.*setlocale",
4748
"src/test/util_tests.cpp:.*strtoll",
4849
"src/wallet/bdb.cpp:.*DbEnv::strerror", # False positive
@@ -215,7 +216,7 @@
215216
def find_locale_dependent_function_uses():
216217
regexp_locale_dependent_functions = "|".join(LOCALE_DEPENDENT_FUNCTIONS)
217218
exclude_args = [":(exclude)" + excl for excl in REGEXP_EXTERNAL_DEPENDENCIES_EXCLUSIONS]
218-
git_grep_command = ["git", "grep", "-E", "[^a-zA-Z0-9_\\`'\"<>](" + regexp_locale_dependent_functions + ")(_r|_s)?[^a-zA-Z0-9_\\`'\"<>]", "--", "*.cpp", "*.h"] + exclude_args
219+
git_grep_command = ["git", "grep", "--extended-regexp", "[^a-zA-Z0-9_\\`'\"<>](" + regexp_locale_dependent_functions + ")(_r|_s)?\\(", "--", "*.cpp", "*.h"] + exclude_args
219220
git_grep_output = list()
220221

221222
try:
@@ -235,8 +236,8 @@ def main():
235236

236237
for locale_dependent_function in LOCALE_DEPENDENT_FUNCTIONS:
237238
matches = [line for line in git_grep_output
238-
if re.search("[^a-zA-Z0-9_\\`'\"<>]" + locale_dependent_function + "(_r|_s)?[^a-zA-Z0-9_\\`'\"<>]", line)
239-
and not re.search("\\.(c|cpp|h):\\s*(//|\\*|/\\*|\").*" + locale_dependent_function, line)
239+
if re.search("[^a-zA-Z0-9_\\`'\"<>]" + locale_dependent_function + "(_r|_s)?\\(", line)
240+
and not re.search("\\.(c|cpp|h):\\s*//.*" + locale_dependent_function, line)
240241
and not re.search(regexp_ignore_known_violations, line)]
241242
if matches:
242243
print(f"The locale dependent function {locale_dependent_function}(...) appears to be used:")

0 commit comments

Comments
 (0)