Skip to content

Commit a231cfe

Browse files
committed
Merge bitcoin/bitcoin#30383: util: Catch translation string errors at compile time
fa601ab util: Catch translation string errors at compile time (MarcoFalke) Pull request description: The translation helper function `_()` has many problems. For example, the following compiles: ```cpp auto ptr{"wrong"}; _(ptr); _(nullptr); _(0); _(NULL); ``` However, it is wrong, because none of the arguments passed to the function can be picked up by the translation tooling for transifex. Fix all issues by enforcing only real string literals can be passed to the function. ACKs for top commit: ryanofsky: Code review ACK fa601ab hebasto: ACK fa601ab. Tree-SHA512: 33aed02d7e8fc9bfb8f90746f5c8072a8c0910fa900ec3516af2e732780b0fee8b07b6596c0fc210b018c0869111d6c34bf8d083de0e88ecdb4dee88e809186d
2 parents e516539 + fa601ab commit a231cfe

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/test/fuzz/string.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2022 The Bitcoin Core developers
1+
// Copyright (c) 2020-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -101,7 +101,6 @@ FUZZ_TARGET(string)
101101
(void)TrimString(random_string_1, random_string_2);
102102
(void)UrlDecode(random_string_1);
103103
(void)ContainsNoNUL(random_string_1);
104-
(void)_(random_string_1.c_str());
105104
try {
106105
throw scriptnum_error{random_string_1};
107106
} catch (const std::runtime_error&) {

src/util/translation.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019-2022 The Bitcoin Core developers
1+
// Copyright (c) 2019-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -67,13 +67,19 @@ bilingual_str format(const bilingual_str& fmt, const Args&... args)
6767
/** Translate a message to the native language of the user. */
6868
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
6969

70+
struct ConstevalStringLiteral {
71+
const char* const lit;
72+
consteval ConstevalStringLiteral(const char* str) : lit{str} {}
73+
consteval ConstevalStringLiteral(std::nullptr_t) = delete;
74+
};
75+
7076
/**
7177
* Translation function.
7278
* If no translation function is set, simply return the input.
7379
*/
74-
inline bilingual_str _(const char* psz)
80+
inline bilingual_str _(ConstevalStringLiteral str)
7581
{
76-
return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
82+
return bilingual_str{str.lit, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(str.lit) : str.lit};
7783
}
7884

7985
#endif // BITCOIN_UTIL_TRANSLATION_H

0 commit comments

Comments
 (0)