diff options
author | Dominique Leuenberger <dimstar@opensuse.org> | 2020-09-10 14:22:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-10 14:22:38 +0200 |
commit | 836c10b60c65e947ff1e10eb02fbcc676d909ffa (patch) | |
tree | 473e61aaa7150811898c9ef84d7eb743dc05db51 | |
parent | 1e49b4f60ab2c88d5faa6186a7eea039a97b1001 (diff) | |
parent | a83dae404feac517695c23ff43ce1e116e2bfbe0 (diff) | |
download | libproxy-git-836c10b60c65e947ff1e10eb02fbcc676d909ffa.tar.gz |
Merge pull request #136 from mcatanzaro/mcatanzaro/#134
Rewrite url::recvline to be nonrecursive
-rw-r--r-- | libproxy/url.cpp | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/libproxy/url.cpp b/libproxy/url.cpp index ee776b2..68d69cd 100644 --- a/libproxy/url.cpp +++ b/libproxy/url.cpp @@ -388,16 +388,24 @@ string url::to_string() const { return m_orig; } -static inline string recvline(int fd) { - // Read a character. - // If we don't get a character, return empty string. - // If we are at the end of the line, return empty string. - char c = '\0'; - - if (recv(fd, &c, 1, 0) != 1 || c == '\n') - return ""; - - return string(1, c) + recvline(fd); +static string recvline(int fd) { + string line; + int ret; + + // Reserve arbitrary amount of space to avoid small memory reallocations. + line.reserve(128); + + do { + char c; + ret = recv(fd, &c, 1, 0); + if (ret == 1) { + if (c == '\n') + return line; + line += c; + } + } while (ret == 1 || (ret == -1 && errno == EINTR)); + + return line; } char* url::get_pac() { |