[cfe-dev] compare two array size

Lionel PRAT lionel.prat9 at gmail.com
Tue Jul 3 05:49:55 PDT 2012


Hello,
I try to see if a checker in a strcpy, the source array size is larger
than the destination array size (overflow).
Here is the piece of code that test and that does not work ...

........
//CE->getArg(1) == src in strcpy
//CE->getArg(1) == dst in strcpy
if(isSizeOverflow(CE->getArg(1),CE->getArg(0), C)){
       //Overflow is possible
}
.......
bool StrcpyCheckOverflow::isSizeOverflow(const Expr *Esrc, const Expr
*Edst, CheckerContext &C) {
QualType ElTydst = Edst->getType()->getPointeeType();
QualType ElTysrc = Esrc->getType()->getPointeeType();
CharUnits BufSrcSize = C.getASTContext().getTypeSizeInChars(ElTysrc);
CharUnits BufDstSize = C.getASTContext().getTypeSizeInChars(ElTydst);
if(BufSrcSize>BufDstSize) return true;
return false;
}


On test code:

#include <stdio.h>
#include <string.h>
int main(){
char addr[16];
char buffer[8];
  scanf("%s", addr);
  strcpy(buffer, addr);
  return 0;
}

Would you or what is the problem?
thank you



More information about the cfe-dev mailing list