mchehab / zbar

ZBar is an open source software suite for reading bar codes from various sources, including webcams. As its development stopped in 2012, I took the task of keeping it updated with the V4L2 API. This is the main repository for it. There's a clone at at LinuxTV.org, and another one at gitlab.
https://linuxtv.org/downloads/zbar/
GNU Lesser General Public License v2.1
1k stars 205 forks source link

Fix memory recycle bug of empty symbols #259

Closed daniel-falk closed 10 months ago

daniel-falk commented 1 year ago

For full background of this PR, see #258

Shortly:

The 0:th bucket in the iscn->recycle array holds a list of recycled zbar_symbol_t with the data_alloc set to 0.

When a 0-sized symbol is requested, the first loop will exit with i=0 which makes sense (since these are in the 0:th index in the recycle array):

    for (i = 0; i < RECYCLE_BUCKETS - 1; i++)
    if (datalen <= 1 << (i * 2))
        break;

But then the next for-loop will never enter, as i > 0 is false:

    for (; i > 0; i--)
    if ((sym = iscn->recycle[i].head)) {
        STAT(sym_recycle[i]);
        break;
    }

This means that sym will still be zero, and a new symbol will be allocated:

    } else {
    sym = calloc(1, sizeof(zbar_symbol_t));
    STAT(sym_new);
    }
mchehab commented 10 months ago

Merged, thanks!