---
title: "twemproxy中经典 bug分析"
author: "fishermen"
date: "2013-12-03T18:22:13Z"
tags: []
categories: ["开源软件"]
slug: "twemproxy-bug"
draft: false
lastmod: "2013-12-03T18:22:13Z"
id: 1984464
url: "https://fishermen.iteye.com/blog/1984464"
---

在nc\_message.c中定义了局部变量sendv，且定义为栈上空间，在后续调用中array\_push(&sendv)，如果array的size达到预定义的值，在array\_push会对send-&gt;elem进行realloc。

      问题就出现了，sendv是栈上变量，不允许对其进行realloc（只有堆变量才可以）。知道原因了，修复方案很简单，send改为malloc方式获得。

      代码如下：

```
...
struct array sendv;                  /* send iovec */
...

ciov = array_push(&sendv);
            |
            |
        array_push(struct array *a)
        {
         ...
            if (a->nelem == a->nalloc) {
               /* the array is full; allocate new array */                                                         
               size = a->size * a->nalloc;
               new = nc_realloc(a->elem, 2 * size);
         }
```