Skip to content

Commit fa601ab

Browse files
author
MarcoFalke
committed
util: Catch translation string errors at compile time
1 parent 9adebe1 commit fa601ab

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)