gdImagePtr _php_image_create_from_string(zval *data, char *tn, gdImagePtr (*ioctx_func_p)())
{
gdImagePtr im;
gdIOCtx *io_ctx;
io_ctx = gdNewDynamicCtxEx(Z_STRLEN_P(data),Z_STRVAL_P(data), 0); //这边的传入参数的长度为0x80000000 由于是int,所以这边整数溢出。
………………………………………………………………………………
gdNewDynamicCtxEx函数代码,行号就不说了。
gdIOCtx * gdNewDynamicCtxEx (int initialSize, void *data, int freeOKFlag)
{
dpIOCtx *ctx;
dynamicPtr *dp;
ctx = (dpIOCtx *) gdMalloc (sizeof (dpIOCtx));
dp = newDynamic(initialSize, data, freeOKFlag);
ctx->dp = dp; //这边将initialSize 赋值了
ctx->ctx.getC =dynamicGetchar; //这边造成栈溢出,跟踪进去进行了。
………………………………………………………………………………
newDynamic函数代码
static dynamicPtr * newDynamic (int initialSize, void *data, int freeOKFlag)
{
dynamicPtr *dp;
dp = (dynamicPtr *) gdMalloc (sizeof (dynamicPtr)); // 这边申请了64个字节
allocDynamic (dp, initialSize, data);
………………………………………………………..
static int
allocDynamic (dynamicPtr * dp, int initialSize, void *data)
{
if (data == NULL) {
dp->logicalSize = 0;
dp->dataGood = FALSE;
dp->data = gdMalloc(initialSize); 这里的initialSize的值为-2147483648
} else {
dp->logicalSize = initialSize;
dp->dataGood = TRUE;
dp->data = data;
}
……………………………………………….
static int dynamicGetchar (gdIOCtxPtr ctx)
{
unsigned char b;
int rv;
rv = dynamicGetbuf (ctx, &b, 1);
………………………………………………………………………………
static int dynamicGetbuf (gdIOCtxPtr ctx, void *buf, int len)
{
int rlen, remain;
dpIOCtxPtr dctx;
dynamicPtr *dp;
dctx = (dpIOCtxPtr) ctx;
dp = dctx->dp;
remain = dp->logicalSize - dp->pos;
if (remain >= len) {
rlen = len;
} else {
if (remain == 0) {
return EOF;
}
rlen = remain;//上面的remain 的值为负数,没有检查,直接比较后赋值,所以导致下面的rlen的值过大造成栈溢出。
}
memcpy(buf, (void *) ((char *) dp->data + dp->pos), rlen); //这边造成了栈溢出