Log4cxx
  1. Log4cxx
  2. LOGCXX-183

Compiler warning: dereferencing type-punned pointer will break strict-aliasing rules

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 0.10.0
    • Fix Version/s: None
    • Component/s: Build
    • Labels:
      None

      Description

      The following line of code:
      void* oldPtr = ObjectPtrBase::exchange((volatile void**) &this->p, newPtr);
      in the ObjectPtrT template class (objectptr.h) produces a 'dereferencing type-punned pointer will break strict aliasing rules' compiler warning.

      This line can be found (with slight variances) in the destructor, operator=(const int& null), operator=(T* p1), and operator=(const ObjectPtrT& p1) functions.

      This warning was produced with gcc v4.1.1 using the following compiler options:
      -Wall -Wextra -Wno-unused-parameter -O3 -mtune-i686

      After investigating, the warning is being caused by the first parameter.

      Replacing all instances of the aforementioned code with the following makes the warning disappear:
      T** pp = &this->p;
      void* oldPtr = ObjectPtrBase::exchange((volatile void**) pp, newPtr);

      A temporary workaround is to use the '-fno-strict-aliasing' compiler option.

        Activity

        Myles Bunbury created issue -
        Hide
        Curt Arnold added a comment -

        Committed rev 588376.

        Warning does not show up in earlier gcc's. I was able to encounter the warning with gcc 4.1.3 (using Ubuntu 7.10 x86 VM) and resolve the issue with the changes. I dropped the -mtune-i686 since it wasn't recognized and didn't seem essential to provoke the warning. I also got the same warning in fileoutputstream, thread and threadlocal.cpp. I'm still unclear what could go wrong with the previous formulation and am not sure that the change would really fix the problem (if there is one) or just mask it. I simplified the HELGRIND variant by embedding it in a macro.

        The only behavioral change is that ObjectPtr destructors no longer clear the pointer when it previously called exchange() to set it to zero. Should only be a good thing and eliminates the need to make sure that APR is terminated after all smart pointers are destructed.

        Show
        Curt Arnold added a comment - Committed rev 588376. Warning does not show up in earlier gcc's. I was able to encounter the warning with gcc 4.1.3 (using Ubuntu 7.10 x86 VM) and resolve the issue with the changes. I dropped the -mtune-i686 since it wasn't recognized and didn't seem essential to provoke the warning. I also got the same warning in fileoutputstream, thread and threadlocal.cpp. I'm still unclear what could go wrong with the previous formulation and am not sure that the change would really fix the problem (if there is one) or just mask it. I simplified the HELGRIND variant by embedding it in a macro. The only behavioral change is that ObjectPtr destructors no longer clear the pointer when it previously called exchange() to set it to zero. Should only be a good thing and eliminates the need to make sure that APR is terminated after all smart pointers are destructed.
        Curt Arnold made changes -
        Field Original Value New Value
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]

          People

          • Assignee:
            Curt Arnold
            Reporter:
            Myles Bunbury
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development