<div dir="ltr">While equivalent, would it be easier to understand if the two functions were flipped around?<div><br></div><div><span style="font-family:arial,sans-serif;font-size:13px">bool isValidOffset(uint32_t offset) const { return </span><span style="font-family:arial,sans-serif;font-size:13px">isValidOffsetForDataOfSize(offset, 1)</span><span style="font-family:arial,sans-serif;font-size:13px">; }</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">bool isValidOffsetForDataOfSize(</span><span style="font-family:arial,sans-serif;font-size:13px">uint32_t offset, uint32_t length) const { return </span><span style="font-family:arial,sans-serif;font-size:13px">offset + length >= offset &&</span><span style="font-family:arial,sans-serif;font-size:13px"> </span><span style="font-size:13px;font-family:arial,sans-serif">Data.size() >= offset+</span><span style="font-size:13px;font-family:arial,sans-serif">length; </span><span style="font-size:13px;font-family:arial,sans-serif">}</span></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 24, 2014 at 8:13 PM, Benjamin Kramer <span dir="ltr"><<a href="mailto:benny.kra@gmail.com" target="_blank">benny.kra@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
> On 23.10.2014, at 21:52, Timur Iskhodzhanov <<a href="mailto:timurrrr@google.com">timurrrr@google.com</a>> wrote:<br>
><br>
> Hi Ben,<br>
><br>
> Me and David were discussing ways to use DataExtractor recently and I<br>
> found one thing in the comments that seems wrong.<br>
><br>
> From include/llvm/Support/DataExtractor.h:<br>
> 336 /// Test the validity of \a offset.<br>
> 337 ///<br>
> 338 /// @return<br>
> 339 /// \b true if \a offset is a valid offset into the data in this<br>
> 340 /// object, \b false otherwise.<br>
> 341 bool isValidOffset(uint32_t offset) const { return Data.size() > offset; }<br>
> 342<br>
> 343 /// Test the availability of \a length bytes of data from \a offset.<br>
> 344 ///<br>
> 345 /// @return<br>
> 346 /// \b true if \a offset is a valid offset and there are \a<br>
> 347 /// length bytes available at that offset, \b false otherwise.<br>
> 348 bool isValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const {<br>
> 349 return offset + length >= offset && isValidOffset(offset + length - 1);<br>
> 350 }<br>
><br>
> The "if \a offset is a valid offset" part of the comment for<br>
> isValidOffsetForDataOfSize doesn't seem right, as it doesn't actually<br>
> call isValidOffset(offset). Specifically,<br>
> "isValidOffsetForDataOfSize(x, 0)" is equivalent to "isValidOffset(x -<br>
> 1)" rather than "isValidOffset(x)", which is surprising (at least if<br>
> you don't look at the implementation).<br>
<br>
</div></div>length=0 is an edge case for this function, it's designed to validate a read of a non-zero size from a specific offset. If you have a use case you can just make it call isValidOffset(Offset) in that case. This also explains the 'surprising' -1 behavior as for a read of 1 byte you only have to check if the offset is valid.<br>
<br>
DataExtractor was initially designed after a LLDB class of the same name, this function seems to have diverged a lot since :(<br>
<span class=""><br>
> The other thing we've discussed is how to check DE for "everything<br>
> we've read so far was at valid offsets". The first solution that<br>
> comes to mind is "isValidOffset(NextByte - 1)"... but I wonder if<br>
> there are any typical usage patterns that should be addressed in a<br>
> more natural way. Like "hasEverReadPastEndOfBuffer()" flag maybe?<br>
<br>
</span>Adding flags to DataExtractor wouldn't be nice as it's designed to have as little mutable state as possible, better add your own class on top of DE if you need that behavior.<br>
<br>
- Ben<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div><br></div>