I have recently started looking into clang, and was interested in participating. After taking a look at the potential projects, the static checking functionality seemed interesting. Specifically, I have taken a look at the <span class="name">checker "memory.LeakPtrValChanged</span>" mentioned on the list of potential checkers page.<br>
<br>Warning: As this is my first attempt at hacking clang, I may have gone a different route than someone with more experience in the project. If something in my description or patch seems out of place, please let me know!<br>
<br>From the description, the proposed <span class="name">memory.LeakPtrValChanged checker was to only consider a pointer to newly allocated data losing its original 
value. Through some investigation, I find that MemRegion objects which track pointers to memory allocations can also maintain any offset currently applied to the pointer. Using this information, the checker can reason about invalidated pointers beyond being 'newly allocated'. For example, the following case can be caught:<br>
<br>int * x = malloc(sizeof(int));<br>x += 1;<br>free(x);<br><br>However, the following is valid:<br></span><br><span class="name"><span class="name">int * x = malloc(sizeof(int));<br>x += 1;<br>free(x-1);<br><br></span>The attached patch uses the RegionOffset of freed malloc allocations to determine if the freed pointer has a non-zero offset, and post a warning in this case. If the offset is symbolic (and thus not known to be non-zero), no warning is posted. There are tests included to verify the proposed changes. <br>
<br>Note that <span class="name">memory.LeakPtrValChanged mentioned checking both malloc/free and new/delete, but this patch only considers malloc/free.<br><br>Please let me know if the attached patch is appropriate, or if it is missing something or there is another solution which may be a better fit.<br>
<br>- Branden<br></span></span><span class="name"><br></span>