public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-3626] libstdc++: Use std::string::__resize_and_overwrite in std::filesystem Date: Fri, 1 Sep 2023 15:02:24 +0000 (GMT) [thread overview] Message-ID: <20230901150224.9FECA3857703@sourceware.org> (raw) https://gcc.gnu.org/g:283994cba687e0746f9a1018bae2c41291e362ff commit r14-3626-g283994cba687e0746f9a1018bae2c41291e362ff Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Aug 17 18:50:36 2023 +0100 libstdc++: Use std::string::__resize_and_overwrite in std::filesystem There are a few places in the std::filesystem code that use a string as a buffer for OS APIs to write to. We can use the new extension __resize_and_overwrite to avoid redundant initialization of those buffers. libstdc++-v3/ChangeLog: * src/c++17/fs_ops.cc (fs::absolute) [FILESYSTEM_IS_WINDOWS]: Use __resize_and_overwrite to fill buffer. (fs::read_symlink) [HAVE_READLINK]: Likewise. * src/filesystem/ops-common.h (get_temp_directory_from_env) [FILESYSTEM_IS_WINDOWS]: Likewise. Diff: --- libstdc++-v3/src/c++17/fs_ops.cc | 45 ++++++++++++++++---------------- libstdc++-v3/src/filesystem/ops-common.h | 7 ++--- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc index c94d260632f6..6cdeac17c33c 100644 --- a/libstdc++-v3/src/c++17/fs_ops.cc +++ b/libstdc++-v3/src/c++17/fs_ops.cc @@ -112,18 +112,17 @@ fs::absolute(const path& p, error_code& ec) wstring buf; do { - buf.resize(len); - len = GetFullPathNameW(s.data(), len, buf.data(), nullptr); + buf.__resize_and_overwrite(len, [&s, &len](wchar_t* p, unsigned n) { + len = GetFullPathNameW(s.data(), n, p, nullptr); + return len > n ? 0 : len; + }); } while (len > buf.size()); if (len == 0) ec = __last_system_error(); else - { - buf.resize(len); - ret = std::move(buf); - } + ret = std::move(buf); #else ret = current_path(ec); ret /= p; @@ -1187,31 +1186,33 @@ fs::path fs::read_symlink(const path& p, error_code& ec) return result; } - std::string buf(st.st_size ? st.st_size + 1 : 128, '\0'); + std::string buf; + size_t bufsz = st.st_size ? st.st_size + 1 : 128; do { - ssize_t len = ::readlink(p.c_str(), buf.data(), buf.size()); - if (len == -1) + ssize_t len; + buf.__resize_and_overwrite(bufsz, [&p, &len](char* ptr, size_t n) { + len = ::readlink(p.c_str(), ptr, n); + return size_t(len) < n ? len : 0; + }); + if (buf.size()) + { + result.assign(std::move(buf)); + ec.clear(); + break; + } + else if (len == -1) { ec.assign(errno, std::generic_category()); return result; } - else if (len == (ssize_t)buf.size()) + else if (bufsz > 4096) { - if (buf.size() > 4096) - { - ec.assign(ENAMETOOLONG, std::generic_category()); - return result; - } - buf.resize(buf.size() * 2); + ec.assign(ENAMETOOLONG, std::generic_category()); + return result; } else - { - buf.resize(len); - result.assign(buf); - ec.clear(); - break; - } + bufsz *= 2; } while (true); #else diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h index 2e4331bb6829..79dcb756453f 100644 --- a/libstdc++-v3/src/filesystem/ops-common.h +++ b/libstdc++-v3/src/filesystem/ops-common.h @@ -700,8 +700,10 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM std::wstring buf; do { - buf.resize(len); - len = GetTempPathW(buf.size(), buf.data()); + buf.__resize_and_overwrite(len, [&len](wchar_t* p, unsigned n) { + len = GetTempPathW(n, p); + return len > n ? 0 : len; + }); } while (len > buf.size()); @@ -710,7 +712,6 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM else ec.clear(); - buf.resize(len); return buf; } #else
reply other threads:[~2023-09-01 15:02 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230901150224.9FECA3857703@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).