<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Aug 9, 2021, at 9:15 AM, Michael D Kinney <<a href="mailto:michael.d.kinney@intel.com" class="">michael.d.kinney@intel.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta charset="UTF-8" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Hi Marvin,</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Can you provide an example of which C compiler is flagging this as</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">an error and what error message is generated.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Please enter a BZ with this background information and add link to the</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">BZ in the commit message.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">This is a change to the BaseLib class, so we need to make sure there</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">are no impacts to any existing code.  I looks like a safe change</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">because changing from a pointer to a fixed size type to VOID *<span class="Apple-converted-space"> </span></span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">should be compatible.  Please add that analysis to the background</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">in the BZ as well.</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""></div></blockquote><div><br class=""></div><div>MIke,</div><div><br class=""></div><div>I want to say we had a discussion about this years ago? I don’t remember the outcome. </div><div><br class=""></div><div>Dereferencing a misaligned pointer is UB (Undefined Behavior) in C [1], but historically x86 compilers have let it slide.</div><div><br class=""></div><div>I think the situation we are in is the BaseLib functions don’t contain UB, but it is UB for the caller to use the returned pointer directly. </div><div><br class=""></div><div>Here is a simple example with clang UndefinedBehaviorSanitizer (UBSan) . </div><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(180, 36, 25);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">~/work/Compiler</span><span style="font-variant-ligatures: no-common-ligatures; color: #9fa01c" class="">></span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">cat ub.c</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">#include <stdlib.h></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">#define EFIAPI</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">#define IN</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">#define OUT</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">typedef unsigned char <span class="Apple-tab-span" style="white-space:pre">    </span>UINT8;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">typedef unsigned short <span class="Apple-tab-span" style="white-space:pre">    </span>UINT16;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">UINT16</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">EFIAPI</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">WriteUnaligned16 (</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  OUT UINT16                    *Buffer,</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  IN  UINT16                    Value</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  )</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  // ASSERT (Buffer != NULL);</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  ((volatile UINT8*)Buffer)[0] = (UINT8)Value;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  ((volatile UINT8*)Buffer)[1] = (UINT8)(Value >> 8);</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">  return Value;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">int main()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">  </span>UINT8 *buffer = malloc(64);</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">      </span>UINT16 *pointer = (UINT16 *)(buffer + 1);</span></div><p style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">      </span>WriteUnaligned16 (pointer, 42);</span></div><p style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">  </span></span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">      </span>// *pointer = 42; // Error: misaligned integer pointer assignment</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return *pointer;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">~/work/Compiler</span><span style="font-variant-ligatures: no-common-ligatures; color: #9fa01c" class="">></span><span style="font-variant-ligatures: no-common-ligatures" class="">clang -fsanitize=undefined  ub.c</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(180, 36, 25);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">~/work/Compiler</span><span style="font-variant-ligatures: no-common-ligatures; color: #9fa01c" class="">></span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">./a.out</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">ub.c:34:9:</b></span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class=""><b class=""> runtime error: </b></span><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">load of misaligned address 0x7feac6405aa1 for type 'UINT16' (aka 'unsigned short'), which requires 2 byte alignment</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">0x7feac6405aa1: note: </b>pointer points here</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> 00 00 00  64 2a 00 79 6d 28 52 54  4c 44 5f 44 45 46 41 55  4c 54 2c 20 73 77 69 66  74 5f 64 65 6d</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">              ^ </b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ub.c:34:9 in </span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div>FYI line 39 is `return *pointer`and 42 is 0x2A. So reading an writing to *pointer is UB. </div><div><br class=""></div><div><br class=""></div><div>As you can see in [1] the general advice is to take code that looks like:</div><div><span data-v-4403ff9a="" class="code-line-container" style="display: flex; flex-shrink: 0; font-family: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace; font-size: 14.999950408935547px; letter-spacing: -0.40499866008758545px; white-space: pre;"><span data-v-4403ff9a="" class="code-line">int8_t *buffer = malloc(<span class="syntax-number" style="color: var(--syntax-number,var(--color-syntax-characters));">64</span>);</span></span><span data-v-4403ff9a="" class="code-line-container" style="display: flex; flex-shrink: 0; font-family: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace; font-size: 14.999950408935547px; letter-spacing: -0.40499866008758545px; white-space: pre;"><span data-v-4403ff9a="" class="code-line">int32_t *pointer = (int32_t *)(buffer + <span class="syntax-number" style="color: var(--syntax-number,var(--color-syntax-characters));">1</span>);</span></span><span data-v-4403ff9a="" class="code-line-container" style="display: flex; flex-shrink: 0; font-family: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace; font-size: 14.999950408935547px; letter-spacing: -0.40499866008758545px; white-space: pre;"><span data-v-4403ff9a="" class="code-line">*pointer = <span class="syntax-number" style="color: var(--syntax-number,var(--color-syntax-characters));">42</span>; <span class="syntax-comment" style="color: var(--syntax-comment,var(--color-syntax-comments));">// Error: misaligned integer pointer assignment</span></span></span></div><div>And replace it with;</div><div><span data-v-4403ff9a="" class="code-line-container" style="display: flex; flex-shrink: 0; font-family: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace; font-size: 14.999950408935547px; letter-spacing: -0.40499866008758545px; white-space: pre;"><span data-v-4403ff9a="" class="code-line">int8_t *buffer = malloc(<span class="syntax-number" style="color: var(--syntax-number,var(--color-syntax-characters));">64</span>);</span></span><span data-v-4403ff9a="" class="code-line-container" style="display: flex; flex-shrink: 0; font-family: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace; font-size: 14.999950408935547px; letter-spacing: -0.40499866008758545px; white-space: pre;"><span data-v-4403ff9a="" class="code-line">int32_t value = <span class="syntax-number" style="color: var(--syntax-number,var(--color-syntax-characters));">42</span>;</span></span><span data-v-4403ff9a="" class="code-line-container" style="display: flex; flex-shrink: 0; font-family: "SF Mono", SFMono-Regular, ui-monospace, Menlo, monospace; font-size: 14.999950408935547px; letter-spacing: -0.40499866008758545px; white-space: pre;"><span data-v-4403ff9a="" class="code-line">memcpy(buffer + <span class="syntax-number" style="color: var(--syntax-number,var(--color-syntax-characters));">1</span>, &value, <span class="syntax-keyword" style="color: var(--syntax-keyword,var(--color-syntax-keywords));">sizeof</span>(int32_t)); <span class="syntax-comment" style="color: var(--syntax-comment,var(--color-syntax-comments));">// Correct</span></span></span></div><div><br class=""></div><div>But in these cases the result is in a byte aligned buffer….</div><div><br class=""></div><div>[1] <a href="https://developer.apple.com/documentation/xcode/misaligned-pointer" class="">https://developer.apple.com/documentation/xcode/misaligned-pointer</a></div><div><br class=""></div><div>Thanks,</div><div><br class=""></div><div>Andrew Fish</div><br class=""><blockquote type="cite" class=""><div class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Thanks,</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Mike</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">-----Original Message-----<br class="">From: Marvin Häuser <<a href="mailto:mhaeuser@posteo.de" class="">mhaeuser@posteo.de</a>><br class="">Sent: Monday, August 9, 2021 2:51 AM<br class="">To:<span class="Apple-converted-space"> </span><a href="mailto:devel@edk2.groups.io" class="">devel@edk2.groups.io</a><br class="">Cc: Kinney, Michael D <<a href="mailto:michael.d.kinney@intel.com" class="">michael.d.kinney@intel.com</a>>; Liming Gao <<a href="mailto:gaoliming@byosoft.com.cn" class="">gaoliming@byosoft.com.cn</a>>; Liu, Zhiguang<br class=""><<a href="mailto:zhiguang.liu@intel.com" class="">zhiguang.liu@intel.com</a>>; Vitaly Cheptsov <<a href="mailto:vit9696@protonmail.com" class="">vit9696@protonmail.com</a>><br class="">Subject: [PATCH v2 1/2] MdePkg/BaseLib: Fix unaligned API prototypes<br class=""><br class="">C prohibits not only dereferencing but also casting to unaligned<br class="">pointers. Thus, the current set of unaligned APIs cannot be called<br class="">safely. Update their prototypes to take VOID * pointers, which must<br class="">be able to represent any valid pointer.<br class=""><br class="">Cc: Michael D Kinney <<a href="mailto:michael.d.kinney@intel.com" class="">michael.d.kinney@intel.com</a>><br class="">Cc: Liming Gao <<a href="mailto:gaoliming@byosoft.com.cn" class="">gaoliming@byosoft.com.cn</a>><br class="">Cc: Zhiguang Liu <<a href="mailto:zhiguang.liu@intel.com" class="">zhiguang.liu@intel.com</a>><br class="">Cc: Vitaly Cheptsov <<a href="mailto:vit9696@protonmail.com" class="">vit9696@protonmail.com</a>><br class="">Signed-off-by: Marvin Häuser <<a href="mailto:mhaeuser@posteo.de" class="">mhaeuser@posteo.de</a>><br class="">---<br class="">MdePkg/Library/BaseLib/Arm/Unaligned.c | 14 ++++-----<br class="">MdePkg/Library/BaseLib/Unaligned.c     | 32 ++++++++++----------<br class="">MdePkg/Include/Library/BaseLib.h       | 16 +++++-----<br class="">3 files changed, 31 insertions(+), 31 deletions(-)<br class=""><br class="">diff --git a/MdePkg/Library/BaseLib/Arm/Unaligned.c b/MdePkg/Library/BaseLib/Arm/Unaligned.c<br class="">index e9934e7003cb..57f19fc44e0b 100644<br class="">--- a/MdePkg/Library/BaseLib/Arm/Unaligned.c<br class="">+++ b/MdePkg/Library/BaseLib/Arm/Unaligned.c<br class="">@@ -59,7 +59,7 @@ ReadUnaligned16 (<br class="">UINT16<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned16 (<br class=""><br class="">-  OUT UINT16                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT16                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">@@ -87,7 +87,7 @@ WriteUnaligned16 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned24 (<br class=""><br class="">-  IN CONST UINT32              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class="">@@ -116,7 +116,7 @@ ReadUnaligned24 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned24 (<br class=""><br class="">-  OUT UINT32                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT32                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">@@ -143,7 +143,7 @@ WriteUnaligned24 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned32 (<br class=""><br class="">-  IN CONST UINT32              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  UINT16  LowerBytes;<br class=""><br class="">@@ -175,7 +175,7 @@ ReadUnaligned32 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned32 (<br class=""><br class="">-  OUT UINT32                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT32                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">@@ -202,7 +202,7 @@ WriteUnaligned32 (<br class="">UINT64<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned64 (<br class=""><br class="">-  IN CONST UINT64              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  UINT32  LowerBytes;<br class=""><br class="">@@ -234,7 +234,7 @@ ReadUnaligned64 (<br class="">UINT64<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned64 (<br class=""><br class="">-  OUT UINT64                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT64                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">diff --git a/MdePkg/Library/BaseLib/Unaligned.c b/MdePkg/Library/BaseLib/Unaligned.c<br class="">index a419cb85e53c..3041adcde606 100644<br class="">--- a/MdePkg/Library/BaseLib/Unaligned.c<br class="">+++ b/MdePkg/Library/BaseLib/Unaligned.c<br class="">@@ -26,12 +26,12 @@<br class="">UINT16<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned16 (<br class=""><br class="">-  IN CONST UINT16              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer;<br class=""><br class="">+  return *(CONST UINT16 *) Buffer;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">/**<br class=""><br class="">@@ -52,13 +52,13 @@ ReadUnaligned16 (<br class="">UINT16<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned16 (<br class=""><br class="">-  OUT UINT16                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT16                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer = Value;<br class=""><br class="">+  return *(UINT16 *) Buffer = Value;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">/**<br class=""><br class="">@@ -77,12 +77,12 @@ WriteUnaligned16 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned24 (<br class=""><br class="">-  IN CONST UINT32              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer & 0xffffff;<br class=""><br class="">+  return *(CONST UINT32 *) Buffer & 0xffffff;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">/**<br class=""><br class="">@@ -103,13 +103,13 @@ ReadUnaligned24 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned24 (<br class=""><br class="">-  OUT UINT32                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT32                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  *Buffer = BitFieldWrite32 (*Buffer, 0, 23, Value);<br class=""><br class="">+  *(UINT32 *) Buffer = BitFieldWrite32 (*(CONST UINT32 *) Buffer, 0, 23, Value);<br class=""><br class="">  return Value;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">@@ -129,12 +129,12 @@ WriteUnaligned24 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned32 (<br class=""><br class="">-  IN CONST UINT32              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer;<br class=""><br class="">+  return *(CONST UINT32 *) Buffer;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">/**<br class=""><br class="">@@ -155,13 +155,13 @@ ReadUnaligned32 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned32 (<br class=""><br class="">-  OUT UINT32                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT32                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer = Value;<br class=""><br class="">+  return *(UINT32 *) Buffer = Value;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">/**<br class=""><br class="">@@ -180,12 +180,12 @@ WriteUnaligned32 (<br class="">UINT64<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned64 (<br class=""><br class="">-  IN CONST UINT64              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer;<br class=""><br class="">+  return *(CONST UINT64 *) Buffer;<br class=""><br class="">}<br class=""><br class=""><br class=""><br class="">/**<br class=""><br class="">@@ -206,11 +206,11 @@ ReadUnaligned64 (<br class="">UINT64<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned64 (<br class=""><br class="">-  OUT UINT64                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT64                    Value<br class=""><br class="">  )<br class=""><br class="">{<br class=""><br class="">  ASSERT (Buffer != NULL);<br class=""><br class=""><br class=""><br class="">-  return *Buffer = Value;<br class=""><br class="">+  return *(UINT64 *) Buffer = Value;<br class=""><br class="">}<br class=""><br class="">diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h<br class="">index 2452c1d92e51..4d30f0539c6b 100644<br class="">--- a/MdePkg/Include/Library/BaseLib.h<br class="">+++ b/MdePkg/Include/Library/BaseLib.h<br class="">@@ -3420,7 +3420,7 @@ DivS64x64Remainder (<br class="">UINT16<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned16 (<br class=""><br class="">-  IN CONST UINT16              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class=""><br class=""><br class="">@@ -3442,7 +3442,7 @@ ReadUnaligned16 (<br class="">UINT16<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned16 (<br class=""><br class="">-  OUT UINT16                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT16                    Value<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class="">@@ -3463,7 +3463,7 @@ WriteUnaligned16 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned24 (<br class=""><br class="">-  IN CONST UINT32              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class=""><br class=""><br class="">@@ -3485,7 +3485,7 @@ ReadUnaligned24 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned24 (<br class=""><br class="">-  OUT UINT32                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT32                    Value<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class="">@@ -3506,7 +3506,7 @@ WriteUnaligned24 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned32 (<br class=""><br class="">-  IN CONST UINT32              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class=""><br class=""><br class="">@@ -3528,7 +3528,7 @@ ReadUnaligned32 (<br class="">UINT32<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned32 (<br class=""><br class="">-  OUT UINT32                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT32                    Value<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class="">@@ -3549,7 +3549,7 @@ WriteUnaligned32 (<br class="">UINT64<br class=""><br class="">EFIAPI<br class=""><br class="">ReadUnaligned64 (<br class=""><br class="">-  IN CONST UINT64              *Buffer<br class=""><br class="">+  IN CONST VOID                *Buffer<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class=""><br class=""><br class="">@@ -3571,7 +3571,7 @@ ReadUnaligned64 (<br class="">UINT64<br class=""><br class="">EFIAPI<br class=""><br class="">WriteUnaligned64 (<br class=""><br class="">-  OUT UINT64                    *Buffer,<br class=""><br class="">+  OUT VOID                      *Buffer,<br class=""><br class="">  IN  UINT64                    Value<br class=""><br class="">  );<br class=""><br class=""><br class=""><br class="">--<br class="">2.31.1<br class=""></blockquote><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class=""></span></div></blockquote></div><br class=""></body></html>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr>   Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/78993">View/Reply Online (#78993)</a> |    |  <a target="_blank" href="https://groups.io/mt/84764900/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>