[cfe-dev] Safe use of offsetof?

Richard Smith richard at metafoo.co.uk
Tue Sep 17 12:50:00 PDT 2013


What are you really trying to do? How do you intend to use the result of
this function?
On 17 Sep 2013 12:26, "Jeffrey Walton" <noloader at gmail.com> wrote:

> I'm working with some source code that does the following. Its
> declared as a static method.
>
> 167  long
> 168  cbdata::MakeOffset()
> 169  {
> 170      cbdata *zero = (cbdata *)0L;
> 171      void **dataOffset = &zero->data;
> 172      return (long)dataOffset;
> 173  }
>
> Under UBSan, the code creates an entry for each use of MakeOffset():
>
>     runtime error: member access within null pointer of type 'cbdata'
>
> The code is not amicable to use of offsetof(...). I think its because
> there's a Stack member, but I don't know the C/C++ language rules like
> most folks.
>
>    cbdata.cc:176:19: error: offset of on non-POD type 'struct cbdata'
>
> But I'm fairly certain (correct me here) it falls under case 2 listed
> at http://gcc.gnu.org/ml/gcc/2003-11/msg00281.html:
>
>     2) Those cases where any reasonable implementation (and all
>        implementations we know of) give the right answer, but ISO
>        C++ says "undefined".
>
> Is there a way to code MakeOffset() so that UBSan will be happy so as
> to avoid the runtime error? Or will this have to go into an ignore
> list?
>
> Thanks in advance.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130917/0724ee35/attachment.html>


More information about the cfe-dev mailing list