changeset 2807:6356e2ceb493

Fix issue #99 (PamWriter test segfaults on alpine linux with gcc 6.4.0)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Sep 2018 15:58:26 +0200
parents 4fdb25c5df9c
children 37583cd183ed
files Core/Endianness.h Core/Images/PamReader.cpp Core/Images/PamWriter.cpp NEWS
diffstat 4 files changed, 31 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Endianness.h	Tue Sep 04 15:07:40 2018 +0200
+++ b/Core/Endianness.h	Tue Sep 04 15:58:26 2018 +0200
@@ -145,13 +145,18 @@
 
 static inline uint16_t __orthanc_bswap16(uint16_t a)
 {
-  const uint8_t* p = reinterpret_cast<const uint8_t*>(&a);
-  return (static_cast<uint16_t>(p[0]) << 8 |
-          static_cast<uint16_t>(p[1]));
-
-  // WARNING: The implementation below makes LSB (Linux Standard
-  // Base) segfault in release builds. Don't use it!!!
-  // return (a << 8) | (a >> 8);
+  /**
+   * Note that an alternative implementation was included in Orthanc
+   * 1.4.0 and 1.4.1:
+   * 
+   *  # hg log -p -r 2706
+   *
+   * This alternative implementation only hid an underlying problem
+   * with pointer alignment on some architectures, and was thus
+   * reverted. Check out issue #99:
+   * https://bitbucket.org/sjodogne/orthanc/issues/99
+   **/
+  return (a << 8) | (a >> 8);
 }
 
 static inline uint32_t __orthanc_bswap32(uint32_t a)
--- a/Core/Images/PamReader.cpp	Tue Sep 04 15:07:40 2018 +0200
+++ b/Core/Images/PamReader.cpp	Tue Sep 04 15:58:26 2018 +0200
@@ -209,7 +209,8 @@
       throw OrthancException(ErrorCode_NotImplemented);
     }
     
-    if (Toolbox::DetectEndianness() == Endianness_Little && bytesPerChannel == 2)
+    if (Toolbox::DetectEndianness() == Endianness_Little &&
+        bytesPerChannel == 2)
     {
       for (unsigned int h = 0; h < height; ++h)
       {
@@ -217,7 +218,12 @@
         
         for (unsigned int w = 0; w < GetWidth(); ++w, ++pixel)
         {
-          *pixel = htobe16(*pixel);
+          // memcpy() is necessary to avoid segmentation fault if the
+          // "pixel" pointer is not 16-bit aligned (which is the case
+          // if "offset" is an odd number). Check out issue #99:
+          // https://bitbucket.org/sjodogne/orthanc/issues/99
+          uint16_t v = htobe16(*pixel);
+          memcpy(pixel, &v, sizeof(v));
         }
       }
     }
--- a/Core/Images/PamWriter.cpp	Tue Sep 04 15:07:40 2018 +0200
+++ b/Core/Images/PamWriter.cpp	Tue Sep 04 15:58:26 2018 +0200
@@ -130,7 +130,13 @@
         
         for (unsigned int w = 0; w < width * channelCount; ++w)
         {
-          *q = htobe16(*p);
+          // memcpy() is necessary to avoid segmentation fault if the
+          // "pixel" pointer is not 16-bit aligned (which is the case
+          // if "offset" is an odd number). Check out issue #99:
+          // https://bitbucket.org/sjodogne/orthanc/issues/99
+          uint16_t v = htobe16(*p);
+          memcpy(q, &v, sizeof(uint16_t));
+
           p++;
           q++;
         }
--- a/NEWS	Tue Sep 04 15:07:40 2018 +0200
+++ b/NEWS	Tue Sep 04 15:58:26 2018 +0200
@@ -5,7 +5,8 @@
 -------
 
 * "OrthancPeers" configuration option now allows to specify HTTP headers
-* Fix "/series/.../ordered-slices" in the presence of non-parallel slices
+* New main DICOM tag: "ImageOrientationPatient" at the instance level
+* New configuration option: "HttpVerbose" to debug outgoing HTTP connections
 
 REST API
 --------
@@ -22,14 +23,14 @@
 Maintenance
 -----------
 
-* New main DICOM tag: "ImageOrientationPatient" at the instance level
-* New configuration option: "HttpVerbose" to debug outgoing HTTP connections
+* Fix "/series/.../ordered-slices" in the presence of non-parallel slices
 * Fix incoming DICOM C-Store filtering for JPEG-LS transfer syntaxes
 * Fix OrthancPluginHttpClient() to return the HTTP status on errors
 * Fix HTTPS requests to sites using a certificate encrypted with ECDSA
 * Fix handling of incoming C-FIND queries containing Generic Group Length (*, 0x0000)
 * Fix issue 54 (quoting multipart answers), for OsiriX compatibility through DICOMweb
 * Fix issue 98 (DCMTK configuration fails with GCC 6.4.0 on Alpine)
+* Fix issue 99 (PamWriter test segfaults on alpine linux with gcc 6.4.0)
 
 
 Version 1.4.1 (2018-07-17)