mirror of
https://github.com/transmission/transmission
synced 2025-01-03 05:25:52 +00:00
refactor: remove tr_strlcpy() (#6433)
* refactor: do not use tr_strlcpy() in tr_strratio() * refactor: do not use tr_strlcpy() in bindUnixSocket() * refactor: do not use tr_strlcpy() in trackerView() * chore: remove tr_strlcpy() unit tests * chore: remove tr_strlcpy() * chore: remove -DHAVE_STRLCPY from Xcode build * fixup! refactor: do not use tr_strlcpy() in trackerView() chore: fix copypaste bug
This commit is contained in:
parent
0ef34878de
commit
5e51fda92e
6 changed files with 6 additions and 86 deletions
|
@ -3688,7 +3688,6 @@
|
||||||
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
|
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
|
||||||
"-DRAPIDJSON_HAS_STDSTRING=1",
|
"-DRAPIDJSON_HAS_STDSTRING=1",
|
||||||
"-DHAVE_FLOCK",
|
"-DHAVE_FLOCK",
|
||||||
"-DHAVE_STRLCPY",
|
|
||||||
);
|
);
|
||||||
PRODUCT_NAME = transmission;
|
PRODUCT_NAME = transmission;
|
||||||
SYSTEM_HEADER_SEARCH_PATHS = (
|
SYSTEM_HEADER_SEARCH_PATHS = (
|
||||||
|
@ -3941,7 +3940,6 @@
|
||||||
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
|
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
|
||||||
"-DRAPIDJSON_HAS_STDSTRING=1",
|
"-DRAPIDJSON_HAS_STDSTRING=1",
|
||||||
"-DHAVE_FLOCK",
|
"-DHAVE_FLOCK",
|
||||||
"-DHAVE_STRLCPY",
|
|
||||||
);
|
);
|
||||||
PRODUCT_NAME = transmission;
|
PRODUCT_NAME = transmission;
|
||||||
SYSTEM_HEADER_SEARCH_PATHS = (
|
SYSTEM_HEADER_SEARCH_PATHS = (
|
||||||
|
@ -4268,7 +4266,6 @@
|
||||||
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
|
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
|
||||||
"-DRAPIDJSON_HAS_STDSTRING=1",
|
"-DRAPIDJSON_HAS_STDSTRING=1",
|
||||||
"-DHAVE_FLOCK",
|
"-DHAVE_FLOCK",
|
||||||
"-DHAVE_STRLCPY",
|
|
||||||
);
|
);
|
||||||
PRODUCT_NAME = transmission;
|
PRODUCT_NAME = transmission;
|
||||||
SYSTEM_HEADER_SEARCH_PATHS = (
|
SYSTEM_HEADER_SEARCH_PATHS = (
|
||||||
|
|
|
@ -1644,7 +1644,8 @@ namespace tracker_view_helpers
|
||||||
view.lastScrapeTime = tier.lastScrapeTime;
|
view.lastScrapeTime = tier.lastScrapeTime;
|
||||||
view.lastScrapeSucceeded = tier.lastScrapeSucceeded;
|
view.lastScrapeSucceeded = tier.lastScrapeSucceeded;
|
||||||
view.lastScrapeTimedOut = tier.lastScrapeTimedOut;
|
view.lastScrapeTimedOut = tier.lastScrapeTimedOut;
|
||||||
tr_strlcpy(view.lastScrapeResult, tier.last_scrape_str.c_str(), sizeof(view.lastScrapeResult));
|
auto& buf = view.lastScrapeResult;
|
||||||
|
*fmt::format_to_n(buf, sizeof(buf) - 1, "{:s}", tier.last_scrape_str).out = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tier.isScraping)
|
if (tier.isScraping)
|
||||||
|
@ -1674,7 +1675,8 @@ namespace tracker_view_helpers
|
||||||
view.lastAnnounceSucceeded = tier.lastAnnounceSucceeded;
|
view.lastAnnounceSucceeded = tier.lastAnnounceSucceeded;
|
||||||
view.lastAnnounceTimedOut = tier.lastAnnounceTimedOut;
|
view.lastAnnounceTimedOut = tier.lastAnnounceTimedOut;
|
||||||
view.lastAnnouncePeerCount = tier.lastAnnouncePeerCount;
|
view.lastAnnouncePeerCount = tier.lastAnnouncePeerCount;
|
||||||
tr_strlcpy(view.lastAnnounceResult, tier.last_announce_str.c_str(), sizeof(view.lastAnnounceResult));
|
auto& buf = view.lastAnnounceResult;
|
||||||
|
*fmt::format_to_n(buf, sizeof(buf) - 1, "{:s}", tier.last_announce_str).out = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tier.isAnnouncing)
|
if (tier.isAnnouncing)
|
||||||
|
|
|
@ -601,7 +601,7 @@ bool bindUnixSocket(
|
||||||
#else
|
#else
|
||||||
auto addr = sockaddr_un{};
|
auto addr = sockaddr_un{};
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sun_family = AF_UNIX;
|
||||||
tr_strlcpy(addr.sun_path, path + std::size(TrUnixSocketPrefix), sizeof(addr.sun_path));
|
*fmt::format_to_n(addr.sun_path, sizeof(addr.sun_path) - 1, "{:s}", path + std::size(TrUnixSocketPrefix)).out = '\0';
|
||||||
|
|
||||||
unlink(addr.sun_path);
|
unlink(addr.sun_path);
|
||||||
|
|
||||||
|
|
|
@ -269,26 +269,6 @@ uint64_t tr_time_msec()
|
||||||
|
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/*
|
|
||||||
* Copy src to string dst of size siz. At most siz-1 characters
|
|
||||||
* will be copied. Always NUL terminates (unless siz == 0).
|
|
||||||
* Returns strlen (src); if retval >= siz, truncation occurred.
|
|
||||||
*/
|
|
||||||
size_t tr_strlcpy(void* vdst, void const* vsrc, size_t siz)
|
|
||||||
{
|
|
||||||
auto* dst = static_cast<char*>(vdst);
|
|
||||||
auto const* const src = static_cast<char const*>(vsrc);
|
|
||||||
|
|
||||||
TR_ASSERT(dst != nullptr);
|
|
||||||
TR_ASSERT(src != nullptr);
|
|
||||||
|
|
||||||
auto const res = fmt::format_to_n(dst, siz - 1, "{:s}", src);
|
|
||||||
*res.out = '\0';
|
|
||||||
return res.size;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---
|
|
||||||
|
|
||||||
double tr_getRatio(uint64_t numerator, uint64_t denominator)
|
double tr_getRatio(uint64_t numerator, uint64_t denominator)
|
||||||
{
|
{
|
||||||
if (denominator > 0)
|
if (denominator > 0)
|
||||||
|
@ -567,9 +547,7 @@ std::string tr_strratio(double ratio, char const* infinity)
|
||||||
|
|
||||||
if ((int)ratio == TR_RATIO_INF)
|
if ((int)ratio == TR_RATIO_INF)
|
||||||
{
|
{
|
||||||
auto buf = std::array<char, 64>{};
|
return infinity != nullptr ? infinity : "";
|
||||||
tr_strlcpy(std::data(buf), infinity, std::size(buf));
|
|
||||||
return std::data(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tr_strpercent(ratio);
|
return tr_strpercent(ratio);
|
||||||
|
|
|
@ -100,9 +100,6 @@ int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**));
|
||||||
|
|
||||||
// ---
|
// ---
|
||||||
|
|
||||||
/** @brief Portability wrapper for `strlcpy()` that uses the system implementation if available */
|
|
||||||
size_t tr_strlcpy(void* dst, void const* src, size_t siz);
|
|
||||||
|
|
||||||
/** @brief Convenience wrapper around `strerorr()` guaranteed to not return nullptr
|
/** @brief Convenience wrapper around `strerorr()` guaranteed to not return nullptr
|
||||||
@param errnum the error number to describe */
|
@param errnum the error number to describe */
|
||||||
[[nodiscard]] char const* tr_strerror(int errnum);
|
[[nodiscard]] char const* tr_strerror(int errnum);
|
||||||
|
|
|
@ -226,60 +226,6 @@ TEST_F(UtilsTest, truncd)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(UtilsTest, trStrlcpy)
|
|
||||||
{
|
|
||||||
// destination will be initialized with this char
|
|
||||||
char const initial_char = '1';
|
|
||||||
std::array<char, 100> destination = { initial_char };
|
|
||||||
|
|
||||||
std::vector<std::string> const tests{
|
|
||||||
"a",
|
|
||||||
"",
|
|
||||||
"12345678901234567890",
|
|
||||||
"This, very useful string contains a total of 105 characters not counting null. Almost like an easter egg!"
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto const& test : tests)
|
|
||||||
{
|
|
||||||
auto c_string = test.c_str();
|
|
||||||
auto length = strlen(c_string);
|
|
||||||
|
|
||||||
destination.fill(initial_char);
|
|
||||||
|
|
||||||
auto response = tr_strlcpy(&destination, c_string, 98);
|
|
||||||
|
|
||||||
// Check response length
|
|
||||||
ASSERT_EQ(response, length);
|
|
||||||
|
|
||||||
// Check what was copied
|
|
||||||
for (unsigned i = 0U; i < 97U; ++i)
|
|
||||||
{
|
|
||||||
if (i <= length)
|
|
||||||
{
|
|
||||||
ASSERT_EQ(destination[i], c_string[i]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ASSERT_EQ(destination[i], initial_char);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// tr_strlcpy should only write this far if (length >= 98)
|
|
||||||
if (length >= 98)
|
|
||||||
{
|
|
||||||
ASSERT_EQ(destination[97], '\0');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ASSERT_EQ(destination[97], initial_char);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tr_strlcpy should not write this far
|
|
||||||
ASSERT_EQ(destination[98], initial_char);
|
|
||||||
ASSERT_EQ(destination[99], initial_char);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(UtilsTest, env)
|
TEST_F(UtilsTest, env)
|
||||||
{
|
{
|
||||||
char const* test_key = "TR_TEST_ENV";
|
char const* test_key = "TR_TEST_ENV";
|
||||||
|
|
Loading…
Reference in a new issue