From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- mozglue/interposers/getline_interposer.cpp | 110 +++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 mozglue/interposers/getline_interposer.cpp (limited to 'mozglue/interposers/getline_interposer.cpp') diff --git a/mozglue/interposers/getline_interposer.cpp b/mozglue/interposers/getline_interposer.cpp new file mode 100644 index 0000000000..902b52b718 --- /dev/null +++ b/mozglue/interposers/getline_interposer.cpp @@ -0,0 +1,110 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * Interposing getline because of + * https://bugzilla.mozilla.org/show_bug.cgi?id=914190 + */ + +#ifdef __ANDROID__ + +# include +# include +# include + +# include + +namespace { + +// RAII on file locking. +class FileLocker { + FILE* stream; + + public: + explicit FileLocker(FILE* stream) : stream(stream) { flockfile(stream); } + ~FileLocker() { funlockfile(stream); } +}; + +ssize_t internal_getdelim(char** __restrict lineptr, size_t* __restrict n, + int delim, FILE* __restrict stream) { + constexpr size_t n_default = 64; + constexpr size_t n_max = + std::numeric_limits::max() < std::numeric_limits::max() + ? (size_t)std::numeric_limits::max() + 1 + : std::numeric_limits::max(); + constexpr size_t n_limit = 2 * ((n_max - 1) / 3); + + if (!lineptr || !n || !stream) { + errno = EINVAL; + return -1; + } + + // Lock the file so that we can us unlocked getc in the inner loop. + FileLocker fl(stream); + + if (!*lineptr || *n == 0) { + *n = n_default; + if (auto* new_lineptr = reinterpret_cast(realloc(*lineptr, *n))) { + *lineptr = new_lineptr; + } else { + errno = ENOMEM; + return -1; + } + } + + ssize_t result; + size_t cur_len = 0; + + while (true) { + // Retrieve an extra char. + int i = getc_unlocked(stream); + if (i == EOF) { + result = -1; + break; + } + + // Eventually grow the buffer. + if (cur_len + 1 >= *n) { + size_t needed = *n >= n_limit ? n_max : 3 * *n / 2 + 1; + + if (cur_len + 1 >= needed) { + errno = EOVERFLOW; + return -1; + } + + if (auto* new_lineptr = (char*)realloc(*lineptr, needed)) { + *lineptr = new_lineptr; + } else { + errno = ENOMEM; + return -1; + } + *n = needed; + } + + (*lineptr)[cur_len] = i; + cur_len++; + + if (i == delim) break; + } + (*lineptr)[cur_len] = '\0'; + return cur_len ? cur_len : result; +} + +} // namespace + +extern "C" { + +MFBT_API ssize_t getline(char** __restrict lineptr, size_t* __restrict n, + FILE* __restrict stream) { + return internal_getdelim(lineptr, n, '\n', stream); +} + +MFBT_API ssize_t getdelim(char** __restrict lineptr, size_t* __restrict n, + int delim, FILE* __restrict stream) { + return internal_getdelim(lineptr, n, delim, stream); +} + +} // extern "C" + +#endif -- cgit v1.2.3