<div dir="auto">I might be wrong, but probably this is the line which triggered the warning.<div dir="auto"><div dir="auto"><a href="https://github.com/robertu94/libpressio/blob/master/include/libpressio_ext/cpp/data.h#L247">https://github.com/robertu94/libpressio/blob/master/include/libpressio_ext/cpp/data.h#L247</a><br></div></div><br><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Mon, Mar 16, 2020, 05:35 Artem Dergachev via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It looks like we don't understand that size_in_bytes() always returns <br>
the same value. There may be multiple reasons for this. I'd like to see <br>
the whole code in order to understand what's going on. Could you produce <br>
a preprocessed file and attach it together with the analyzer invocation?<br>
<br>
As a workaround / suppression you should be able to do something like this:<br>
<br>
   static void *make_data_ptr(RHS rhs) {<br>
     if (!rhs.has_data())<br>
       return nullptr;<br>
<br>
     size_t size = rhs.size_in_bytes();<br>
     return size ? malloc(size) : nullptr;<br>
   }<br>
<br>
   Ctor(RHS rhs): data_ptr(make_data_ptr(rhs)) {}<br>
<br>
This would prevent the analyzer (and, well, your actual generated code) <br>
from calling size_in_bytes() twice and believing that it may return two <br>
different values.<br>
<br>
On 2/28/20 5:41 PM, Robert Underwood via cfe-dev wrote:<br>
><br>
> To whom it may concern,<br>
><br>
> tl;dr I think I found a bug in the clang static analyzer. Could <br>
> someone please help me find a workaround/where to properly report the bug?<br>
><br>
> I have a statement in a class initializer list:<br>
><br>
> |data_ptr((rhs.has_data() && rhs.size_in_bytes() > 0)? <br>
> malloc(rhs.size_in_bytes()) : nullptr)|<br>
><br>
> rhs.has_data() is a const function that returns t/f if rhs.data_ptr != <br>
> null<br>
><br>
> rhs.size_in_bytes() a const function that returns the number of bytes <br>
> in the structure *if has_data was true*<br>
><br>
> i.e. it is possible that rhs.has_data() is false, and <br>
> rhs.size_in_bytes() > 0<br>
><br>
> However the clang static analyzer seems to get a false positive here.<br>
><br>
> |/usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/include/g++-v8/bits/unique_ptr.h:831:34: <br>
> note: Calling copy constructor for 'pressio_data' <br>
> ../include/libpressio_ext/cpp/data.h:247:15: note: Left side of '&&' <br>
> is true data_ptr((rhs.has_data() && rhs.size_in_bytes() > 0)? <br>
> malloc(rhs.size_in_bytes()) : nullptr), ^ <br>
> ../include/libpressio_ext/cpp/data.h:247:33: note: Assuming the <br>
> condition is true data_ptr((rhs.has_data() && rhs.size_in_bytes() > <br>
> 0)? malloc(rhs.size_in_bytes()) : nullptr), ^ <br>
> ../include/libpressio_ext/cpp/data.h:247:14: note: '?' condition is <br>
> true data_ptr((rhs.has_data() && rhs.size_in_bytes() > 0)? <br>
> malloc(rhs.size_in_bytes()) : nullptr), ^ <br>
> ../include/libpressio_ext/cpp/data.h:247:66: note: Calling <br>
> 'pressio_data::size_in_bytes' data_ptr((rhs.has_data() && <br>
> rhs.size_in_bytes() > 0)? malloc(rhs.size_in_bytes()) : nullptr), ^ <br>
> ../include/libpressio_ext/cpp/data.h:384:12: note: Calling <br>
> 'data_size_in_bytes<unsigned long>' return <br>
> data_size_in_bytes(data_dtype, num_dimensions(), dims.data()); ^ <br>
> ../include/libpressio_ext/cpp/data.h:31:5: note: Returning zero return <br>
> data_size_in_elements(dimensions, dims) * pressio_dtype_size(type); ^ <br>
> ../include/libpressio_ext/cpp/data.h:384:12: note: Returning from <br>
> 'data_size_in_bytes<unsigned long>' return <br>
> data_size_in_bytes(data_dtype, num_dimensions(), dims.data()); ^ <br>
> ../include/libpressio_ext/cpp/data.h:384:5: note: Returning zero <br>
> return data_size_in_bytes(data_dtype, num_dimensions(), dims.data()); <br>
> ^ ../include/libpressio_ext/cpp/data.h:247:66: note: Returning from <br>
> 'pressio_data::size_in_bytes' data_ptr((rhs.has_data() && <br>
> rhs.size_in_bytes() > 0)? malloc(rhs.size_in_bytes()) : nullptr), ^ <br>
> ../include/libpressio_ext/cpp/data.h:247:59: note: Call to 'malloc' <br>
> has an allocation size of 0 bytes data_ptr((rhs.has_data() && <br>
> rhs.size_in_bytes() > 0)? malloc(rhs.size_in_bytes()) : nullptr), |<br>
><br>
> Is there a way to instruct the static analyzer that malloc cannot be <br>
> called with size_in_bytes == 0 because saying (rhs.size_in_bytes() > <br>
> 0) isn’t enough. I’m using clang-9.0.1 on gentoo.<br>
><br>
> Respectfully,<br>
> Robert Underwood<br>
><br>
><br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank" rel="noreferrer">cfe-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" rel="noreferrer">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>