comparison Core/Images/PamReader.cpp @ 3487:ce29644acd19

Prevented unaligned memcpy when using Web Assembly
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 05 Aug 2019 13:57:54 +0200
parents 4e43e67f8ecf
children 94f4a18a79cc
comparison
equal deleted inserted replaced
3486:5e9fae2faff2 3487:ce29644acd19
206 if (bytesPerChannel != 1 && 206 if (bytesPerChannel != 1 &&
207 bytesPerChannel != 2) 207 bytesPerChannel != 2)
208 { 208 {
209 throw OrthancException(ErrorCode_NotImplemented); 209 throw OrthancException(ErrorCode_NotImplemented);
210 } 210 }
211 211
212 if (Toolbox::DetectEndianness() == Endianness_Little && 212 if (Toolbox::DetectEndianness() == Endianness_Little &&
213 bytesPerChannel == 2) 213 bytesPerChannel == 2)
214 { 214 {
215 for (unsigned int h = 0; h < height; ++h) 215 for (unsigned int h = 0; h < height; ++h)
216 { 216 {
217 uint16_t* pixel = reinterpret_cast<uint16_t*>(GetRow(h)); 217 uint16_t* pixel = reinterpret_cast<uint16_t*>(GetRow(h));
218 218
219 for (unsigned int w = 0; w < GetWidth(); ++w, ++pixel) 219 for (unsigned int w = 0; w < GetWidth(); ++w, ++pixel)
220 { 220 {
221 #if ORTHANC_ENABLE_WASM == 1
222 /*
223
224 crash (2019-08-05):
225
226 Uncaught abort(alignment fault) at Error
227 at jsStackTrace
228 at stackTrace
229 at abort
230 at alignfault
231 at SAFE_HEAP_LOAD_i32_2_2 (wasm-function[251132]:39)
232 at __ZN7Orthanc9PamReader12ParseContentEv (wasm-function[11457]:8088)
233
234 Web Assenmbly IS LITTLE ENDIAN!
235
236 Perhaps in htobe16 ?
237 */
238 uint8_t* srcdst = reinterpret_cast<uint8_t*>(pixel);
239 uint8_t tmp = srcdst[0];
240 srcdst[0] = srcdst[1];
241 srcdst[1] = tmp;
242 #else
221 // memcpy() is necessary to avoid segmentation fault if the 243 // memcpy() is necessary to avoid segmentation fault if the
222 // "pixel" pointer is not 16-bit aligned (which is the case 244 // "pixel" pointer is not 16-bit aligned (which is the case
223 // if "offset" is an odd number). Check out issue #99: 245 // if "offset" is an odd number). Check out issue #99:
224 // https://bitbucket.org/sjodogne/orthanc/issues/99 246 // https://bitbucket.org/sjodogne/orthanc/issues/99
225 uint16_t v = htobe16(*pixel); 247 uint16_t v = htobe16(*pixel);
226 memcpy(pixel, &v, sizeof(v)); 248 memcpy(pixel, &v, sizeof(v));
249 #endif
227 } 250 }
228 } 251 }
229 } 252 }
230 } 253 }
231 254