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:
Charles Kerr 2023-12-24 13:33:33 -06:00 committed by GitHub
parent 0ef34878de
commit 5e51fda92e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 6 additions and 86 deletions

View File

@ -3688,7 +3688,6 @@
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
"-DRAPIDJSON_HAS_STDSTRING=1",
"-DHAVE_FLOCK",
"-DHAVE_STRLCPY",
);
PRODUCT_NAME = transmission;
SYSTEM_HEADER_SEARCH_PATHS = (
@ -3941,7 +3940,6 @@
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
"-DRAPIDJSON_HAS_STDSTRING=1",
"-DHAVE_FLOCK",
"-DHAVE_STRLCPY",
);
PRODUCT_NAME = transmission;
SYSTEM_HEADER_SEARCH_PATHS = (
@ -4268,7 +4266,6 @@
"-DWIDE_INTEGER_DISABLE_IOSTREAM",
"-DRAPIDJSON_HAS_STDSTRING=1",
"-DHAVE_FLOCK",
"-DHAVE_STRLCPY",
);
PRODUCT_NAME = transmission;
SYSTEM_HEADER_SEARCH_PATHS = (

View File

@ -1644,7 +1644,8 @@ namespace tracker_view_helpers
view.lastScrapeTime = tier.lastScrapeTime;
view.lastScrapeSucceeded = tier.lastScrapeSucceeded;
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)
@ -1674,7 +1675,8 @@ namespace tracker_view_helpers
view.lastAnnounceSucceeded = tier.lastAnnounceSucceeded;
view.lastAnnounceTimedOut = tier.lastAnnounceTimedOut;
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)

View File

@ -601,7 +601,7 @@ bool bindUnixSocket(
#else
auto addr = sockaddr_un{};
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);

View File

@ -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)
{
if (denominator > 0)
@ -567,9 +547,7 @@ std::string tr_strratio(double ratio, char const* infinity)
if ((int)ratio == TR_RATIO_INF)
{
auto buf = std::array<char, 64>{};
tr_strlcpy(std::data(buf), infinity, std::size(buf));
return std::data(buf);
return infinity != nullptr ? infinity : "";
}
return tr_strpercent(ratio);

View File

@ -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
@param errnum the error number to describe */
[[nodiscard]] char const* tr_strerror(int errnum);

View File

@ -226,60 +226,6 @@ TEST_F(UtilsTest, truncd)
#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)
{
char const* test_key = "TR_TEST_ENV";