File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -640,6 +640,28 @@ Strings and formatting
640
640
641
641
- * Rationale* : Bitcoin Core uses tinyformat, which is type safe. Leave them out to avoid confusion.
642
642
643
+ - Use ` .c_str() ` sparingly. Its only valid use is to pass C++ strings to C functions that take NULL-terminated
644
+ strings.
645
+
646
+ - Do not use it when passing a sized array (so along with ` .size() ` ). Use ` .data() ` instead to get a pointer
647
+ to the raw data.
648
+
649
+ - * Rationale* : Although this is guaranteed to be safe starting with C++11, ` .data() ` communicates the intent better.
650
+
651
+ - Do not use it when passing strings to ` tfm::format ` , ` strprintf ` , ` LogPrint[f] ` .
652
+
653
+ - * Rationale* : This is redundant. Tinyformat handles strings.
654
+
655
+ - Do not use it to convert to ` QString ` . Use ` QString::fromStdString() ` .
656
+
657
+ - * Rationale* : Qt has build-in functionality for converting their string
658
+ type from/to C++. No need to roll your own.
659
+
660
+ - In cases where do you call ` .c_str() ` , you might want to additionally check that the string does not contain embedded '\0' characters, because
661
+ it will (necessarily) truncate the string. This might be used to hide parts of the string from logging or to circumvent
662
+ checks. If a use of strings is sensitive to this, take care to check the string for embedded NULL characters first
663
+ and reject it if there are any (see ` ParsePrechecks ` in ` strencodings.cpp ` for an example).
664
+
643
665
Shadowing
644
666
--------------
645
667
You can’t perform that action at this time.
0 commit comments