[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

COPY_TWO_BYTES and C strict aliasing

From: Philip Martin <philip.martin_at_wandisco.com>
Date: Fri, 01 Jun 2012 13:22:35 +0100

GCC gives a compiler warning where the COPY_TWO_BYTES macro is used. A
typical warning is:

../src/subversion/libsvn_subr/string.c:971:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
           COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
           ^

The code is:

/* Copy the two bytes at SOURCE[0] and SOURCE[1] to DEST[0] and DEST[1] */
#if SVN_UNALIGNED_ACCESS_IS_OK
# define COPY_TWO_BYTES(dest,source)\
      *(apr_uint16_t*)(dest) = *(apr_uint16_t*)(source);
#else
# define COPY_TWO_BYTES(dest,source) \
    do { \
      (dest)[0] = (source)[0]; \
      (dest)[1] = (source)[1]; \
    } while (0)
#endif

apr_size_t
svn__ui64toa(char * dest, apr_uint64_t number)
{
  char buffer[SVN_INT64_BUFFER_SIZE];
  apr_uint32_t reduced; /* used for 32 bit DIV */
  char* target;

  /* Small numbers are by far the most common case.
   * Therefore, we use special code.
   */
  if (number < 100)
    {
      if (number < 10)
        {
          dest[0] = (char)('0' + number);
          dest[1] = 0;
          return 1;
        }
      else
        {
          COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
          dest[2] = 0;
          return 2;
        }
    }

Is the warning something we can ignore? My understanding of C aliasing
is that writing to memory is only supposed to happen as the declared
type of that memory.

Is COPY_TWO_BYTES a significant optimisation? On Linux we can avoid the
warning by simply using

   memcpy(dest, source, 2)

since that memcpy call will be inlined these days.

-- 
uberSVN: Apache Subversion Made Easy
http://www.uberSVN.com
Received on 2012-06-01 14:23:16 CEST

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.