Skip to content

bcftools gtcheck --no-HWE-prob segfaults trying to access uninitialized args->nmatch array #2180

@freeseek

Description

@freeseek

If you run bcftools gtcheck --no-HWE-prob the array args->nmatch is not initialized:

if ( args->calc_hwe_prob )
{
    // prob of the observed sequence of matches given site AFs and HWE
    args->hwe_prob = (double*) calloc(args->npairs,sizeof(*args->hwe_prob));
    if ( !args->hwe_prob ) error("Error: failed to allocate %.1f Mb. Run with --no-HWE-prob to save some memory.\n", args->npairs*sizeof(*args->hwe_prob)/1e6);
    args->nmatch = (uint32_t*) calloc(args->npairs,sizeof(*args->ncnt));         // number of matches, used only with calc_hwe_prob
    if ( !args->nmatch ) error("Error: failed to allocate %.1f Mb.\n", args->npairs*sizeof(*args->ncnt)/1e6);
}

At the end of the run gtcheck writes the report with the following command:

if ( args->ndiff )
{
    ksprintf(&args->kstr,"DCv2\t%s\t%s\t%u\t%e\t%u\t%u\n",
            args->qry_hdr->samples[iqry],
            args->gt_hdr?args->gt_hdr->samples[igt]:args->qry_hdr->samples[igt],
            args->ndiff[idx],
            (args->calc_hwe_prob && args->nmatch[idx]) ? args->hwe_prob[idx]/args->nmatch[idx] : 0,
            args->ncnt[idx],
            args->nmatch[idx]);
}
else
{
    ksprintf(&args->kstr,"DCv2\t%s\t%s\t%e\t%e\t%u\t%u\n",
            args->qry_hdr->samples[iqry],
            args->gt_hdr?args->gt_hdr->samples[igt]:args->qry_hdr->samples[igt],
            args->pdiff[idx],
            (args->calc_hwe_prob && args->nmatch[idx]) ? args->hwe_prob[idx]/args->nmatch[idx] : 0,
            args->ncnt[idx],
            args->nmatch[idx]);
}

But there is no check for args->calc_hwe_prob when accessing args->nmatch[idx]. The two instances of args->nmatch[idx] should be replaced with args->calc_hwe_prob ? args->nmatch[idx] : 0 or something similar

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions