[PATCH] D88824: [Support][unittests] Enforce alignment in ConvertUTFTest

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 04:59:50 PDT 2020


jhenderson accepted this revision.
jhenderson added a comment.

Looks good.



================
Comment at: llvm/lib/Support/ConvertUTFWrapper.cpp:100
 
+  assert((uintptr_t)Src % sizeof(UTF16) == 0);
+
----------------
ro wrote:
> grimar wrote:
> > I think more natural way is to test pre-conditions for arguments before using them?
> > I.e. I'd move this line before `const UTF16 *Src =...`
> > 
> > ```
> > assert((uintptr_t)SrcBytes.begin() % sizeof(UTF16) == 0);
> > ```
> I had it before the casts initially (but without the `uintptr_t` cast, breaking the build).  I moved it after to avoid doubling the `SrcBytes.begin()`, keeping the line shorter.
> 
> IMO `Src` is used only when dereferenced, but I'm fine either way.
I think this is fine here, thought I might assert `SrcEnd` too for consistency and to show that the `end` result makes sense. I think the key thing is that the thing being read (i.e. `Src`) is aligned. The fact that it came from `SrcBytes` is somewhat irrelevant. In a future version of this code, there might be an alternative way to initialise `Src`, not using `SrcBytes.begin()`, and the assertion ideally would be valid for all such cases.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D88824/new/

https://reviews.llvm.org/D88824



More information about the llvm-commits mailing list