Unverified Commit 02fc0a5e authored by Alexander Hansen Færøy's avatar Alexander Hansen Færøy 💬
Browse files

Remove fgets() compatbility function and related tests.

This patch removes the `tor_fgets()` wrapper around `fgets(3)` since it
is no longer needed. The function was created due to inconsistency
between the returned values of `fgets(3)` on different versions of Unix
when using `fgets(3)` on non-blocking file descriptors, but with the
recent changes in bug #21654 we switch from unbuffered to direct I/O on
non-blocking file descriptors in our utility module.

We continue to use `fgets(3)` directly in the geoip and dirserv module
since this usage is considered safe.

This patch also removes the test-case that was created to detect
differences in the implementation of `fgets(3)` as well as the changes
file since these changes was not included in any releases yet.

See: https://bugs.torproject.org/21654
parent 02ef0651
Loading
Loading
Loading
Loading

changes/bug20988

deleted100644 → 0
+0 −4
Original line number Diff line number Diff line
  o Minor bugfixes (portability):
    - Add Tor compatibility function for fgets(3) due to inconsistency of
      returned values in different supported C libraries. This fixes unit test
      failures reported on FreeBSD. Fixes bug 20988.
+0 −39
Original line number Diff line number Diff line
@@ -3476,45 +3476,6 @@ tor_getpass(const char *prompt, char *output, size_t buflen)
#endif
}

/** Read at most one less than the number of characters specified by
 * <b>size</b> from the given <b>stream</b> and store it in <b>str</b>.
 *
 * Reading stops when a newline character is found or at EOF or error. If any
 * characters are read and there's no error, a trailing NUL byte is appended to
 * the end of <b>str</b>.
 *
 * Upon successful completion, this function returns a pointer to the string
 * <b>str</b>. If EOF occurs before any characters are read the function will
 * return NULL and the content of <b>str</b> is unchanged. Upon error, the
 * function returns NULL and the caller must check for error using foef(3) and
 * ferror(3).
 */
char *
tor_fgets(char *str, int size, FILE *stream)
{
  char *ret;

  /* Reset errno before our call to fgets(3) to avoid a situation where the
   * caller is calling us again because we just returned NULL and errno ==
   * EAGAIN, but when they call us again we will always return NULL because the
   * error flag on the file handler remains set and errno is set to EAGAIN.
   */
  errno = 0;

  ret = fgets(str, size, stream);

  /* FreeBSD, OpenBSD, Linux (glibc), and Linux (musl) seem to disagree about
   * what to do in the given situation. We check if the stream has been flagged
   * with an error-bit and return NULL in that situation if errno is also set
   * to EAGAIN.
   */
  if (ferror(stream) && errno == EAGAIN) {
    return NULL;
  }

  return ret;
}

/** Return the amount of free disk space we have permission to use, in
 * bytes. Return -1 if the amount of free space can't be determined. */
int64_t
+0 −2
Original line number Diff line number Diff line
@@ -740,8 +740,6 @@ STATIC int tor_ersatz_socketpair(int family, int type, int protocol,

ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);

char *tor_fgets(char *str, int size, FILE *stream);

/* This needs some of the declarations above so we include it here. */
#include "compat_threads.h"

+2 −2
Original line number Diff line number Diff line
@@ -2701,7 +2701,7 @@ dirserv_read_measured_bandwidths(const char *from_file,
    return -1;
  }

  if (!tor_fgets(line, sizeof(line), fp)
  if (!fgets(line, sizeof(line), fp)
          || !strlen(line) || line[strlen(line)-1] != '\n') {
    log_warn(LD_DIRSERV, "Long or truncated time in bandwidth file: %s",
             escaped(line));
@@ -2731,7 +2731,7 @@ dirserv_read_measured_bandwidths(const char *from_file,

  while (!feof(fp)) {
    measured_bw_line_t parsed_line;
    if (tor_fgets(line, sizeof(line), fp) && strlen(line)) {
    if (fgets(line, sizeof(line), fp) && strlen(line)) {
      if (measured_bw_line_parse(&parsed_line, line) != -1) {
        /* Also cache the line for dirserv_get_bandwidth_for_router() */
        dirserv_cache_measured_bw(&parsed_line, file_time);
+1 −1
Original line number Diff line number Diff line
@@ -346,7 +346,7 @@ geoip_load_file(sa_family_t family, const char *filename)
             (family == AF_INET) ? "IPv4" : "IPv6", filename);
  while (!feof(f)) {
    char buf[512];
    if (tor_fgets(buf, (int)sizeof(buf), f) == NULL)
    if (fgets(buf, (int)sizeof(buf), f) == NULL)
      break;
    crypto_digest_add_bytes(geoip_digest_env, buf, strlen(buf));
    /* FFFF track full country name. */
Loading