Commit a8cb23f5 authored by Steven Murdoch's avatar Steven Murdoch Committed by Karsten Loesing
Browse files

Fix compilation on MacOS X

- Properly cast parameters to printf
- Replace GNU-specific strndup with tor_strndup
parent 9e94e666
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -249,7 +249,7 @@ do_http_get(int s, const char *path, const char *hostname, size_t *read_bytes, s

static int
print_time(struct timeval t) {
  return printf("%ld %ld ", t.tv_sec, t.tv_usec);
  return printf("%ld %ld ", (long int)t.tv_sec, (long int)t.tv_usec);
}

// Timestamps of important events
+22 −1
Original line number Diff line number Diff line
@@ -73,6 +73,27 @@ read_all(int fd, char *buf, size_t count, int isSocket)
  return numread;
}

/** Allocate and return a new string containing the first <b>n</b>
 * characters of <b>s</b>.  If <b>s</b> is longer than <b>n</b>
 * characters, only the first <b>n</b> are copied.  The result is
 * always NUL-terminated.
 */
char *
tor_strndup(const char *s, size_t n)
{
  char *dup;
  dup = malloc(n+1);
  /* Performance note: Ordinarily we prefer strlcpy to strncpy.  But
   * this function gets called a whole lot, and platform strncpy is
   * much faster than strlcpy when strlen(s) is much longer than n.
   */
  if (!dup)
      return dup;
  strncpy(dup, s, n);
  dup[n]='\0';
  return dup;
}

/**
 * Read a 16-bit value beginning at <b>cp</b>.  Equivalent to
 * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
@@ -207,7 +228,7 @@ parse_addr_port(int severity, const char *addrport, char **address,

  colon = strchr(addrport, ':');
  if (colon) {
    _address = strndup(addrport, colon-addrport);
    _address = tor_strndup(addrport, colon-addrport);
    _port = (int) parse_long(colon+1,10,1,65535,NULL,NULL);
    if (!_port) {
      fprintf(stderr, "Port %s out of range\n", colon+1);