[cfe-commits] strncpy checker - proposed patch

Ted Kremenek kremenek at apple.com
Tue Dec 21 08:52:56 PST 2010

On Dec 21, 2010, at 7:04 AM, Lenny Maiorani wrote:

> Is that patch in the queue to be reviewed and committed? I just want to make sure it wasn't dropped...
> -Lenny
> On Dec 17, 2010, at 5:22 PM, Lenny Maiorani wrote:
>> After taking Argiris's advice, attached is a patch submittal for adding strncpy() buffer overflow checker to CStringChecker.cpp and associated unit tests.
>> -Lenny
>> <strncpy-checker.diff>
>> the definition of open: "mkdir android ; cd android ; repo init -i git://android.git.kernel.org/platform/manifest.git ; repo sync ; make" - Andy Rubin

Hi Lenny,

Thank you for your patience.  Overall the patch looks great, but I'm a little confused about the following section:

>    // Get the string length of the source.
>    SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
> +  if (isStrncpy) {
> +    // Check if the number of bytes to copy is less than the size of the src
> +    const Expr *lenExpr = CE->getArg(2);
> +    strLength = state->getSVal(lenExpr);
> +  }
> +
>    // If the source isn't a valid C string, give up.
>    if (strLength.isUndef())
>      return;

This looks like an intermingling of logic that it's not clear should compose is this way.

At the beginning we (a) fetch a value for 'strLength', then (b) overwrite that value if 'isStrncpy' is true, and then (c) we check if strLength is undefined.   Both (a) and (b) look like competing logic.  If they are truly mutually exclusive, I rather have one, but not both, get computed.  This logic also looks slightly pessimistic, as the length of the string can be smaller than the max number of bytes specified to strncpy().  If the value retrieved at (a) is less than the value retrieved at (b), should we use the strLength from (a) and not (b)?  I can see the argument to always use the most pessimistic value, but then our error reporting should probably reflect that 'size_t n' argument to strncpy() is too large, and not necessarily that we have a buffer overflow.  That would make it clearer to the user what they actually need to fix in their code (i.e., while it might not be a buffer overflow, it is one waiting to happen, etc.).

Overall, this looks great.  I'd just like to iron these last details out a bit (and document the final design decision in the code itself with comments) so it's clear the checker is always doing what you intend and that the user understands why they are getting a warning for their code.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20101221/d6b6119e/attachment.html>

More information about the cfe-commits mailing list