intel / QATzip

Compression Library accelerated by Intel® QuickAssist Technology
https://developer.intel.com/quickassist
Other
137 stars 51 forks source link

memor leak in function testqzEndStreamInvalidParam #109

Open ColinIanKing opened 7 months ago

ColinIanKing commented 7 months ago

the return path in line 2694 is not free'ing the allocated objects orig_src, comp_src, decomp_src, leaking to (non-fatal) memory leaks.

void *testqzEndStreamInvalidParam(void *arg, int test_no)
2671    {
2672        int rc = -1;
2673        QzSession_T comp_sess = {0}, decomp_sess = {0};
2674        QzStream_T comp_strm = {0};
2675        QzSessionParams_T comp_params = {0};
2676        uint8_t *orig_src, *comp_src, *decomp_src;
2677        size_t orig_sz, comp_sz, decomp_sz;
2678        unsigned int slice_sz = 0, done = 0;
2679        unsigned int consumed = 0, produced = 0;
2680        unsigned int input_left = 0, last = 0;
2681     
2682        QzSession_T *test_sess = NULL;
2683        QzStream_T *test_strm = NULL;
2684     
2685        TestArg_T *test_arg = (TestArg_T *) arg;
2686     
2687        orig_sz = comp_sz = decomp_sz = test_arg->src_sz;
2688        orig_src = malloc(orig_sz);
2689        comp_src = malloc(comp_sz);

    Memory is allocated 

2690        decomp_src = calloc(orig_sz, 1);
2691     
2692        if (qzGetDefaults(&comp_params) != QZ_OK) {

    Assuming the condition is true  
    Taking true branch  

2693            QZ_ERROR("Err: fail to get default params.\n");

    Potential leak of memory pointed to by 'comp_src'

2694            return NULL;
2695        }
GarenJian-Intel commented 6 months ago

Thanks! We will fix this issue. Jira ticket created: https://jira.devtools.intel.com/browse/QATAPP-31728

nefigtut commented 2 months ago

+1 to this. our code security scanner blames the same lines:

Defect type: CPPCHECK_WARNING: QATzip-1.2.0/test/main.c:2694: error[memleak]: Memory leak: orig_src QATzip-1.2.0/test/main.c:2694: error[memleak]: Memory leak: decomp_src QATzip-1.2.0/test/main.c:2694: error[memleak]: Memory leak: comp_src QATzip-1.2.0/test/main.c:2520: error[memleak]: Memory leak: orig_src QATzip-1.2.0/test/main.c:2520: error[memleak]: Memory leak: decomp_src QATzip-1.2.0/test/main.c:2520: error[memleak]: Memory leak: comp_src

so, orig_src, comp_src and decomp_src can leak at test/main.c:2694 and test/main.c:2520:

void *testqzEndStreamInvalidParam(void *arg, int test_no)
{
...
    // ALLOC HERE
    orig_src = malloc(orig_sz);
    comp_src = malloc(comp_sz);
    decomp_src = calloc(orig_sz, 1);

    if (qzGetDefaults(&comp_params) != QZ_OK) {
        QZ_ERROR("Err: fail to get default params.\n");
        return NULL; //<-- LEAK HERE
    }

and

void *testqzDecompressStreamInvalidParam(void *arg, int test_no)
{
...
    // ALLOC HERE
    orig_src = malloc(orig_sz);
    comp_src = malloc(comp_sz);
    decomp_src = calloc(orig_sz, 1);

    if (qzGetDefaults(&comp_params) != QZ_OK) {
        QZ_ERROR("Err: fail to get default params.\n");
        return NULL; //<-- LEAK HERE
    }