<p dir="ltr">I was just reading about -Wcast-align the other day, isn't this what it does? Or does it only trigger for explicit casts?</p>
<p dir="ltr">- Kim </p>
<div class="gmail_extra"><br><div class="gmail_quote">Den 16 sep. 2016 10:09 em skrev "Vedant Kumar via cfe-dev" <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>>:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
Does it make sense to emit a diagnostic when we implicitly cast a pointer to<br>
another pointer type with a higher required alignment?<br>
<br>
Here's an example where this might be useful. The pointer "uiptr" is at least<br>
1-byte aligned, but it's implicitly cast to "int *" (and treated as at least<br>
8-byte aligned):<br>
<br>
    extern int printf(const char *S, ...);<br>
<br>
    int load_aligned(int *ptr) {<br>
      printf("Alignment of ptr: %lu\n", __alignof__(ptr));<br>
      return *ptr;<br>
    }<br>
<br>
    typedef int unaligned_int __attribute__((aligned(1)));<br>
<br>
    int main() {<br>
      unaligned_int ui = 0;<br>
      unaligned_int *__attribute__((aligned(1))) uiptr = &ui;<br>
      printf("Alignment of uiptr: %lu\n", __alignof__(uiptr));<br>
      return load_aligned(uiptr);<br>
    }<br>
<br>
When compiled with ToT clang, this program prints out:<br>
<br>
    Alignment of uiptr: 1<br>
    Alignment of ptr: 8<br>
<br>
IIUC, users should only see the warning if they opt into lowering the alignment<br>
of a pointer. I wouldn't expect this to be too noisy, but I don't have any data<br>
on this.<br>
<br>
It's true that we can catch this issue with ubsan, but only when the value of<br>
"uiptr" is actually not 8-byte aligned. Besides, it might be helpful to have a<br>
compile-time check for this (e.g in situations where it's tricky to deploy the<br>
sanitizer runtime).<br>
<br>
best,<br>
vedant<br>
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>