Ticket #22521: tor_offsetof_0001.patch

File tor_offsetof_0001.patch, 16.3 KB (added by neel, 2 years ago)

Patch to change STRUCT_OFFSET to offsetof()

  • src/common/container.c

    From 40f83c9404f8b6136d35ca564198a7086a2f8d7a Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Mon, 31 Jul 2017 19:30:30 -0400
    Subject: [PATCH] Switch to offsetof()
    
    ---
     src/common/container.c       |  4 ++--
     src/common/crypto.c          |  2 +-
     src/common/memarea.c         |  3 ++-
     src/common/util.h            | 12 ++----------
     src/or/buffers.c             |  2 +-
     src/or/circuitmux_ewma.c     |  6 +++---
     src/or/config.c              |  4 ++--
     src/or/connection_or.c       |  4 ++--
     src/or/dircollate.c          |  2 +-
     src/or/dns.c                 |  6 +++---
     src/or/ext_orport.c          |  2 +-
     src/or/policies.c            |  2 +-
     src/or/scheduler.c           | 16 ++++++++--------
     src/or/shared_random_state.c |  6 +++---
     src/or/statefile.c           |  6 +++---
     src/test/test_containers.c   |  2 +-
     16 files changed, 36 insertions(+), 43 deletions(-)
    
    diff --git a/src/common/container.c b/src/common/container.c
    index 689e7e55e..8645cb482 100644
    a b smartlist_sort_pointers(smartlist_t *sl) 
    843843 *   }
    844844 *
    845845 *   void timer_heap_insert(smartlist_t *heap, timer_t *timer) {
    846  *      smartlist_pqueue_add(heap, compare, STRUCT_OFFSET(timer_t, heap_index),
     846 *      smartlist_pqueue_add(heap, compare, offsetof(timer_t, heap_index),
    847847 *         timer);
    848848 *   }
    849849 *
    850850 *   void timer_heap_pop(smartlist_t *heap) {
    851851 *      return smartlist_pqueue_pop(heap, compare,
    852  *         STRUCT_OFFSET(timer_t, heap_index));
     852 *         offsetof(timer_t, heap_index));
    853853 *   }
    854854 */
    855855
  • src/common/crypto.c

    diff --git a/src/common/crypto.c b/src/common/crypto.c
    index c258f239a..4d6a70bc4 100644
    a b crypto_digest_alloc_bytes(digest_algorithm_t alg) 
    18841884  /* Helper: returns the number of bytes in the 'f' field of 'st' */
    18851885#define STRUCT_FIELD_SIZE(st, f) (sizeof( ((st*)0)->f ))
    18861886  /* Gives the length of crypto_digest_t through the end of the field 'd' */
    1887 #define END_OF_FIELD(f) (STRUCT_OFFSET(crypto_digest_t, f) + \
     1887#define END_OF_FIELD(f) (offsetof(crypto_digest_t, f) + \
    18881888                         STRUCT_FIELD_SIZE(crypto_digest_t, f))
    18891889  switch (alg) {
    18901890    case DIGEST_SHA1:
  • src/common/memarea.c

    diff --git a/src/common/memarea.c b/src/common/memarea.c
    index 659d1edf5..4e2a5e5fc 100644
    a b  
    77 */
    88
    99#include "orconfig.h"
     10#include <stddef.h>
    1011#include <stdlib.h>
    1112#include "memarea.h"
    1213#include "util.h"
    typedef struct memarea_chunk_t { 
    101102
    102103/** How many bytes are needed for overhead before we get to the memory part
    103104 * of a chunk? */
    104 #define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, U_MEM)
     105#define CHUNK_HEADER_SIZE offsetof(memarea_chunk_t, U_MEM)
    105106
    106107/** What's the smallest that we'll allocate a chunk? */
    107108#define CHUNK_SIZE 4096
  • src/common/util.h

    diff --git a/src/common/util.h b/src/common/util.h
    index d56abcee2..df581d240 100644
    a b extern int dmalloc_free(const char *file, const int line, void *pnt, 
    109109
    110110void tor_log_mallinfo(int severity);
    111111
    112 /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
    113 #if defined(__GNUC__) && __GNUC__ > 3
    114 #define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
    115 #else
    116  #define STRUCT_OFFSET(tp, member) \
    117    ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
    118 #endif
    119 
    120112/** Macro: yield a pointer to the field at position <b>off</b> within the
    121113 * structure <b>st</b>.  Example:
    122114 * <pre>
    123115 *   struct a { int foo; int bar; } x;
    124  *   off_t bar_offset = STRUCT_OFFSET(struct a, bar);
     116 *   off_t bar_offset = offsetof(struct a, bar);
    125117 *   int *bar_p = STRUCT_VAR_P(&x, bar_offset);
    126118 *   *bar_p = 3;
    127119 * </pre>
    void tor_log_mallinfo(int severity); 
    138130 * </pre>
    139131 */
    140132#define SUBTYPE_P(p, subtype, basemember) \
    141   ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
     133  ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
    142134
    143135/* Logic */
    144136/** Macro: true if two values have the same boolean value. */
  • src/or/buffers.c

    diff --git a/src/or/buffers.c b/src/or/buffers.c
    index d5ecfb848..bd84103c3 100644
    a b static int parse_socks_client(const uint8_t *data, size_t datalen, 
    8080
    8181/* Chunk manipulation functions */
    8282
    83 #define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])
     83#define CHUNK_HEADER_LEN offsetof(chunk_t, mem[0])
    8484
    8585/* We leave this many NUL bytes at the end of the buffer. */
    8686#ifdef DISABLE_MEMORY_SENTINELS
  • src/or/circuitmux_ewma.c

    diff --git a/src/or/circuitmux_ewma.c b/src/or/circuitmux_ewma.c
    index c2440b13f..fde2d22a8 100644
    a b add_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma) 
    731731
    732732  smartlist_pqueue_add(pol->active_circuit_pqueue,
    733733                       compare_cell_ewma_counts,
    734                        STRUCT_OFFSET(cell_ewma_t, heap_index),
     734                       offsetof(cell_ewma_t, heap_index),
    735735                       ewma);
    736736}
    737737
    remove_cell_ewma(ewma_policy_data_t *pol, cell_ewma_t *ewma) 
    746746
    747747  smartlist_pqueue_remove(pol->active_circuit_pqueue,
    748748                          compare_cell_ewma_counts,
    749                           STRUCT_OFFSET(cell_ewma_t, heap_index),
     749                          offsetof(cell_ewma_t, heap_index),
    750750                          ewma);
    751751}
    752752
    pop_first_cell_ewma(ewma_policy_data_t *pol) 
    760760
    761761  return smartlist_pqueue_pop(pol->active_circuit_pqueue,
    762762                              compare_cell_ewma_counts,
    763                               STRUCT_OFFSET(cell_ewma_t, heap_index));
     763                              offsetof(cell_ewma_t, heap_index));
    764764}
    765765
  • src/or/config.c

    diff --git a/src/or/config.c b/src/or/config.c
    index 58d22d9f1..53fc2795c 100644
    a b static config_abbrev_t option_abbrevs_[] = { 
    177177 * or_options_t.<b>member</b>"
    178178 */
    179179#define VAR(name,conftype,member,initvalue)                             \
    180   { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_options_t, member), \
     180  { name, CONFIG_TYPE_ ## conftype, offsetof(or_options_t, member),    \
    181181      initvalue }
    182182/** As VAR, but the option name and member name are the same. */
    183183#define V(member,conftype,initvalue)                                    \
    static uint64_t compute_real_max_mem_in_queues(const uint64_t val, 
    733733STATIC config_format_t options_format = {
    734734  sizeof(or_options_t),
    735735  OR_OPTIONS_MAGIC,
    736   STRUCT_OFFSET(or_options_t, magic_),
     736  offsetof(or_options_t, magic_),
    737737  option_abbrevs_,
    738738  option_deprecation_notes_,
    739739  option_vars_,
  • src/or/connection_or.c

    diff --git a/src/or/connection_or.c b/src/or/connection_or.c
    index 051bf9a17..7c929e527 100644
    a b var_cell_pack_header(const var_cell_t *cell, char *hdr_out, int wide_circ_ids) 
    472472var_cell_t *
    473473var_cell_new(uint16_t payload_len)
    474474{
    475   size_t size = STRUCT_OFFSET(var_cell_t, payload) + payload_len;
     475  size_t size = offsetof(var_cell_t, payload) + payload_len;
    476476  var_cell_t *cell = tor_malloc_zero(size);
    477477  cell->payload_len = payload_len;
    478478  cell->command = 0;
    var_cell_copy(const var_cell_t *src) 
    491491  size_t size = 0;
    492492
    493493  if (src != NULL) {
    494     size = STRUCT_OFFSET(var_cell_t, payload) + src->payload_len;
     494    size = offsetof(var_cell_t, payload) + src->payload_len;
    495495    copy = tor_malloc_zero(size);
    496496    copy->payload_len = src->payload_len;
    497497    copy->command = src->command;
  • src/or/dircollate.c

    diff --git a/src/or/dircollate.c b/src/or/dircollate.c
    index 172364c5f..d34ebe8af 100644
    a b ddmap_entry_free(ddmap_entry_t *e) 
    5353static ddmap_entry_t *
    5454ddmap_entry_new(int n_votes)
    5555{
    56   return tor_malloc_zero(STRUCT_OFFSET(ddmap_entry_t, vrs_lst) +
     56  return tor_malloc_zero(offsetof(ddmap_entry_t, vrs_lst) +
    5757                         sizeof(vote_routerstatus_t *) * n_votes);
    5858}
    5959
  • src/or/dns.c

    diff --git a/src/or/dns.c b/src/or/dns.c
    index cc062e30e..2d642773f 100644
    a b set_expiry(cached_resolve_t *resolve, time_t expires) 
    378378  resolve->expire = expires;
    379379  smartlist_pqueue_add(cached_resolve_pqueue,
    380380                       compare_cached_resolves_by_expiry_,
    381                        STRUCT_OFFSET(cached_resolve_t, minheap_idx),
     381                       offsetof(cached_resolve_t, minheap_idx),
    382382                       resolve);
    383383}
    384384
    purge_expired_resolves(time_t now) 
    425425      break;
    426426    smartlist_pqueue_pop(cached_resolve_pqueue,
    427427                         compare_cached_resolves_by_expiry_,
    428                          STRUCT_OFFSET(cached_resolve_t, minheap_idx));
     428                         offsetof(cached_resolve_t, minheap_idx));
    429429
    430430    if (resolve->state == CACHE_STATE_PENDING) {
    431431      log_debug(LD_EXIT,
    assert_cache_ok_(void) 
    20832083
    20842084  smartlist_pqueue_assert_ok(cached_resolve_pqueue,
    20852085                             compare_cached_resolves_by_expiry_,
    2086                              STRUCT_OFFSET(cached_resolve_t, minheap_idx));
     2086                             offsetof(cached_resolve_t, minheap_idx));
    20872087
    20882088  SMARTLIST_FOREACH(cached_resolve_pqueue, cached_resolve_t *, res,
    20892089    {
  • src/or/ext_orport.c

    diff --git a/src/or/ext_orport.c b/src/or/ext_orport.c
    index b60d2e55c..01dc06ce1 100644
    a b  
    3131ext_or_cmd_t *
    3232ext_or_cmd_new(uint16_t len)
    3333{
    34   size_t size = STRUCT_OFFSET(ext_or_cmd_t, body) + len;
     34  size_t size = offsetof(ext_or_cmd_t, body) + len;
    3535  ext_or_cmd_t *cmd = tor_malloc(size);
    3636  cmd->len = len;
    3737  return cmd;
  • src/or/policies.c

    diff --git a/src/or/policies.c b/src/or/policies.c
    index 3d49a6110..4c24bfbc3 100644
    a b parse_short_policy(const char *summary) 
    27312731  }
    27322732
    27332733  {
    2734     size_t size = STRUCT_OFFSET(short_policy_t, entries) +
     2734    size_t size = offsetof(short_policy_t, entries) +
    27352735      sizeof(short_policy_entry_t)*(n_entries);
    27362736    result = tor_malloc_zero(size);
    27372737
  • src/or/scheduler.c

    diff --git a/src/or/scheduler.c b/src/or/scheduler.c
    index fac545fba..0d31c7d58 100644
    a b scheduler_channel_doesnt_want_writes,(channel_t *chan)) 
    282282     */
    283283    smartlist_pqueue_remove(channels_pending,
    284284                            scheduler_compare_channels,
    285                             STRUCT_OFFSET(channel_t, sched_heap_idx),
     285                            offsetof(channel_t, sched_heap_idx),
    286286                            chan);
    287287    chan->scheduler_state = SCHED_CHAN_WAITING_TO_WRITE;
    288288    log_debug(LD_SCHED,
    scheduler_channel_has_waiting_cells,(channel_t *chan)) 
    324324    chan->scheduler_state = SCHED_CHAN_PENDING;
    325325    smartlist_pqueue_add(channels_pending,
    326326                         scheduler_compare_channels,
    327                          STRUCT_OFFSET(channel_t, sched_heap_idx),
     327                         offsetof(channel_t, sched_heap_idx),
    328328                         chan);
    329329    log_debug(LD_SCHED,
    330330              "Channel " U64_FORMAT " at %p went from waiting_for_cells "
    scheduler_release_channel,(channel_t *chan)) 
    400400  if (chan->scheduler_state == SCHED_CHAN_PENDING) {
    401401    smartlist_pqueue_remove(channels_pending,
    402402                            scheduler_compare_channels,
    403                             STRUCT_OFFSET(channel_t, sched_heap_idx),
     403                            offsetof(channel_t, sched_heap_idx),
    404404                            chan);
    405405  }
    406406
    scheduler_run, (void)) 
    430430      /* Pop off a channel */
    431431      chan = smartlist_pqueue_pop(channels_pending,
    432432                                  scheduler_compare_channels,
    433                                   STRUCT_OFFSET(channel_t, sched_heap_idx));
     433                                  offsetof(channel_t, sched_heap_idx));
    434434      tor_assert(chan);
    435435
    436436      /* Figure out how many cells we can write */
    scheduler_run, (void)) 
    531531        readd_chan->scheduler_state = SCHED_CHAN_PENDING;
    532532        smartlist_pqueue_add(channels_pending,
    533533                             scheduler_compare_channels,
    534                              STRUCT_OFFSET(channel_t, sched_heap_idx),
     534                             offsetof(channel_t, sched_heap_idx),
    535535                             readd_chan);
    536536      } SMARTLIST_FOREACH_END(readd_chan);
    537537      smartlist_free(to_readd);
    scheduler_channel_wants_writes(channel_t *chan) 
    581581     */
    582582    smartlist_pqueue_add(channels_pending,
    583583                         scheduler_compare_channels,
    584                          STRUCT_OFFSET(channel_t, sched_heap_idx),
     584                         offsetof(channel_t, sched_heap_idx),
    585585                         chan);
    586586    chan->scheduler_state = SCHED_CHAN_PENDING;
    587587    log_debug(LD_SCHED,
    scheduler_touch_channel(channel_t *chan) 
    624624    /* Remove and re-add it */
    625625    smartlist_pqueue_remove(channels_pending,
    626626                            scheduler_compare_channels,
    627                             STRUCT_OFFSET(channel_t, sched_heap_idx),
     627                            offsetof(channel_t, sched_heap_idx),
    628628                            chan);
    629629    smartlist_pqueue_add(channels_pending,
    630630                         scheduler_compare_channels,
    631                          STRUCT_OFFSET(channel_t, sched_heap_idx),
     631                         offsetof(channel_t, sched_heap_idx),
    632632                         chan);
    633633  }
    634634  /* else no-op, since it isn't in the queue */
  • src/or/shared_random_state.c

    diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c
    index 89d2e8d7f..ef026e7f2 100644
    a b static const char dstate_cur_srv_key[] = "SharedRandCurrentValue"; 
    4242
    4343/* These next two are duplicates or near-duplicates from config.c */
    4444#define VAR(name, conftype, member, initvalue)                              \
    45   { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(sr_disk_state_t, member), \
     45  { name, CONFIG_TYPE_ ## conftype, offsetof(sr_disk_state_t, member),      \
    4646    initvalue }
    4747/* As VAR, but the option name and member name are the same. */
    4848#define V(member, conftype, initvalue) \
    static config_var_t state_vars[] = { 
    7777 * lets us preserve options from versions of Tor newer than us. */
    7878static config_var_t state_extra_var = {
    7979  "__extra", CONFIG_TYPE_LINELIST,
    80   STRUCT_OFFSET(sr_disk_state_t, ExtraLines), NULL
     80  offsetof(sr_disk_state_t, ExtraLines), NULL
    8181};
    8282
    8383/* Configuration format of sr_disk_state_t. */
    8484static const config_format_t state_format = {
    8585  sizeof(sr_disk_state_t),
    8686  SR_DISK_STATE_MAGIC,
    87   STRUCT_OFFSET(sr_disk_state_t, magic_),
     87  offsetof(sr_disk_state_t, magic_),
    8888  NULL,
    8989  NULL,
    9090  state_vars,
  • src/or/statefile.c

    diff --git a/src/or/statefile.c b/src/or/statefile.c
    index d0606b301..aaed10409 100644
    a b static config_abbrev_t state_abbrevs_[] = { 
    5555
    5656/*XXXX these next two are duplicates or near-duplicates from config.c */
    5757#define VAR(name,conftype,member,initvalue)                             \
    58   { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_state_t, member),  \
     58  { name, CONFIG_TYPE_ ## conftype, offsetof(or_state_t, member),       \
    5959      initvalue }
    6060/** As VAR, but the option name and member name are the same. */
    6161#define V(member,conftype,initvalue)                                    \
    static int or_state_validate_cb(void *old_options, void *options, 
    131131/** "Extra" variable in the state that receives lines we can't parse. This
    132132 * lets us preserve options from versions of Tor newer than us. */
    133133static config_var_t state_extra_var = {
    134   "__extra", CONFIG_TYPE_LINELIST, STRUCT_OFFSET(or_state_t, ExtraLines), NULL
     134  "__extra", CONFIG_TYPE_LINELIST, offsetof(or_state_t, ExtraLines), NULL
    135135};
    136136
    137137/** Configuration format for or_state_t. */
    138138static const config_format_t state_format = {
    139139  sizeof(or_state_t),
    140140  OR_STATE_MAGIC,
    141   STRUCT_OFFSET(or_state_t, magic_),
     141  offsetof(or_state_t, magic_),
    142142  state_abbrevs_,
    143143  NULL,
    144144  state_vars_,
  • src/test/test_containers.c

    diff --git a/src/test/test_containers.c b/src/test/test_containers.c
    index 54484a2a9..0005dcb02 100644
    a b test_container_pqueue(void *arg) 
    681681{
    682682  smartlist_t *sl = smartlist_new();
    683683  int (*cmp)(const void *, const void*);
    684   const int offset = STRUCT_OFFSET(pq_entry_t, idx);
     684  const int offset = offsetof(pq_entry_t, idx);
    685685#define ENTRY(s) pq_entry_t s = { #s, -1 }
    686686  ENTRY(cows);
    687687  ENTRY(zebras);