twemproxy中經典 bug分析

      在nc_message.c中定義了局部變量sendv,且定義爲棧上空間,在後續調用中array_push(&sendv),如果array的size達到預定義的值,在array_push會對send->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);
         }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章