Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace remaining locale specific functions #2268

Closed
barton2526 opened this issue Aug 4, 2021 · 14 comments · Fixed by #2270
Closed

Replace remaining locale specific functions #2268

barton2526 opened this issue Aug 4, 2021 · 14 comments · Fixed by #2270
Assignees
Milestone

Comments

@barton2526
Copy link
Member

barton2526 commented Aug 4, 2021

The following locale specific functions remain after #2265 and #2266:
atoi(...) - 6 uses (#2270 addresses)
isxdigit(...) - 1 use (#2270 addresses)
printf(...) - 3 uses (#2269 addresses)
sprintf(...) - 2 uses (see #2264 - #2269 addresses)
stod(...) - 4 uses (#2270 addresses)
stof(...) - 1 use (#2270 addresses)
stoi(...) - 8 uses (#2270 addresses - 2 of these are in leveldb, which should be excluded)
stoul(...) - 2 uses (#2270 addresses)
stoull(...) - 1 use (#2270 addresses)
strftime(...) - 3 uses (an exception should be put in to this - see comments below)
strtod(...) - 2 uses (#2270 addresses)
strtold(...) - 1 use (#2270 addresses)
trim(...) - 6 uses (#2270 addresses)

Special case:
boost::to_lower is triggering the to_lower locale function check. Upstream replaced this boost call with a std implementation, but it requires a custom solution because the output from the boost call is not exactly the same. See bitcoin/bitcoin#13671 (#2270 addresses by replacing boost::to_lower(s) with s = ToLower(s).)

@barton2526
Copy link
Member Author

Note, if some of those functions cannot be replaced/updated, we should add them to the list of known violations to silence the linter.

@jamescowens
Copy link
Member

Where is the third sprintf use? I can only find 2 (and those are taken care of by #2269.

@barton2526
Copy link
Member Author

It is 2, my mistake. Edited.

@jamescowens
Copy link
Member

The real issue with boost::to_lower is that it is a "modify in place" type call, where the bitcoin replacements return a value.

@jamescowens
Copy link
Member

strftime is used in DateTimeStrFormat, which is overloaded. It either takes a format string const char* pszFormat, or a default one is used. This means the format is specifically controlled and the specific use in DateTimeStrFormat can be added as an exception to the linter. The other direct uses also provide an output format string that is specific, so they can be added as exceptions too.

@jamescowens
Copy link
Member

Note that we have in strencodings.h...

int atoi(const std::string& str)
{
return atoi(str.c_str());
}

This takes the c_str() of the str which is fed into the atoi function. I don't believe this is locale specific. @denravonska can correct me if I am wrong. Given that this is really an overload of the built in atoi, and I don't think we can specify the argument types to distinguish, I think we should rename our atoi to strtoi so that we don't have to make an exception. Comments? I will go ahead and do a commit for that.

@jamescowens
Copy link
Member

Looks like I am wrong about that. the base atoi uses LC_NUMERIC of the current locale.

@jamescowens
Copy link
Member

Maybe I should replace all uses of atoi with ParseInt32, which is the wrapped version of strtol. What a pain.

@jamescowens jamescowens self-assigned this Aug 7, 2021
@jamescowens
Copy link
Member

I implemented ParseInt and ParseUInt, modelled after the Parse(U)Int32 functions and replaced atoi with that. I can also go back and simplify a few other places I already did with these new functions.

@jamescowens
Copy link
Member

jamescowens commented Aug 8, 2021

Ok. #2270 addresses everything remaining.

@jamescowens
Copy link
Member

^---- failure generated from test/lint/lint-include-guards.sh
Duplicate include(s) in src/qt/bitcoin.cpp:
#include

^---- failure generated from test/lint/lint-includes.sh
The locale dependent function strftime(...) appears to be used:
src/gridcoin/backup.cpp: strftime(boTime, sizeof(boTime), "%Y-%m-%dT%H-%M-%S", blTime);
src/logging.h: strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
src/rpc/rawtransaction.cpp: strftime(boTime, sizeof(boTime), "%Y-%m-%dT%H-%M-%S", blTime);

Unnecessary locale dependence can cause bugs that are very
tricky to isolate and fix. Please avoid using locale dependent
functions if possible.

Advice not applicable in this specific case? Add an exception
by updating the ignore list in test/lint/lint-locale-dependence.sh
^---- failure generated from test/lint/lint-locale-dependence.sh
Python's open(...) seems to be used to open text files without explicitly
specifying encoding="utf8":

share/qt/extract_strings_qt.py:f = open(OUT_CPP, 'w')
^---- failure generated from test/lint/lint-python-utf8-encoding.sh
usage: mypy [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Missing target module, package, files, or command.
^---- failure generated from test/lint/lint-python.sh

#2270 clears all of the locale entries except the strftime, for which we are going to put in exceptions in #2271.

@barton2526
Copy link
Member Author

Does this mean the other lines in the Violation list can be cleared?
For reference:

KNOWN_VIOLATIONS=(
"src/bitcoin-tx.cpp.*stoul"
"src/bitcoin-tx.cpp.*trim_right"
"src/dbwrapper.cpp.*stoul"
"src/dbwrapper.cpp:.*vsnprintf"
"src/httprpc.cpp.*trim"
"src/init.cpp:.*atoi"
"src/qt/rpcconsole.cpp:.*atoi"
"src/rest.cpp:.*strtol"
"src/test/dbwrapper_tests.cpp:.*snprintf"
"src/test/fuzz/locale.cpp"
"src/test/fuzz/parse_numbers.cpp:.*atoi"
"src/torcontrol.cpp:.*atoi"
"src/torcontrol.cpp:.*strtol"
"src/util/strencodings.cpp:.*atoi"
"src/util/strencodings.cpp:.*strtol"
"src/util/strencodings.cpp:.*strtoul"
"src/util/strencodings.h:.*atoi"
"src/util/system.cpp:.*atoi"

@jamescowens
Copy link
Member

I think all can be cleared except
"src/util/strencodings.cpp:.*atoi"
"src/util/strencodings.cpp:.*strtol"
"src/util/strencodings.cpp:.*strtoul"
"src/util/strencodings.h:.*atoi"

@barton2526
Copy link
Member Author

#2271 is ready now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants